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矩阵转换为数组


原文地址:https://www.cnblogs.com/Aaron12/p/8986462.html