How to add and concate two variables in same list - python-3.x

array = [[1676, 196, 159, 29, 'invoice'], [1857, 198, 108, 28, 'date:']]
width = 159+108 = 267
height = 29+28 = 57
label = invoice date:
Required solution: [1676, 196, 267, 57, 'invoice date:']
Is there any solution to concatenate string and add numbers in same list

Assuming your other lines are for your own notes/testing, and your "required solution" is a typo, you can use zip like so:
array = [[1676, 196, 159, 29, 'invoice'], [1857, 198, 108, 28, 'date:']]
res = []
for x, y in zip(array[0], array[1]): # compare values at each index
if isinstance(x, str): # check to see if x is a string
res.append(x + ' ' + y) # add a space if x is a string
else:
res.append(x + y) # add the two integers if x is not a string
print(res)
[3533, 394, 267, 57, 'invoice date:']
Note that the concatenation above will only work if you are sure that the same indices will have strings vs. integers.

Related

Double integrate with sipy

I want ask how can calculate this integral with python
enter image description here
from scipy import integrate
Hight_above_ground = 2.5
surface_ground = 81
surface_collector = 65
f = (np.cos(tilt)*np.cos(tilt)) / (PI * s **2 )
(1/surface_ground) * integrate.dblquad(f, 0, surface_ground, 0, surface_collector)
NB : A1 and A2 are 2 surface areas A1 is surface_gound and A2 is surface_collector
and this the error given
Traceback (most recent call last):
File "Bifacial_systems.py", line 34, in <module>
(1/surface_ground)*integrate.dblquad(f, 0, surface_ground, 0, surface_collector)
File "C:\Users\S1-DEV-Manel\AppData\Local\Programs\Python\Python36\lib\site-packages\scipy\integrate\quadpack.py", line 602, in dblquad
opts={"epsabs": epsabs, "epsrel": epsrel})
File "C:\Users\S1-DEV-Manel\AppData\Local\Programs\Python\Python36\lib\site-packages\scipy\integrate\quadpack.py", line 826, in nquad
return _NQuad(func, ranges, opts, full_output).integrate(*args)
File "C:\Users\S1-DEV-Manel\AppData\Local\Programs\Python\Python36\lib\site-packages\scipy\integrate\quadpack.py", line 881, in integrate
**opt)
File "C:\Users\S1-DEV-Manel\AppData\Local\Programs\Python\Python36\lib\site-packages\scipy\integrate\quadpack.py", line 352, in quad
points)
File "C:\Users\S1-DEV-Manel\AppData\Local\Programs\Python\Python36\lib\site-packages\scipy\integrate\quadpack.py", line 463, in _quad
return _quadpack._qagse(func,a,b,args,full_output,epsabs,epsrel,limit)
File "C:\Users\S1-DEV-Manel\AppData\Local\Programs\Python\Python36\lib\site-packages\scipy\integrate\quadpack.py", line 881, in integrate
**opt)
File "C:\Users\S1-DEV-Manel\AppData\Local\Programs\Python\Python36\lib\site-packages\scipy\integrate\quadpack.py", line 352, in quad
points)
File "C:\Users\S1-DEV-Manel\AppData\Local\Programs\Python\Python36\lib\site-packages\scipy\integrate\quadpack.py", line 463, in _quad
return _quadpack._qagse(func,a,b,args,full_output,epsabs,epsrel,limit)
ValueError: invalid callable given
You have to rewrite your integrand making expressing expressiong dA1 and dA2 in terms of theta1, theta2, d theta1 and d theta2. Assuming that dA1 dA2 = d theta1 d theta2 the solution would be
from scipy.integrate import dblquad
def integrand(theta1, theta2, S):
return np.cos(theta1)*np.cos(theta2)/S**2
S = 1
dblquad(integrand, 0, np.pi/2, 0, lambda x: np.pi/2-x, args=(S,))
In case you end up with an integral of higher order you can use nquad

KeyError: 0 - Function works initially, but returns error when called on other data

