Python 索引错误:索引过多。1 行 2 列的 Numpy 数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29199585/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 04:11:33  来源:igfitidea点击:

IndexError: too many indices. Numpy Array with 1 row and 2 columns

pythonarraysnumpy

提问by Stripers247

When I try to get just the first element of an array like this

当我尝试像这样只获取数组的第一个元素时

import numpy

a = numpy.array([1,2])

a[:,0]

I get this error

我收到这个错误

---------------------------------------------------------------------------
 IndexError                                Traceback (most recent call last)
<ipython-input-3-ed371621c46c> in <module>()
----> 1 a[:,0]

IndexError: too many indices

I would like to find a way to do this while still using slicing because the full code opens and reads many different files using numpy.loadtxt()all having two columns which vary from 1 to some N.

我想找到一种方法来做到这一点,同时仍然使用切片,因为完整的代码打开并读取许多不同的文件,numpy.loadtxt()所有文件都有两列,从 1 到一些 N 不等。

采纳答案by Alex Riley

Your array a = numpy.array([1,2])only has onedimension: its shape is (2,). However, your slice a[:,0]specifies selections for twodimensions. This causes NumPy to raise the error.

你的数组a = numpy.array([1,2])只有维:它的形状是(2,). 但是,您的切片 a[:,0]指定了两个维度的选择。这会导致 NumPy 引发错误。

To get the first element from ayou only need to write a[0](a selection for only one dimension is being made here).

要从您那里获取第一个元素,a只需编写a[0](此处仅选择一个维度)。



Looking at your other question, if you want to ensure that the syntax a[:,0]always works, you could ensure that aalways has two dimensions. When loading an array with np.loadtxtuse the ndminparameter, e.g.:

看看你的另一个问题,如果你想确保语法a[:,0]总是有效,你可以确保a总是有两个维度。np.loadtxt使用ndmin参数加载数组时,例如:

np.loadtxt(F, skiprows=0, ndmin=2)

回答by rkp

As mentioned above, you have a one-dimensional array and you're trying to slice it with two dimensions.

如上所述,您有一个一维数组,并且您正尝试将其切片为二维。

One thing to add, and which I've found very useful in the past, is that numpy allows you easily cast a 1D array into a 2D array (either as a row or column):

要添加的一件事,我在过去发现非常有用的是,numpy 允许您轻松地将一维数组转换为二维数组(作为行或列):

>>> a = np.array([0,1,2])
>>> a.shape
(3,)
>>> a_row = a[None,:]
>>> a_row.shape
(1,3)
>>> a_col = a[:,None]
>>> a_col.shape
(3,1)

回答by Dave cook

I have also struggled with this problem when parsing many input files that can have 1-1000s of rows. However, I am using numpy genfromtxt which doesn't allow you to set 'ndmin', so the solution I came up with is to manually set the array shape equal 1 for arrays with 1 row:

在解析许多可能有 1-1000 行的输入文件时,我也遇到了这个问题。但是,我使用的 numpy genfromtxt 不允许您设置 'ndmin',所以我想出的解决方案是手动将数组形状设置为等于 1 的 1 行数组:

>>> arr=np.genfromtxt('file',names=['a','b'],dtype='f4,f4') 
>>> if (np.size(arr) == 1): arr.shape=1

The 1 row array now acts like a 1-D array that can be indexed:

1 行数组现在就像一个可以被索引的一维数组:

>>> for i in range(np.size(arr)): print arr['a'][i]