我不知道为什么我会收到索引错误。我对Python很新,因此无法弄清楚该怎么做。我认为我正在初始化一些错误的维度,但我无法打破它。索引1超出大小为1的轴0的边界
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)
为什么你将'xup'设置为形状(1,120)的二维数组?尝试使它成为一维:'xup = np.zeros(shape = 120)' –