Python 对于大小为 1 的轴 0,索引 1 超出范围

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

index 1 is out of bounds for axis 0 with size 1

pythonnumpystatistics

提问by SIDDHESH THAKUR

I don't know why I am getting an error of index. I am quite new to python and therefore am not able to figure out what to do. I think I am initialzing some wrong dimensions but I am not able to break it.

我不知道为什么我收到索引错误。我对python很陌生,因此无法弄清楚该怎么做。我想我正在初始化一些错误的维度,但我无法打破它。

import numpy as np
import matplotlib as plt

x = np.array([45, 68, 41, 87, 61, 44, 67, 30, 54, 8, 39, 60, 37, 50, 19, 86, 42, 29, 32, 61, 25, 77, 62, 98, 47, 36, 15, 40, 9, 25, 34, 50, 61, 75, 51, 96, 20, 13, 18, 35, 43, 88, 25, 95, 68, 81, 29, 41, 45, 87,45, 68, 41, 87, 61, 44, 67, 30, 54, 8, 39, 60, 37, 50, 19, 86, 42, 29, 32, 61, 25, 77, 62, 98, 47, 36, 15, 40, 9, 25, 34, 50, 61, 75, 51, 96, 20, 13, 18, 35, 43, 88, 25, 95, 68, 81, 29, 41, 45, 87])
len_x = len(x)
mean = np.mean(x)

xup = np.zeros(shape=(1,120))
for i in range(len_x) :
    xup[i] = (x[i] - mean) ** 2

xup_sum = np.sum(xup)
var = xup_sum / len_x
std_dev = var ** 0.5

z = np.zeros(shape = (1,120))
for i in range(len_x) :
    z[i] = (x[i] - mean)/std_dev

print("Mean :", mean)
print("Standard_dev :",std_dev)
print("Variance : ",var)

回答by OnethingSimple

xupis 2 dimensional. So instead of xup[i]you would need xup[0][i]

xup是二维的。所以而不是xup[i]你需要xup[0][i]

Just fix these 2 places:

只需修复这两个地方:

xup = np.zeros(shape=(1,120))
for i in range(len_x) :
    xup[0, i] = (x[i] - mean) ** 2

And then again here:

然后再次在这里:

z = np.zeros(shape = (1,120))
for i in range(len_x) :
    z[0, i] = (x[i] - mean)/std_dev

This would be the file you posted above with the 2 changes:

这将是您在上面发布的带有 2 个更改的文件:

import numpy as np
import matplotlib as plt

x = np.array([45, 68, 41, 87, 61, 44, 67, 30, 54, 8, 39, 60, 37, 50, 19, 86, 42, 29, 32, 61, 25, 77, 62, 98, 47, 36, 15, 40, 9, 25, 34, 50, 61, 75, 51, 96, 20, 13, 18, 35, 43, 88, 25, 95, 68, 81, 29, 41, 45, 87,45, 68, 41, 87, 61, 44, 67, 30, 54, 8, 39, 60, 37, 50, 19, 86, 42, 29, 32, 61, 25, 77, 62, 98, 47, 36, 15, 40, 9, 25, 34, 50, 61, 75, 51, 96, 20, 13, 18, 35, 43, 88, 25, 95, 68, 81, 29, 41, 45, 87])
len_x = len(x)
mean = np.mean(x)

xup = np.zeros(shape=(1,120))
for i in range(len_x) :
    xup[0, i] = (x[i] - mean) ** 2

xup_sum = np.sum(xup)
var = xup_sum / len_x
std_dev = var ** 0.5

z = np.zeros(shape = (1,120))
for i in range(len_x) :
    z[0, i] = (x[i] - mean)/std_dev

print("Mean :", mean)
print("Standard_dev :",std_dev)
print("Variance : ",var)

回答by hpaulj

You really should tell us where the error occurred. But I can guess:

你真的应该告诉我们错误发生在哪里。但我能猜到:

xup = np.zeros(shape=(1,120))
for i in range(len_x) :
    xup[i,:] = (x[i] - mean) ** 2  #<=====

(Similar zloop follows)

(类似的z循环如下)

I added an implied ,:. Your xup[i]is indexing the first dimension. But that is only size 1. As created it's the 2nd dimension that is large. xup[0,i]is the right indexing.

我添加了一个隐含的,:. 您xup[i]正在索引第一个维度。但这只是大小 1。在创建时,它是大的第二维。 xup[0,i]是正确的索引。

Why is xup2d with the (1,120) shape? Why not the same shape as x(which I assume is (120,))? xup = np.zeros(len_x).

为什么xup2d 是 (1,120) 形状?为什么形状不一样x(我假设是 (120,))? xup = np.zeros(len_x).

Better yet use a proper numpyarray calculation:

更好的是使用适当的numpy数组计算:

xup = (x-mean)**2 

However this xuphas the shape (100,), the same as x.

然而,它xup的形状为 (100,),与x.

You are already using np.mean(x)which operates on the whole of x. Operators like -and **do so as well.

您已经在使用np.mean(x)which 对整个x. 运营商喜欢-**这样做很好。

(Earlier I'd suggested using np.zeros_like(x), but then realized that it would create an integer array like x. Assigning float values from the calculation to that would give problems. When doing an assign and fill loop you need to pay attention to both the shape and dtype of target array.)

(此前我已经使用建议np.zeros_like(x),但后来意识到,这将创建一个整数数组一样x。从计算分配浮点值,这将产生问题。如果做一个分配和填充循环中,您需要注意的形状和两个D型目标数组。)