简体   繁体   English

切片数组的IndexError

[英]IndexError on slicing array

I assume I'm asking a newbie question but have spent too much time today searching for an answer. 我想我正在问一个新手问题,但今天花了太多时间寻找答案。 I get an IndexError: too many indices for array error when naively attempting to perform the same slice operation on a numpy array after saving and reloading with np.genfromtxt . 我得到一个IndexError: too many indices for array在使用np.genfromtxt保存和重新加载后,天真地尝试在numpy数组上执行相同的切片操作时IndexError: too many indices for array错误的IndexError: too many indices for array

Note: I see that the dimension has changed from (3,6) to (3,) upon reloading but was unable to convert the result back to dimensions (3,6)- this is the part I assume must be obvious (or maybe I need to specify type differently) 注意:我看到尺寸在重新加载时已经从(3,6)变为(3,)但是无法将结果转换回尺寸(3,6) - 这是我认为必须明显的部分(或者可能是我需要以不同的方式指定类型)

yo = np.arange(18)
yo = yo.reshape(3,6)

print(yo)
[[ 0  1  2  3  4  5]
 [ 6  7  8  9 10 11]
 [12 13 14 15 16 17]]

print(yo[:,:2])
[[ 0  1]
 [ 6  7]
 [12 13]]

np.savetxt("test_data.csv", yo, delimiter=",",  fmt='%1.4e')
yo_reloaded = np.genfromtxt("test_data.csv", dtype=(float, float, float, float, float, float), delimiter = ",")

#same as above but doesn't work
print(yo_reloaded[:,:2])
IndexError: too many indices for array

print(yo_reloaded)
[(  0.,   1.,   2.,   3.,   4.,   5.) (  6.,   7.,   8.,   9.,  10.,  11.)
 ( 12.,  13.,  14.,  15.,  16.,  17.)]

# shape changed
print(yo_reloaded.shape)
(3,)

Use dtype=None to tell genfromtxt to attempt to intelligently guess the dtype . 使用dtype=None告诉genfromtxt尝试智能地猜测dtype In this case, since all values are floats, genfromtxt will assign a floating-point dtype to the array: 在这种情况下,由于所有值都是浮点数, genfromtxt将为数组指定一个浮点genfromtxt

In [19]: yo_reloaded = np.genfromtxt("test_data.csv", dtype=None, delimiter = ",")
In [21]: yo_reloaded.dtype
Out[21]: dtype('float64')

and yo_reload will have shape (3,6) . yo_reload将有形状(3,6)

In contrast, if you set dtype=(float, float, float, float, float, float) : 相反,如果设置dtype=(float, float, float, float, float, float)

yo_reloaded = np.genfromtxt("test_data.csv", dtype=(float, float, float, float, float, float), delimiter = ",")

then yo_reloaded.dtype becomes: 然后yo_reloaded.dtype变为:

In [18]: yo_reloaded.dtype
Out[18]: dtype([('f0', '<f8'), ('f1', '<f8'), ('f2', '<f8'), ('f3', '<f8'), ('f4', '<f8'), ('f5', '<f8')])

which is the dtype of a structured array . 这是结构化数组的dtype。 The shape of the structured array is (3,) become NumPy views this array as consisting of 3 rows with each row having a single value consisting of 6 fields of floating-point dtype. 结构化数组的形状是(3,)变为NumPy视图,该数组由3行组成,每行具有由6个浮点dtype字段组成的单个值。 That's simply not what you want, but what you get when you set dtype to equal a tuple of types. 这根本不是你想要的,而是当你将dtype设置为等于类型元组时得到的结果。

Note you could also obtain the desired array using dtype=float : 请注意,您还可以使用dtype=float获取所需的数组:

In [24]: yo_reloaded = np.genfromtxt("test_data.csv", dtype=float, delimiter = ",")
In [25]: yo_reloaded.shape
Out[25]: (3, 6)
In [26]: yo_reloaded.dtype
Out[26]: dtype('float64')

Or, as hpaulj points out , you could simply omit the dtype parameter altogether, in which case it defaults to dtype=float . 或者,正如hpaulj指出的那样 ,你可以简单地省略dtype参数,在这种情况下它默认为dtype=float

if you run print(yo_reloaded.shape) before print(yo_reloaded[:,:2]) you can see that your np.genfromtxt() call returns (3,) which means 3 rows with one column data. 如果你在print(yo_reloaded[:,:2]) print(yo_reloaded.shape)之前运行print(yo_reloaded.shape) print(yo_reloaded[:,:2])你可以看到你的np.genfromtxt()调用返回(3,) ,这意味着3行有一个列数据。

When you use dtype=(float, float, float, float, float, float) you are mapping every row in "test_data.csv" 5-tuple. 当你使用dtype=(float, float, float, float, float, float)你将映射"test_data.csv" 5元组中的每一行。 So np.genfromtxt() returns every row as a 5-tuple element. 因此, np.genfromtxt()将每一行作为5元组元素返回。

In order to get the same results you have to change dtype=dtype=(float, float, float, float, float, float) to dtype=float . 为了获得相同的结果,您必须将dtype=dtype=(float, float, float, float, float, float)更改为dtype=float

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM