我試圖找到數字列表的最大值列表。當找到最大值時,Python錯誤「無法使用彈性類型執行縮減」
使用Python 2.7空閒,我嘗試這樣做:
import numpy
vectors = [[1, 2, 3], [4,5,6]]
numpyFiles = numpy.array(vectors)
maxRawFreq = numpyFiles.max()
它的工作原理,並maxRawFreq = 6
我使用一個非常類似的代碼,有一個更大的列表,並使用Pydev的(Eclipse中)試過了,但我得到以下錯誤:
cannot perform reduce with flexible type
是什麼意思? (關於這個錯誤的其他SO問題給出了具體的解決方案......)。
我的代碼:
import numpy
with open(inputFile) as f:
vectors = f.readlines()
vectorLength=len(vectors[0])#number of columns (word vector length)
numpyFiles = numpy.array(vectors)
#both these line gave me the same error:
#maxRawFreq = numpyFiles.max()
maxRawFreq = numpy.max(numpyFiles)
我inputFile
包含數字,像這樣:
-1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
-1, 0, 0, 3, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 4, 0, 0,
+1, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 6,
您在字符串,而不是數字閱讀。您需要相應地投入您的輸入。 –
爲什麼不使用'numpy.loadtxt'或'numpy.genfromtxt'從文件加載數據? 'file.readlines'只是返回文件中的行列表。 –