gpt4 book ai didi

python - 索引错误 : too many indices for array for an array that is definitely as big

转载 作者:行者123 更新时间:2023-11-28 16:26:47 25 4
gpt4 key购买 nike

我正在尝试通过拍摄更新情节的 png 图像并将它们拼接在一起来制作电影。共有三个变量:degrees、ksB 和 mp。只有 mp 改变每一帧;其他两个是不变的。 mp所有时间的数据存储在X中。这是代码的相关部分:

def plot(fname, haveMLPY=False):
# Load data from .npz file.
data = np.load(fname)
X = data["X"]
T = data["T"]
N = X.shape[1]
A = data["vipWeights"]
degrees = A.sum(1)
ksB = data["ksB"]

# Initialize a figure.
figure = plt.figure()

# Generate a plottable axis as the first subplot in 1 rows and 1 columns.
axis = figure.add_subplot(1,1,1)

# MP is the first (0th) variable. Plot one trajectory for each cell over time.
axis.plot(T, X[:,:,0], color="black")

# Decorate the plot.
axis.set_xlabel("time [hours]")
axis.set_ylabel("MP [nM]")
axis.set_title("PER mRNA concentration across all %d cells" % N)
firstInd = int(T.size / 2)

if haveMLPY:
import circadian.analysis
# Generate a and plot Signal object, which encapsulates wavelet analysis.
signal = circadian.analysis.Signal(X[firstInd:, 0, 0], T[firstInd:])
signal.showSpectrum(show=False)


files=[]
# filename for the name of the resulting movie
filename = 'animation'
mp = X[10**4-1,:,0]
from mpl_toolkits.mplot3d import Axes3D
for i in range(10**4):
print i
mp = X[i,:,0]
data2 = np.c_[degrees, ksB, mp]

# Find best fit surface for data2
# regular grid covering the domain of the data
mn = np.min(data2, axis=0)
mx = np.max(data2, axis=0)
X,Y = np.meshgrid(np.linspace(mn[0], mx[0], 20), np.linspace(mn[1], mx[1], 20))
XX = X.flatten()
YY = Y.flatten()
order = 2 # 1: linear, 2: quadratic
if order == 1:
# best-fit linear plane
A = np.c_[data2[:,0], data2[:,1], np.ones(data2.shape[0])]
C,_,_,_ = scipy.linalg.lstsq(A, data2[:,2]) # coefficients

# evaluate it on grid
Z = C[0]*X + C[1]*Y + C[2]

# or expressed using matrix/vector product
#Z = np.dot(np.c_[XX, YY, np.ones(XX.shape)], C).reshape(X.shape)

elif order == 2:
# best-fit quadratic curve
A = np.c_[np.ones(data2.shape[0]), data2[:,:2], np.prod(data2[:,:2], axis=1), data2[:,:2]**2]
C,_,_,_ = scipy.linalg.lstsq(A, data2[:,2])

# evaluate it on a grid
Z = np.dot(np.c_[np.ones(XX.shape), XX, YY, XX*YY, XX**2, YY**2], C).reshape(X.shape)

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.plot_surface(X, Y, Z, rstride=1, cstride=1, alpha=0.2)
ax.scatter(degrees, ksB, mp)
ax.set_xlabel('degrees')
ax.set_ylabel('ksB')
ax.set_zlabel('mp')
# form a filename
fname2 = '_tmp%03d.png'%i
# save the frame
savefig(fname2)
# append the filename to the list
files.append(fname2)
# call mencoder
os.system("mencoder 'mf://_tmp*.png' -mf type=png:fps=10 -ovc lavc -lavcopts vcodec=wmv2 -oac copy -o " + filename + ".mpg")
# cleanup
for fname2 in files: os.remove(fname2)

基本上,所有数据都存储在X中。格式X[i, i, i]表示X[时间,神经元,数据类型]。每次通过循环,我都想更新时间,但仍然为所有神经元绘制 mp(第 0 个变量)。

当我运行这段代码时,我得到“IndexError: too many indices for array”。我要求它打印 i 以查看代码何时出错。当 i = 1 时出现错误,这意味着代码循环一次但第二次出现错误。

但是,我有 10^4 个时间步长的数据。您可以在提供的代码的第一行中看到,我成功访问了 X[10**4-1, :, 0] 。这就是为什么 X[1,:,0] 会超出范围让我感到困惑的原因。如果有人能解释为什么/帮助我解决这个问题,那就太好了。

回溯错误是

Traceback (most recent call last):
File"/Users/angadanand/Documents/LiClipseWorkspace/Circadian/scripts /runMeNets.py", line 196, in module
plot(fname)
File"/Users/angadanand/Documents/LiClipseWorkspace/Circadian/scripts /runMeNets.py", line 142, in plot
mp = X[i,:,0]
IndexError: too many indices for array

谢谢!

最佳答案

您的问题是您在循环中覆盖了 X:

X,Y = np.meshgrid(np.linspace(mn[0], mx[0], 20), np.linspace(mn[1], mx[1], 20))

因此之后它将具有另一种形状并包含不同的数据。我建议将第二个 X 更改为 x_grid 并检查您在哪里需要这个“其他”X 以及原始位置。

例如:

X_grid, Y_grid = np.meshgrid(np.linspace(mn[0], mx[0], 20), np.linspace(mn[1], mx[1], 20))

关于python - 索引错误 : too many indices for array for an array that is definitely as big,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35990106/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com