python機器學習實戰 getA()函數詳解


在機器學習實戰一書的第五章中出現了getA()這個函數

logRegres.plotBestFit(weight.getA())

當輸入下下代碼時

logRegres.plotBestFit(weight)

會出現錯誤,原因在於下面這一段代碼中len(x) = 60, 而len(y) = 1

 x = arange(-3.0, 3.0, 0.1)
 y = (-weights[0] - weights[1]*x)/weights[2]
 ax.plot(x, y)

接下來我們看一下分析getA()這個函數的作用。首先看以下代碼

temp = ones((3, 1))         #創建數組
weights = mat(w)            #轉換為numpy矩陣
s = weights.getA()          #將numpy矩陣轉換為數組
x = arange(-3.0, 3.0, 0.1)
y1 = (-weights[0] - weights[1]*x)/weights[2]
y2 = (s[0] - s[1] *x)/s[2]

輸出結果

>>>len(x)
60
>>>len(y1)
1
>>>len(y2)
60

可以看到y1和x的維數不一樣,所以ax.plot(x, y)會出錯

再看看結果

>>>temp = ones((3, 1))          #創建數組
>>>temp
array([[ 1.],
       [ 1.],
       [ 1.]])
>>>weights = mat(w)         #轉換為numpy矩陣
>>>weights
matrix([[ 1.],
        [ 1.],
        [ 1.]])
>>>s = weights.getA()           #將numpy矩陣轉換為數組
>>>s
array([[ 1.],
       [ 1.],
       [ 1.]])

從上述結果中可以看書getA()函數與mat()函數的功能相反,是將一個numpy矩陣轉換為數組

 

 

 

 

 
       


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2026 CODEPRJ.COM