Python 索引 4 超出轴 1 的范围,大小为 4 代码为双和

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

index 4 is out of bounds for axis 1 with size 4 Code with Double Sum

pythonboundsout

提问by Shaun

Hi I have the following function which produces an out of bounds error:

嗨,我有以下函数会产生越界错误:

import numpy as np
import pylab as plt
import scipy
import math
import sympy as sy


T = sy.Symbol('T')
rho = sy.Symbol('rho')


g_T   = [1,T,T**2,T*sy.log(T),T**2*sy.log(T)]
g_rho  = [1,rho,rho**2,rho*sy.log(rho),rho**2*sy.log(rho)]

g_T_np = np.asarray(g_T)
g_rho_np = np.asarray(g_rho)


c = np.loadtxt("c_test.txt")


def F(T,rho):
    ret = 0
    for n in xrange(1,5):
        for m in xrange(1,6):
            inner= c[n,m]*g_T_np*g_rho_np
        ret += inner
    return ret

print F(T,rho)

where the .txt file is like this:

其中 .txt 文件是这样的:

-0.529586   -0.000208559    -3.36563E-09    2.29441E-05 
2.22722E-06 -0.00014526 -2.48888E-09    1.89488E-05 
-6.26662E-05    0.000421028 6.17407E-09 -5.14488E-05    
0.09977346  -0.000622051    -8.56485E-09    7.49956E-05 
-0.01437627 -9.86754E-05    -1.59808E-09    1.22574E-05

The full error displayed is:

显示的完整错误是:

Traceback (most recent call last):File "EOS_test.py", line 38, in <module> print F(T,rho) File "EOS_test.py", line 31, in F inner=c[n,m]*g_T_np*g_rho_np IndexError: index 4 is out of bounds for axis 1 with size 4

Traceback (most recent call last):File "EOS_test.py", line 38, in <module> print F(T,rho) File "EOS_test.py", line 31, in F inner=c[n,m]*g_T_np*g_rho_np IndexError: index 4 is out of bounds for axis 1 with size 4

How can I solve this error?

我该如何解决这个错误?

采纳答案by pdowling

Numpy uses 0-based indexing. From the looks of it you are indexing the array from 1 (2nd position) to 4 (5th position), which is of course out of bounds for the array you are working with. The same is true for the second axis.

Numpy 使用基于 0 的索引。从它的外观来看,您正在将数组从 1(第 2 位)索引到 4(第 5 位),这当然超出了您正在使用的数组的范围。对于第二个轴也是如此。

Secondly, you've mixed up your axes:

其次,你混淆了你的轴:

  • The first axis (0) is the index of the selected row (0 to 5)
  • the second axis indexes the column, i.e. the value inside a row (indexed 0 to 4)
  • 第一个轴 (0) 是所选行的索引(0 到 5)
  • 第二个轴索引列,即行内的值(索引为 0 到 4)

This should work:

这应该有效:

def F(T,rho):
    ret = 0
    for n in range(5):
        for m in range(4):
            inner= c[n,m]*g_T_np*g_rho_np
        ret += inner
    return ret

回答by Scott Mermelstein

Your problem is in setting your xranges.

您的问题在于设置您的xranges。

Python lists (and np arrays) are 0 indexed, so you don't want the indices [1,2,3,4,5] and [1,2,3,4,5,6], but instead want [0,1,2,3,4] and [0,1,2,3,4,5][0,1,2,3] and [0,1,2,3,4].

Python 列表(和 np 数组)的索引为 0,因此您不想要索引 [1,2,3,4,5] 和 [1,2,3,4,5,6],而是想要[0 ,1,2,3,4] 和 [0,1,2,3,4,5][0,1,2,3] 和 [0,1,2,3,4]。

Setting up your for loops like this would solve your problem:

像这样设置 for 循环可以解决您的问题:

for n in xrange(0,4):
    for m in xrange(0,5):

Or, you can take advantage of xrange's default starting point, and simply list one parameter:

或者,您可以利用 xrange 的默认起点,并简单地列出一个参数:

for n in xrange(4):
    for m in xrange(5):

Also, for a "more pythonic" solution, instead of having nested for loops, look up how to iterate over an ndarray. The docsgive this example:

此外,对于“更 Pythonic”的解决方案,不要嵌套 for 循环,而是查找如何迭代 ndarray。该文档举这个例子:

it = np.nditer(c, flags=['f_index'])
while not it.finished:
    inner= c[it[0]]*g_T_np*g_rho_np
    ret += inner
    it.iternext()

This avoids the whole issue of needing to know the size of the array you're importing, and is therefore much more robust.

这避免了需要知道您正在导入的数组大小的整个问题,因此更加健壮。

Edit: As pdowling mentioned in his answer, the range numbers should be 4 and 5. I had left 5 and 6 in my aswer, and have now changed that.

编辑:正如 pdowling 在他的回答中提到的,范围数字应该是 4 和 5。我在我的答案中留下了 5 和 6,现在已经改变了。