這裏有一段我的腳本。這個應該做的是打開一個矩陣(在文件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