numpy数组列切片产生IndexError:无效的索引异常 [英] Numpy Array Column Slicing Produces IndexError: invalid index Exception

查看:114
本文介绍了numpy数组列切片产生IndexError:无效的索引异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用numpy的1.5.1版本和Python 2.6.6.

I am using version 1.5.1 of numpy and Python 2.6.6.

我正在将一个二进制文件读入numpy数组:

I am reading a binary file into a numpy array:

>>> dt = np.dtype('<u4,<i2,<i2,<i2,<i2,<i2,<i2,<i2,<i2,u1,u1,u1,u1')
>>> file_data = np.fromfile(os.path.join(folder,f), dtype=dt)

这很好用.检查结果:

>>> type(file_data)
<type 'numpy.ndarray'>

>>> file_data
array([(3571121L, -54, 103, 1, 50, 48, 469, 588, -10, 0, 102, 0, 0),
   (3571122L, -78, 20, 25, 45, 44, 495, 397, -211, 0, 102, 0, 0),
   (3571123L, -69, -48, 23, 60, 19, 317, -26, -151, 0, 102, 0, 0), ...,
   (3691138L, -53, 52, -2, -11, 76, 988, 288, -101, 1, 102, 0, 0),
   (3691139L, -11, 21, -27, 25, 47, 986, 253, 176, 1, 102, 0, 0),
   (3691140L, -30, -19, -63, 59, 12, 729, 23, 302, 1, 102, 0, 0)],
  dtype=[('f0', '<u4'), ('f1', '<i2'), ('f2', '<i2'), ... , ('f12', '|u1')])

>>> file_data[0]
(3571121L, -54, 103, 1, 50, 48, 469, 588, -10, 0, 102, 0, 0)

>>> file_data[0][0]
3571121    

>>> len(file_data)
120020

当我尝试切片第一列时:

When I try to slice the first column:

>>> file_data[:,0]

我得到:

IndexError: invalid index.

我看了一些简单的例子,并能够进行切片:

I have looked at simple examples and was able to do the slicing:

>>> a = np.array([(1,2,3),(4,5,6)])
>>> a[:,0]
array([1, 4])

我的案例和简单示例之间的唯一区别是我正在使用dtype.我在做什么错了?

The only difference I can see between my case and the simple example is that I am using the dtype. What I am doing wrong?

推荐答案

像这样设置dtype时,您正在创建

When you set the dtype like that, you are creating a Record Array. Numpy treats that like a 1D array of elements of your dtype. There's a fundamental difference between

file_data[0][0]

file_data[0,0] 

首先,您要查询一维数组的第一个元素,然后检索该返回元素的第一个元素.在第二个中,您要在2D数组的第一列的第一行中寻找元素.这就是为什么得到IndexError.

In the first, you are asking for the first element of a 1D array and then retrieving the first element of that returned element. In the second, you are asking for the element in the first row of the first column of a 2D array. That's why you are getting the IndexError.

如果要使用2D表示法访问单个元素,则可以创建视图并进行处理.不幸的是,如果您想将对象视为2D数组,则AFAIK的所有元素都必须具有相同的dtype.

If you want to access an individual element using 2D notation, you can create a view and work with that. Unfortunately, AFAIK if you want to treat your object like a 2D array, all elements have to have the same dtype.

这篇关于numpy数组列切片产生IndexError:无效的索引异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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