python机器学习实战 getA()函数详解

来源:互联网 发布:北京新华电脑学校java 编辑:程序博客网 时间:2024/06/05 02:56

在机器学习实战一书的第五章中出现了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))          #创建数组>>>temparray([[ 1.],       [ 1.],       [ 1.]])>>>weights = mat(w)         #转换为numpy矩阵>>>weightsmatrix([[ 1.],        [ 1.],        [ 1.]])>>>s = weights.getA()           #将numpy矩阵转换为数组>>>sarray([[ 1.],       [ 1.],       [ 1.]])

从上述结果中可以看书getA()函数与mat()函数的功能相反,是将一个numpy矩阵转换为数组

原创粉丝点击