在機器學習實戰一書的第五章中出現了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矩陣轉換為數組
