您的位置:首页 > 编程语言 > Python开发

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

2018-01-17 23:34 561 查看
在机器学习实战一书的第五章中出现了getA()这个函数
logRegres.plotBestFit(weight.getA())
1
当输入下下代码时
logRegres.plotBestFit(weight)
1
2
会出现错误,原因在于下面这一段代码中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)
1
2
3
接下来我们看一下分析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]
1
2
3
4
5
6
输出结果
>>>len(x)
60
>>>len(y1)
1
>>>len(y2)
60
1
2
3
4
5
6
可以看到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.]])
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
从上述结果中可以看书getA()函数与mat()函数的功能相反,是将一个numpy矩阵转换为数组
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  机器学习实战