13

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)
2
  • What's the purpose of the array[indices] line? That expression should create a new array, a subset of array. But you don't do anything with that result. It isn't an in-place operation. Commented Nov 24, 2018 at 20:11
  • 1
    But as to the error, is array in that function really an numpy.ndarray? I suspect it's a list. Commented Nov 24, 2018 at 20:12

2 Answers 2

13

As mentioned in the comments, someValuesArray is a list of 2d numpy arrays. I've converted that to an np.array. The code sample in your question attempts to index a python list with a numpy array, which causes the error message you receive.

In [111]: a=np.array(someValuesArray) # Convert to a numpy array

In [112]: a
Out[112]:
array([[[ 2.15071988,  2.12438011,  2.2704699 ,  2.64566994,  2.22975993,      2.18185997]],
[[ 2.29442   ,  2.30870008,  2.31110001,  2.19619989,  2.23693991,  2.16987991]],
[[ 2.82851005,  2.73031998,  2.78301001,  1.71721995,  1.81542003,  1.78189003]],
[[ 1.19271004,  1.14721   ,  1.27893996,  1.16637003,  1.23343003,  1.21666002]]], dtype=float32)

In [113]: def searchValues(array, value):
              labelArray = [2,0,1,2]
              values_of_array = np.array(labelArray)
              indices = np.where(values_of_array == value)[0]
              # print statements added to see what's happening
              print("Indices: ", indices)
              print("Array selection: \n", array[indices])
              return array

          searchValues(a,2)
[Out]
Indices:  [0 3]
Array selection:
[[[ 2.15071988  2.12438011  2.2704699   2.64566994  2.22975993  2.18185997]] # a[0]
[[ 1.19271004  1.14721     1.27893996  1.16637003  1.23343003  1.21666002]]] # a[3]

Out[113]:
array(
[[[ 2.15071988,  2.12438011,  2.2704699 ,  2.64566994,  2.22975993,  2.18185997]],
 [[ 2.29442   ,  2.30870008,  2.31110001,  2.19619989,  2.23693991,  2.16987991]],
 [[ 2.82851005,  2.73031998,  2.78301001,  1.71721995,  1.81542003,  1.78189003]],
 [[ 1.19271004,  1.14721   ,  1.27893996,  1.16637003,  1.23343003,  1.21666002]]],      dtype=float32)

Both indices returned by indices = np.where(values_of_array == value)[0] are used to point to rows in the array.

You have returned the entire array from the function: did you really mean to return array[indices]?

Sign up to request clarification or add additional context in comments.

1 Comment

I edited my comment, forgot to edit the return. I forgot to change someValuesArray into a numpy array. Now it works for me. Thanks.
5

I can reproduce your error message with:

List index with a scalar array:

In [7]: [1,2,3,4][np.array(1)]
Out[7]: 2

List index with an (1,) shape array; one element but not a scalar array:

In [8]: [1,2,3,4][np.array([1])]
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-8-4ad73b219fa3> in <module>()
----> 1 [1,2,3,4][np.array([1])]

TypeError: only integer scalar arrays can be converted to a scalar index

But it's ok to index an array like this:

In [9]: np.array([1,2,3,4])[np.array([1])]
Out[9]: array([2])

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.