IndexError:数组的索引过多 [英] IndexError: too many indices for array

查看:2386
本文介绍了IndexError:数组的索引过多的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道有很多这样的线程,但是所有这些线程都适用于非常简单的情况,如3x3矩阵和那种类型的东西,解决方案甚至不适用于我的情况。所以我试图绘制G对比l1(这不是一个,但是一个L1)。数据位于从Excel文件加载的文件中。 excel文件是14x250,所以有14个参数,每个有250个数据点。我有另一个用户(喊到Hugh Bothwell!)帮助我在我的代码中的错误,但现在另一个错误浮出水面。

I know there is a ton of these threads but all of them are for very simple cases like 3x3 matrices and things of that sort and the solutions do not even begin to apply to my situation. So I'm trying to graph G versus l1 (that's not an eleven, but an L1). The data is in the file that I loaded from an excel file. The excel file is 14x250 so there are 14 arguments, each with 250 data points. I had another user (shout out to Hugh Bothwell!) help me with an error in my code, but now another error has surfaced.

这里是有问题的代码:

# format for CSV file:
header = ['l1', 'l2', 'l3', 'l4', 'l5', 'EI',
      'S', 'P_right', 'P1_0', 'P3_0',
      'w_left', 'w_right', 'G_left', 'G_right']

def loadfile(filename, skip=None, *args):
    skip = set(skip or [])
    with open(filename, *args) as f:
        cr = csv.reader(f, quoting=csv.QUOTE_NONNUMERIC)
        return np.array(row for i,row in enumerate(cr) if i not in skip)
#plot data
outputs_l1 = [loadfile('C:\\Users\\Chris\\Desktop\\Work\\Python Stuff\\BPCROOM - Shingles analysis\\ERR analysis\\l_1 analysis//BS(1) ERR analysis - l_1 - P_3 = {}.csv'.format(p)) for p in p3_arr]

col = {name:i for i,name in enumerate(header)}

fig = plt.figure()
for data,color in zip(outputs_l1, colors):
    xs  = data[:, col["l1"     ]]
    gl = data[:, col["G_left" ]] * 1000.0    # column 12
    gr = data[:, col["G_right"]] * 1000.0    # column 13
    plt.plot(xs, gl, color + "-", gr, color + "--")
for output, col in zip(outputs_l1, colors):
    plt.plot(output[:,0], output[:,11]*1E3, col+'--')
plt.ticklabel_format(axis='both', style='plain', scilimits=(-1,1))
plt.xlabel('$l1 (m)$')
plt.ylabel('G $(J / m^2) * 10^{-3}$')
plt.xlim(xmin=.2)
plt.ylim(ymax=2, ymin=0)

plt.subplots_adjust(top=0.8, bottom=0.15, right=0.7)

运行完整个程序后,我收到错误信息:

After running the entire program, I recieve the error message:

Traceback (most recent call last):
  File "C:/Users/Chris/Desktop/Work/Python Stuff/New Stuff from Brenday 8 26 2014/CD_ssa_plot(2).py", line 115, in <module>
    xs  = data[:, col["l1"     ]]
IndexError: too many indices for array

并且在我遇到这个问题之前,我有另一个涉及到一行以下的错误消息引用的一行:

and before I ran into that problem, I had another involving the line a few below the one the above error message refers to:

Traceback (most recent call last): File "FILE", line 119, in <module> 
gl = data[:, col["G_left" ]] * 1000.0 # column 12 
IndexError: index 12 is out of bounds for axis 1 with size 12

我理解第一个错误,但只是有问题修复它。第二个错误是让我困惑的。我的老板真的在呼吸我的脖子,所以任何帮助将非常感谢!

I understand the first error, but am just having problems fixing it. The second error is confusing for me though. My boss is really breathing down my neck so any help would be GREATLY appreciated!

推荐答案

我认为问题是在错误虽然不是很容易发现:

I think the problem is given in the error message, although it is not very easy to spot:

IndexError: too many indices for array
xs  = data[:, col["l1"     ]]

'太多索引'索引值。您已经给出了2个值,因为您希望数据是2D数组。 Numpy抱怨,因为数据不是2D(它是1D或无)。

'Too many indices' means you've given too many index values. You've given 2 values as you're expecting data to be a 2D array. Numpy is complaining because data is not 2D (it's either 1D or None).

这是一个猜测 - 我想知道你传递给loadfile()的文件名是否指向一个空文件或格式不正确的文件?如果是这样,你可能得到一个返回的数组,它是1D,甚至是空的( np.array(None)不会抛出 code>,所以你永远不会知道...)。如果你想防范此失败,可以在 loadfile 函数中插入一些错误检查。

This is a bit of a guess - I wonder if one of the filenames you pass to loadfile() points to an empty file, or a badly formatted one? If so, you might get an array returned that is either 1D, or even empty (np.array(None) does not throw an Error, so you would never know...). If you want to guard against this failure, you can insert some error checking into your loadfile function.

推荐在中插入

print(data)

这将在Python 2.x或3.x中工作,并可能揭示问题的根源。您可能发现只有您的 outputs_l1 列表(即一个文件)中的一个值是发出此问题的。

This will work in Python 2.x or 3.x and might reveal the source of the issue. You might well find it is only one value of your outputs_l1 list (i.e. one file) that is giving the issue.

这篇关于IndexError:数组的索引过多的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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