75
i=np.arange(1,4,dtype=np.int)
a=np.arange(9).reshape(3,3)

and

a
>>>array([[0, 1, 2],
          [3, 4, 5],
          [6, 7, 8]])
a[:,0:1]
>>>array([[0],
          [3],
          [6]])
a[:,0:2]
>>>array([[0, 1],
          [3, 4],
          [6, 7]])
a[:,0:3]
>>>array([[0, 1, 2],
          [3, 4, 5],
          [6, 7, 8]])

Now I want to vectorize the array to print them all together. I try

a[:,0:i]

or

a[:,0:i[:,None]]

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

sifferman
  • 2,955
  • 2
  • 27
  • 37
kinder chen
  • 1,371
  • 5
  • 15
  • 25

8 Answers8

44

I ran into the problem when venturing to use numpy.concatenate to emulate a C++ like pushback for 2D-vectors; If A and B are two 2D numpy.arrays, then numpy.concatenate(A,B) yields the error.

The fix was to simply to add the missing brackets: numpy.concatenate( ( A,B ) ), which are required because the arrays to be concatenated constitute to a single argument

user2961818
  • 721
  • 5
  • 11
39

Short answer:

[a[:,:j] for j in i]

What you are trying to do is not a vectorizable operation. Wikipedia defines vectorization as a batch operation on a single array, instead of on individual scalars:

In computer science, array programming languages (also known as vector or multidimensional languages) generalize operations on scalars to apply transparently to vectors, matrices, and higher-dimensional arrays.

...

... an operation that operates on entire arrays can be called a vectorized operation...

In terms of CPU-level optimization, the definition of vectorization is:

"Vectorization" (simplified) is the process of rewriting a loop so that instead of processing a single element of an array N times, it processes (say) 4 elements of the array simultaneously N/4 times.

The problem with your case is that the result of each individual operation has a different shape: (3, 1), (3, 2) and (3, 3). They can not form the output of a single vectorized operation, because the output has to be one contiguous array. Of course, it can contain (3, 1), (3, 2) and (3, 3) arrays inside of it (as views), but that's what your original array a already does.

What you're really looking for is just a single expression that computes all of them:

[a[:,:j] for j in i]

... but it's not vectorized in a sense of performance optimization. Under the hood it's plain old for loop that computes each item one by one.

Maxim
  • 52,561
  • 27
  • 155
  • 209
22

This could be unrelated to this specific problem, but I ran into a similar issue where I used NumPy indexing on a Python list and got the same exact error message:

# incorrect
weights = list(range(1, 129)) + list(range(128, 0, -1))
mapped_image = weights[image[:, :, band]] # image.shape = [800, 600, 3]
# TypeError: only integer scalar arrays can be converted to a scalar index

It turns out I needed to turn weights, a 1D Python list, into a NumPy array before I could apply multi-dimensional NumPy indexing. The code below works:

# correct
weights = np.array(list(range(1, 129)) + list(range(128, 0, -1)))
mapped_image = weights[image[:, :, band]] # image.shape = [800, 600, 3]
Zhanwen Chen
  • 1,295
  • 17
  • 21
13

try the following to change your array to 1D

a.reshape((1, -1))
WIND.Knight
  • 346
  • 2
  • 9
  • https://stackoverflow.com/questions/48513824/compute-mfcc-for-varying-time-intervals-based-on-time-stamps I have similar error how do I address it. – DJ_Stuffy_K Feb 01 '18 at 04:16
8

You can use numpy.ravel to return a flattened array from n-dimensional array:

>>> a
array([[0, 1, 2],
       [3, 4, 5],
       [6, 7, 8]])
>>> a.ravel()
array([0, 1, 2, 3, 4, 5, 6, 7, 8])
Andre Araujo
  • 2,348
  • 2
  • 27
  • 41
7

I had a similar problem and solved it using list...not sure if this will help or not

classes = list(unique_labels(y_true, y_pred))
Til
  • 5,150
  • 13
  • 26
  • 34
Chadee Fouad
  • 2,630
  • 2
  • 23
  • 29
  • 1
    You're right...unfortunately I'm not quite sure how it was solved...I had a working example and when I compared the output of example vs my code I found that the example code in this line was generating a list of items...so I thought of making mine a list too and that fixed it. Thanks. – Chadee Fouad Mar 28 '19 at 11:56
2

this problem arises when we use vectors in place of scalars for example in a for loop the range should be a scalar, in case you have given a vector in that place you get error. So to avoid the problem use the length of the vector you have used

0

I ran across this error when while trying to access elements of a list using a 1-D array. I was suggested this page but I don't the answer I was looking for.

Let l be the list and myarray be my 1D array. The correct way to access list l using elements of myarray is

np.take(l,myarray)

Pixel_Bear
  • 97
  • 1
  • 1
  • 11