numpy数组TypeError:只能将整数标量数组转换为标量索引 [英] 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)
和
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 wanna vectorize the array to print them all together. I try
a[:,0:i]
或
a[:,0:i[:,None]]
它给出TypeError:只有整数标量数组可以转换为标量索引
It gives TypeError: only integer scalar arrays can be converted to a scalar index
推荐答案
简答:
[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...
就CPU级优化而言,矢量化定义是:
In terms of CPU-level optimization, the definition of vectorization is:
矢量化(简化)是重写循环的过程,这样它不是处理数组的单个元素N次,而是同时处理(比如说)数组的4个元素。 N / 4次。
"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.
您的案例的问题是每个单独操作的结果具有不同的形状:(3,1)
,(3,2)
和(3 ,3)
。它们不能形成单个矢量化操作的输出,因为输出必须是一个连续的数组。当然,它可以包含(3,1)
,(3,2)
和 (3,3)
里面的数组(作为视图),但这就是原始数组 a
已经存在的。
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.
这篇关于numpy数组TypeError:只能将整数标量数组转换为标量索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!