I have a function:
def create_variables(name, probabilities, labels):
print('function called')
model = Metrics(probabilities, labels)
prec_curve = model.precision_curve()
kappa_curve = model.kappa_curve()
tpr_curve = model.tpr_curve()
fpr_curve = model.fpr_curve()
pr_auc = auc(tpr_curve, prec_curve)
roc_auc = auc(fpr_curve, tpr_curve)
auk = auc(fpr_curve, kappa_curve)
return [name, prec_curve, kappa_curve, tpr_curve, fpr_curve, pr_auc, roc_auc, auk]
I have the following variables:
svm = pd.read_csv('SVM.csv')
svm_prob_1 = svm.probability[svm.fold_number == 1]
svm_prob_2 = svm.probability[svm.fold_number == 2]
svm_label_1 = svm.true_label[svm.fold_number == 1]
svm_label_2 = svm.true_label[svm.fold_number == 2]
I want to execute the following lines:
svm1 = create_variables('svm_fold1', svm_prob_1, svm_label_1)
svm2 = create_variables('svm_fold2', svm_prob_2, svm_label_2)
Python works as expected for svm1. However, when it starts processing svm2, I receive the following error:
svm2 = create_variables('svm_fold2', svm_prob_2, svm_label_2)
function called
Traceback (most recent call last):
File "<ipython-input-742-702cfac4d100>", line 1, in <module>
svm2 = create_variables('svm_fold2', svm_prob_2, svm_label_2)
File "<ipython-input-741-b8b5a84f0298>", line 6, in create_variables
prec_curve = model.precision_curve()
File "<ipython-input-734-dd9c309be961>", line 59, in precision_curve
self.tp, self.tn, self.fp, self.fn = self.confusion_matrix(self.preds)
File "<ipython-input-734-dd9c309be961>", line 72, in confusion_matrix
if pred == self.labels[i]:
File "C:\Users\20200016\AppData\Local\Continuum\anaconda3\lib\site-packages\pandas\core\series.py", line 1068, in __getitem__
result = self.index.get_value(self, key)
File "C:\Users\20200016\AppData\Local\Continuum\anaconda3\lib\site-packages\pandas\core\indexes\base.py", line 4730, in get_value
return self._engine.get_value(s, k, tz=getattr(series.dtype, "tz", None))
File "pandas\_libs\index.pyx", line 80, in pandas._libs.index.IndexEngine.get_value
File "pandas\_libs\index.pyx", line 88, in pandas._libs.index.IndexEngine.get_value
File "pandas\_libs\index.pyx", line 131, in pandas._libs.index.IndexEngine.get_loc
File "pandas\_libs\hashtable_class_helper.pxi", line 992, in pandas._libs.hashtable.Int64HashTable.get_item
File "pandas\_libs\hashtable_class_helper.pxi", line 998, in pandas._libs.hashtable.Int64HashTable.get_item
KeyError: 0
svm_prob_1 and svm_prob_2 are both of the same shape and contain non-zero values. svm_label_2 contains 0's and 1's and has the same length as svm_prob_2.
Furthermore, the error seems to be in svm_label_1. After changing this variable, the following line does work:
svm2 = create_variables('svm_fold2', svm_prob_2, svm_label_1
Based on the code below, there seems to be no difference between svm_label_1 and svm_label_2 though.
type(svm_label_1)
Out[806]: pandas.core.series.Series
type(svm_label_2)
Out[807]: pandas.core.series.Series
min(svm_label_1)
Out[808]: 0
min(svm_label_2)
Out[809]: 0
max(svm_label_1)
Out[810]: 1
max(svm_label_2)
Out[811]: 1
sum(svm_label_1)
Out[812]: 81
sum(svm_label_2)
Out[813]: 89
len(svm_label_1)
Out[814]: 856
len(svm_label_2)
Out[815]: 856
Does anyone know what's going wrong here?
I don't know why it works, but converting svm_label_2 into a list worked:
svm_label_2 = list(svm.true_label[svm.fold_number == 2])
Since, svm_label_1 and svm_label_2 are of the same type, I don't understand why the latter raised an error and the first one did not. Therefore, I still welcome any explanation to this phenomenon.

Dask map_blocks - IndexError: tuple index out of range

I want to do the following with Dask:
Load a matrix from a HDF5 file
Parallelize the calculation of each entry
Here is my code:
def blocked_func(x):
return np.random.random()
with h5py.File(file_path) as f:
d = f['/data']
arr = da.from_array(d, chunks=(chunks_row, chunks_col))
arr2 = arr.map_blocks(blocked_func, dtype='float32').compute()
But the code throws the following error:
File ".../remote_fr_thinkpad/test_big_data.py", line 43, in <module>
arr2 = arr.map_blocks(blocked_func, dtype='float32').compute()
File ".../anaconda3/lib/python3.7/site-packages/dask/base.py", line 156, in compute
(result,) = compute(self, traverse=False, **kwargs)
File ".../anaconda3/lib/python3.7/site-packages/dask/base.py", line 399, in compute
return repack([f(r, *a) for r, (f, a) in zip(results, postcomputes)])
File ".../anaconda3/lib/python3.7/site-packages/dask/base.py", line 399, in <listcomp>
return repack([f(r, *a) for r, (f, a) in zip(results, postcomputes)])
File ".../anaconda3/lib/python3.7/site-packages/dask/array/core.py", line 779, in finalize
return concatenate3(results)
File ".../anaconda3/lib/python3.7/site-packages/dask/array/core.py", line 3497, in concatenate3
chunks = chunks_from_arrays(arrays)
File ".../anaconda3/lib/python3.7/site-packages/dask/array/core.py", line 3327, in chunks_from_arrays
result.append(tuple([shape(deepfirst(a))[dim] for a in arrays]))
File ".../anaconda3/lib/python3.7/site-packages/dask/array/core.py", line 3327, in <listcomp>
result.append(tuple([shape(deepfirst(a))[dim] for a in arrays]))
IndexError: tuple index out of range
I googled around and also tried dask's gu_func, but that threw the same error.
Thanks for your help.
map_block expects blocked_func to return an array of the same shape of its input (chunks_row, chunks_col), while it actually just returns a float.
Try either with
1) a function which preserves shape, e.g:
def blocked_func(x):
return x*2
or
2) tell map_blocks that the shape of the output will be different:
arr2 = arr.map_blocks(blocked_func, chunks=(1,1), dtype='float32').compute()
but keep the dimensionality of the input array in blocked_func, e.g.:
def blocked_func(x):
return np.random.random()[None,None]
# or like this
# return np.array([1,1])

