I want to iterate over numpy arrays stored in a list and a dictionray. My list is named as result
:
result= [array([[ 1. , 0.75 , 69.5198822 ],
...,
[99. , 29.25 , 49.51967621]]),
...
array([[ 1. , 0.75 , 78.88569641],
...,
[99. , 29.25 , 58.88546753]]),
...
array([[ 1. , 0.75 , 77.7075882 ],
...,
[99. , 29.25 , 57.70734406]])]
My dictionary is also named dicti
:
{'sub_arr1': array([[37. , 2.25 , 2.62501693],
...,
[81.68138123, 29.25 , 99. ]]),
...
'sub_arr2': array([[41. , 2.25 , 2.254704 ],
...,
[85.93613434, 29.25 , 99. ]]),
...
'sub_arr3': array([[51. , 2.25 , 1.13132851],
...,
[96.60651398, 29.25 , 99. ]])}
Than, I want to do a calculation between arrays stored in these two data sets ( result
and dicti
). I want to delete some data points of arrays of result
that are closer than a threshold (eg 10) to arrays of dicti
and store them as a new one. In fact, I want to compare the first array of result
to the first array of dicti
, second with second, third with third and so on. I can say I want to clean arrays stored in result
list, then save the new clean arrays as separate ones in a 3d numpy array or a maybe again a list of cleaned arrays. My code (that is not working!!!) is as bellow:
import numpy as np
new_result=np.array ([])
for i in result:
for key, value in dicti():
clp=result[i][np.where(np.min(distance.cdist(value, result[i]),axis=0)<10)[0],:] # it finds the points of result that are closer 10 to points stored as values of dicti
cleaned_result= npi.difference(result[i], clp) # it removes that close points
new_result=np.append (new_result, cleaned_result).
The error I am facing is TypeError: only integer scalar arrays can be converted to a scalar index
but I think my code has also other issues. In advance I appreciate any feedback.
Try something like this:
import numpy as np
new_result=np.array ([])
for res, value in zip(result, dicti.values()):
clp=res[np.where(np.min(distance.cdist(value, res),axis=0)<10)[0],:] # it finds the points of result that are closer 10 to points stored as values of dicti
cleaned_result= npi.difference(res, clp) # it removes that close points
new_result=np.append (new_result, cleaned_result).
zip
is a function that takes two or more iterators and returns in each iteration a tuple where each element is taken from each iterator.
For example:
a = [1,2,3]
b = {'A':'a', 'B':'b', 'C':'c'}
for x, y in zip(a, b.values()):
print(x, y)
This shows as output:
1 a
2 b
3 c
zip
reproduces exactly what you say:
I want to compare the first array of result to the first array of dicti, second with second, third with third and so on
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.