[英]IndexError: list index out of range when creating 2D array
我试图获得一个大小为 235x20 的二维数组,以便对于 235 个 ROI 中的每一个,我根据存储在find_bootstrap_indices
中的索引提取 20 个元素并将它们保存在extract_bootstrap_timepoint
中。 现在,我想将每个 ROI 的值及其对应的 20 个元素存储在一个数组中。 我试图通过这条线new[roi][timepoint] = extract_bootstrap[timepoint]
来做到这一点,但我遇到了索引超出范围错误。 任何有关如何解决此问题的帮助将不胜感激。 谢谢。
new = []
for roi in range(0, rsfMRI_timeseries_2d.shape[0]):
extract_bootstrap_timepoint = np.take(rsfMRI_timeseries_2d[roi, :], find_bootstrap_indices)
for timepoint in range(0, len(extract_bootstrap_timepoint)):
new[roi][timepoint] = extract_bootstrap_timepoint[timepoint]
您需要在分配给它之前创建 new[roi][] ,即:
new = []
for roi in range(0, rsfMRI_timeseries_2d.shape[0]):
extract_bootstrap_timepoint = np.take(rsfMRI_timeseries_2d[roi, :], find_bootstrap_indices)
new[roi] = []
for timepoint in range(0, len(extract_bootstrap_timepoint)):
new[roi][timepoint] = extract_bootstrap_timepoint[timepoint]
这将创建元素列表并操作值
new = [0]*rsfMRI_timeseries_2d.shape[0]
for roi in range(0, rsfMRI_timeseries_2d.shape[0]):
xtract_bootstrap_timepoint = np.take(rsfMRI_timeseries_2d[roi, :], find_bootstrap_indices)
new[roi]=[0]*len(extract_bootstrap_timepoint)
for timepoint in range(0, len(extract_bootstrap_timepoint)):
new[roi][timepoint] = extract_bootstrap_timepoint[timepoint]
另一种方法是 append 值。
new = []
for roi in range(0, rsfMRI_timeseries_2d.shape[0]):
xtract_bootstrap_timepoint = np.take(rsfMRI_timeseries_2d[roi, :], find_bootstrap_indices)
l=list()
for timepoint in range(0, len(extract_bootstrap_timepoint)):
l.append(extract_bootstrap_timepoint[timepoint])
new.append(l)
我认为你需要这样做:
new = []
for roi in range(0, rsfMRI_timeseries_2d.shape[0]):
extract_bootstrap_timepoint = np.take(rsfMRI_timeseries_2d[roi, :], find_bootstrap_indices)
new[roi] = extract_bootstrap_timepoint[:] # Copy data.
其他答案已经涵盖了如何修复错误,但不要提到有更简单的方法来完成整个事情。
概括地说,我可以想到人们使用 Numpy 的几个不同原因:
您错过了第 1 点和第 2 点; 您正在使用 Numpy,但您没有充分利用它,因此您的代码没有尽可能快速和简洁。
首先,一些非 Numpy 的建议:
range()
的三参数形式,否则您可以省略开头 position 如果它是 0 - 即。 range(N)
与range(0, N)
) 做同样的事情for i in range(len(items)): item = items[i]
,你应该for item in items:
做。 如果还需要索引,可以说for i, item in enumerate(items):
extract_bootstrap_timepoint
到ebt
)
new[roi] = ebt
一样分配它new[roi] = list(ebt)
或new[roi] = ebt.tolist()
将其转换(后者仅适用于数组)new[roi] = ebt.copy()
或new[roi] = ebt[:]
(后者不适用于数组)new[roi][:len(ebt)] = ebt
这样的切片分配因此,应用该建议,您的代码将简化为:
new = []
for row in rsfMRI_timeseries_2d:
new.append(np.take(row, find_bootstrap_indices).tolist())
现在,进入更具体的 Numpy 建议:
np.take(arr, indices)
精美的索引; 具体来说,如果arr
是 Numpy 数组并且indices
是 Python 列表,则相当于arr[indices]
由于您只是对rsfMRI_timeseries_2d
中的每一行执行相同的操作,并且np.take
调用也可以通过索引替换,因此整个循环可以替换为:
new = rsfMRI_timeseries_2d[:,find_bootstrap_indices].tolist()
Although, since you're using Numpy, you'll probably want to be working with Numpy arrays instead of nested Python lists - If so, the whole thing becomes just:
new = rsfMRI_timeseries_2d[:,find_bootstrap_indices]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.