Efficient Find Next Greater in Another Array

Is it possible to remove the for loops in this function and get a speed up in the process? I have not been able to get the same results with vector methods for this function. Or is there another option?
import numpy as np
indices = np.array(
[814, 935, 1057, 3069, 3305, 3800, 4093, 4162, 4449])
within = np.array(
[193, 207, 243, 251, 273, 286, 405, 427, 696,
770, 883, 896, 1004, 2014, 2032, 2033, 2046, 2066,
2079, 2154, 2155, 2156, 2157, 2158, 2159, 2163, 2165,
2166, 2167, 2183, 2184, 2208, 2210, 2212, 2213, 2221,
2222, 2223, 2225, 2226, 2227, 2281, 2282, 2338, 2401,
2611, 2612, 2639, 2640, 2649, 2700, 2775, 2776, 2785,
3030, 3171, 3191, 3406, 3427, 3527, 3984, 3996, 3997,
4024, 4323, 4331, 4332])
def get_first_ind_after(indices, within):
"""returns array of the first index after each listed in indices
indices and within must be sorted ascending
"""
first_after_leading = []
for index in indices:
for w_ind in within:
if w_ind > index:
first_after_leading.append(w_ind)
break
# convert to np array
first_after_leading = np.array(first_after_leading).flatten()
return np.unique(first_after_leading)
It should return the next greatest number for each in the indices array if there is one.
# Output:
[ 883 1004 2014 3171 3406 3984 4323]
Here's one based on np.searchsorted -
def next_greater(indices, within):
idx = np.searchsorted(within, indices)
idxv = idx[idx<len(within)]
idxv_unq = np.unique(idxv)
return within[idxv_unq]
Alternatively, idxv_unq could be computed like so and should be more efficient -
idxv_unq = idxv[np.r_[True,idxv[:-1] != idxv[1:]]]
Try this:
[within[within>x][0] if len(within[within>x])>0 else 0 for x in indices]
As in,
In [35]: import numpy as np
...: indices = np.array([814, 935, 1057, 3069, 3305, 3800, 4093, 4162, 4449])
...:
...: within = np.array(
...: [193, 207, 243, 251, 273, 286, 405, 427, 696,
...: 770, 883, 896, 1004, 2014, 2032, 2033, 2046, 2066,
...: 2079, 2154, 2155, 2156, 2157, 2158, 2159, 2163, 2165,
...: 2166, 2167, 2183, 2184, 2208, 2210, 2212, 2213, 2221,
...: 2222, 2223, 2225, 2226, 2227, 2281, 2282, 2338, 2401,
...: 2611, 2612, 2639, 2640, 2649, 2700, 2775, 2776, 2785,
...: 3030, 3171, 3191, 3406, 3427, 3527, 3984, 3996, 3997,
...: 4024, 4323, 4331, 4332])
In [36]: [within[within>x][0] if len(within[within>x])>0 else 0 for x in indices]
Out[36]: [883, 1004, 2014, 3171, 3406, 3984, 4323, 4323, 0]
This is the pythonic approach called list comprehension it's a shortened version of a foreach loop. So if I were to expand this out:
result = []
for x in indices:
# This next line is a boolean index into the array, if returns all of the items in the array that have a value greater than x
y = within[within>x]
# At this point, y is an array of all the items which are larger than x. Since you wanted the first of these items, we'll just take the first item off of this new array, but it is possible that y is None (there are no values that match the condition), so there is a check for that
if len(y) > 0:
z = y[0]
else:
z = 0 # or None or whatever you like
# Now add this value to the array that we are building
result.append(z)
# Now result has the array
I wrote it this way, because it uses the vector operations (i.e. the boolean mask) and also leverages list comprehension, which is a much cleaner simpler way to write a foreach which returns an array.

