我是一名使用python 3的初学者
我需要在一系列txt文件中减去两个不同的矩阵,并在一个txt文件中得到所有结果。代码工作,直到我要求打印(并且它确实),但当我想保存“ncol = X.shape [1] indecerror:元组索引超出范围”时,我不断收到错误消息“
这是代码
import sys, os
import numpy as np
from PIL import Image
newpath = "C:/Users/.../PycharmProjects/.../output/differenza"
os.makedirs(newpath)
for txt in range(162, 167):
#fn = "C:/Users/.../PycharmProjects/.../output" + str(txt).zfill(0) + ".txt"
v1 = np.loadtxt("C:/Users/.../PycharmProjects/.../output/result_628.txt")
v2 = np.loadtxt("C:/Users/.../PycharmProjects/.../output/result_" + str(txt).zfill(1) + ".txt")
v3 = v1 - v2
picture = np.sum(v3)
print("picture", picture)
outfn = newpath + "/result_" + str(txt) + ".txt"
np.savetxt("outfn", picture, fmt='%1.3f')
答案 0 :(得分:0)
一起去:
import sys, os
import numpy as np
from PIL import Image
newpath = "C:/Users/.../PycharmProjects/.../output/differenza"
os.makedirs(newpath)
final = np.array()
for txt in range(162, 167):
#fn = "C:/Users/.../PycharmProjects/.../output" + str(txt).zfill(0) + ".txt"
v1 = np.loadtxt("C:/Users/.../PycharmProjects/.../output/result_628.txt")
v2 = np.loadtxt("C:/Users/.../PycharmProjects/.../output/result_" + str(txt).zfill(1) + ".txt")
v3 = v1 - v2
picture = np.sum(v3)
print("picture", picture)
np.append(final, picture)
np.savetxt("outfn.txt", final, fmt='%1.3f')
答案 1 :(得分:0)
你的v3可能只是一个标量值(由numpy.sum()返回),而numpy.savetxt()需要一个array_like对象。
您可以np.savetxt("outfn", [picture,], fmt='%1.3f')
或zipa建议。但是你确定要用一个标量值来编写多个文件吗?