Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 682
kinder chen Avatar asked Oct 24 '17 04:10

kinder chen


People also ask

How do I fix TypeError only integer scalar arrays can be converted to a scalar index?

It usually can concatenate row-wise and column-wise. By default NumPy's concatenate function concatenate row-wise, to do so it requires iterable (tuple or list) to concatenate. Solution: To solve this error you need to convert array 1 and array 2 in to tuple or list.

What does only integer scalar arrays can be converted to a scalar index mean?

Solution. The error you have faced is this: TypeError: only integer scalar arrays can be converted to a scalar index. Python. This means that the index you are using to refer to an array element is wrong.

How do you fix TypeError only size 1 arrays can be converted to Python scalars?

TypeError occurs when you pass an array instead of passing a single value in the function or when you are working on NumPy and matplotlib. pyplot. To fix this, add the code and it returns vector results.

What is scalar type in NumPy?

A NumPy scalar is any object which is an instance of np.generic or whose type is in np.ScalarType : In [12]: np.ScalarType Out[13]: (int, float, complex, long, bool, str, unicode, buffer, numpy.int16, numpy.float16, numpy.int8, numpy.uint64, numpy.complex192, numpy.void, numpy.uint32, numpy.complex128, numpy.unicode_, ...


2 Answers

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.

like image 156
Maxim Avatar answered Sep 20 '22 19:09

Maxim


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

like image 37
user2961818 Avatar answered Sep 19 '22 19:09

user2961818