numpy 数组类型错误:只有整数标量数组可以转换为标量索引 [英] numpy array TypeError: only integer scalar arrays can be converted to a scalar index

查看:60
本文介绍了numpy 数组类型错误:只有整数标量数组可以转换为标量索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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 want to vectorize the array to print them all together. I try

a[:,0:i]

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

它给出了类型错误:只有整数标量数组可以转换为标量索引

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]

...但它在性能优化的意义上不是矢量化的.在引擎盖下,它是一个简单的旧 for 循环,它一个一个地计算每个项目.

... 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 数组类型错误:只有整数标量数组可以转换为标量索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