機器學習實戰———k均值聚類 算法

 

問題:關於第九章list()添加的問題

fltLine = list(map(float,curLine))
fltLine = map(float,curLine)

二者的區別在於

加list()輸出爲數

[1.658985, 4.285136]
[-3.453687, 3.424321]
[4.838138, -1.151539]
[-5.379713, -3.362104]
[0.972564, 2.924086]
[-3.567919, 1.531611]
[0.450614, -3.302219]
[-3.487105, -1.724432]

不加list輸出爲:

<map object at 0x000001B875605668>
<map object at 0x000001B874D316D8>
<map object at 0x000001B875605710>
<map object at 0x000001B875605C88>
<map object at 0x000001B875605CF8>
<map object at 0x000001B875605D68>
<map object at 0x000001B875605DD8>
<map object at 0x000001B875605E48>
<map object at 0x000001B875605EB8>
<map object at 0x000001B875605F28>
<map object at 0x000001B8756056D8>

同樣

對於readline()和readlines()的區別在於:

readline()讀取到第一行,readlines()讀取所有行。

def loadDataSet(fileName):
    dataMat = []
    fr = open(fileName)
##    print(fr.readlines())
    for line in fr.readlines():
        curLine = line.strip().split('\t')
        fltLine = list(map(float,curLine))
##        fltLine = map(float,curLine) #輸出map格式
##        print(fltLine)
        
        dataMat.append(fltLine)
    return dataMat

問題:ValueError: matrix must be 2-dimensional

加上           .tolist()[0]  即可

centList[bestCentToSplit] = bestNewCents[0,:]
        centList.append(bestNewCents[1,:]


#加上
centList[bestCentToSplit] = bestNewCents[0,:].tolist()[0]
        centList.append(bestNewCents[1,:].tolist()[0])

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章