我正在尝试使用np.savetxt将变量的结果保存在.txt文件中。代码非常简单。
for n in range(2000,11000,2000):
avdist = [0]*(np.max(idx)+1) #average distance of the vectors belonging to their corresponding cluster
avdist1 = [0]*(np.max(idx)+1)
for i in range(0, np.max(idx)+1):
avdist[i] = np.mean([np.linalg.norm(e[x]-centroids[i]) for x in range(len(e)) if idx[x] == i])
avdist1[i] = np.mean([np.linalg.norm(e[x]-centroids[i]) for x in range(len(e)) if idx[x] != i])
np.savetxt('avdist{0}.txt'.format(n), avdist)
但我得到一个IndexError:元组索引超出范围。
为什么会这样?我期待结果是avdist2000,avdist4000等。
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/Cellar/python/2.7.8/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/numpy/lib/npyio.py", line 1034, in savetxt
ncol = X.shape[1]
IndexError: tuple index out of range