del or listl.remove dont work python 3.5

This programm have to order a list of numbersbut every time i become a error can you help me to fix it? I hope somebody has an idea. I hzave also tried to us del list[(thenumber of the element or the number)]
# list = list with the none ordert number
# newlist = with the ordert numbers
# pnumbver = privious number
# add = new number for list
# numberelemente = how many numbers get in list
# length = length of list
# i = counting up for the stop
from random import randint
list = []
newlist = []
numberelemente = 10
while numberelemente > 0:
add = randint(-100, 100)
list.append(add)
numberelemente = numberelemente - 1
print(list)
pnumber=list[0]
length = len(list)
i = 0
while i < length:
for zahl in list:
if number < pnumber:
pnumber = number
list.remove(pnumber)
newlist.append(pnumber)
i = i+1
print(newlist)
but i become this error i become them every time
eenter code her>>> runfile('C:/Users/Max/Desktop/python/liste ordnen.py', wdir='C:/Users/Max/Desktop/python')
[89, 46, 68, -30, 93, 38, -73, 91, 33, -69]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Users\Max\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 866, in runfile
execfile(filename, namespace)
File "C:\Users\Max\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 102, in execfile
exec(compile(f.read(), filename, 'exec'), namespace)
File "C:/Users/Max/Desktop/python/liste ordnen.py", line 29, in <module>
list.remove(vzahl)
ValueError: list.remove(x): x not in list
I never say that you added pnumber to list so it cant delete it. Don't you mean to use.
list.append(pnumber)
This is what list.remove() does
list.remove(x)
Remove the first item from the list whose value is x. It is an error if there is no such item.
-https://docs.python.org/3/tutorial/datastructures.html
Hope this helps.

Resources