且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

numpy数组TypeError:只能将整数标量数组转换为标量索引

更新时间:2021-12-18 04:01:50

简答:

[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]

...但它没有矢量化从性能优化的角度来看。在引擎盖下,它是$ c>循环的普通旧,它逐个计算每个项目。

... 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.