Python numpy 数组类型错误:只有整数标量数组可以转换为标量索引
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/46902367/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
numpy array TypeError: only integer scalar arrays can be converted to a scalar index
提问by kinder chen
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
它给出了类型错误:只有整数标量数组可以转换为标量索引
回答by Maxim
Short answer:
简短的回答:
[a[:,:j] for j in i]
What you are trying to do is not a vectorizable operation. Wikipedia definesvectorization 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 vectorizedoperation...
在计算机科学中,数组编程语言(也称为向量或多维语言)将标量运算泛化为透明地应用于向量、矩阵和高维数组。
...
...对整个数组进行操作的操作可以称为向量化操作...
In terms of CPU-level optimization, the definition of vectorizationis:
在CPU级优化方面,vectorization的定义是:
"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.
“向量化”(简化)是重写循环的过程,这样它不是处理数组的单个元素 N 次,而是同时处理(比如说)数组的 4 个元素 N/4 次。
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.
您的案例的问题在于每个单独操作的结果具有不同的形状:(3, 1)
,(3, 2)
和(3, 3)
。它们不能形成单个向量化操作的输出,因为输出必须是一个连续的数组。当然,它可以包含(3, 1)
,(3, 2)
和其中的(3, 3)
数组(作为视图),但这就是您的原始数组a
已经做到的。
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.
...但它没有在性能优化意义上进行矢量化。在引擎盖下,它是一个简单的旧for
循环,它一项一项地计算每个项目。
回答by Zhanwen Chen
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:
这可能与这个特定问题无关,但我遇到了一个类似的问题,我在 Python 列表上使用了 NumPy 索引并得到了完全相同的错误消息:
# 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:
事实证明weights
,在应用多维 NumPy 索引之前,我需要将1D Python 列表转换为 NumPy 数组。下面的代码有效:
# correct
weights = np.array(list(range(1, 129)) + list(range(128, 0, -1)))
mapped_image = weights[image[:, :, band]] # image.shape = [800, 600, 3]
回答by WIND.Knight
try the following to change your array to 1D
尝试以下将您的阵列更改为一维
a.reshape((1, -1))
回答by user2961818
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.
我在冒险使用 numpy.concatenate 来模拟 C++ 时遇到了这个问题,比如 2D 向量的推回;如果 A 和 B 是两个 2D numpy.arrays,则 numpy.concatenate(A,B) 会产生错误。
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 singleargument
解决方法是简单地添加缺少的括号: numpy.concatenate( (A,B )),这是必需的,因为要连接的数组构成单个参数
回答by Andre Araujo
You can use numpy.ravel to return a flattened array from n-dimensional array:
您可以使用 numpy.ravel 从 n 维数组返回扁平数组:
>>> a
array([[0, 1, 2],
[3, 4, 5],
[6, 7, 8]])
>>> a.ravel()
array([0, 1, 2, 3, 4, 5, 6, 7, 8])
回答by ShadyMBA
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))
回答by Athil Althaf
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
当我们使用向量代替标量时会出现这个问题,例如在 for 循环中,范围应该是标量,如果您在那个地方给出了一个向量,则会出错。所以为了避免这个问题,使用你使用过的向量的长度