这里有一段我的脚本。这个应该做的是打开一个矩阵(在文件matrix_seeds_to_all_targets中)并且对每列中的所有元素求和(最后我应该得到一个1xN的数组)。我得到的是一个错误:AttributeError:'list'对象没有属性'sum'。您能否就此给我任何见解?python sum matrix列表
def collapse_probtrack_results(waytotal_file, matrix_file):
with open(waytotal_file) as f:
waytotal = int(f.read())
f = open(wayfile_template + roi + "/matrix_seeds_to_all_targets")
l = [map(int, line.split(',')) for line in f if line.strip() != ""]
collapsed = l.sum(axis=0)/waytotal * 100.
return collapsed
print (collapsed)
感谢您的回答。我试图使用你的建议,但是我得到以下错误: sum = a.sum AttributeError:'list'对象没有属性'sum' – StudentOIST
你不应该得到与上述列表错误。在我的代码中,您必须将数据导入为实际的numpy数组,而不是列表或嵌套列表。我的'矩阵'不像你的原始代码中的列表。您必须使用numpys'fromfile','genfromtext','loadtxt'方法之一加载数据文件,以便在尝试求和之前将其转换为正确的格式。您使用哪个取决于您原始矩阵文件的格式 – Simon