I have made for this question an easier example of two arrays: The labelArray
is a 1D-array has labels at indices which corresponds to the same indices of the nD-array someValuesArray
. I get all indices where label 2 occurs and want to retrieve the values in someValuesArray
.
labelArray = [2,0,1,2]
someValuesArray= [array([[ 2.15072 , 2.12438 , 2.27047 , 2.64567 ,
2.22976 , 2.18186 ]], dtype=float32),
array([ [ 2.29442, 2.3087 , 2.3111 , 2.1962 , 2.23694, 2.16988]], dtype=float32)),
array([[2.82851 , 2.73032 , 2.78301 , 1.71722 , 1.81542 , 1.78189 ]], dtype=float32)),
array([[ 1.19271, 1.14721, 1.27894 , 1.16637, 1.23343, 1.21666]], dtype=float32)]
So if I want label 2, I get indices 0 and 3, which should give me the values of the indices 0 and 3 in the corresponding array someValuesArray
.
But I receive a TypeError @ array[indices]
when I want to call my function.
TypeError: only integer scalar arrays can be converted to a scalar index
My function:
def searchValues(array, value):
labelArray = [2,0,1,2]
values_of_array = np.array(labelArray)
indices = np.where(values_of_array == value)[0]
return array[indices]
searchValues(someValuesArray,2)
array[indices]
line? That expression should create a new array, a subset ofarray
. But you don't do anything with that result. It isn't an in-place operation. – Deacharray
in that function really annumpy.ndarray
? I suspect it's a list. – Deach