机器学习实战第五章LOGISTIC回归中出现weight.getA()

描述:在pycharm控制台中输入logRegres.plotBestFit(weights.getA()),为什么需要加getA()呢????不加会报错又是为什么呢????

对于x=[1.1,1.2,1.3,1.4,1.5,1.6,1.7,1.8]     y=[3.3,3.6,3.9,4.2,4.5,4.8,5.1,5.4] 来说 是可以使用plot绘图的 ,因为他们的长度是一致的。

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

对于以上代码我们会发现,len(x) = 60 而len(y)=1 ,他是一个[[1 6 7 6 9 ...........41 41 2 32 55 ]]这种形式,因为之前定义的weights 是就是这种形式,因为长度对应不上,所以如果不加getA()会出现报错 。只有x y长度一样了才不会报错。而getA()的目的就是可以把numpy矩阵转换成numpy数组既[2 3 3 6 7 5 56 ]这种形式,这样长度会变长,而不是1。


from numpy import *

example = ones((3,1))
weight = mat(example)
weights=weight.getA()

print(example)
print(type(example))
print(len(example))

print(weight)
print(type(weight))
print(len(weight))

print(weights)
print(type(weights))
print(len(weights))
x = arange(-3.0, 3.0, 0.1)
y1 = (-weight[0] - weight[1]*x)/weight[2]
y2 = (weights[0] - weights[1] *x)/weights[2]

print(y1)
print(y2)

看下结果:

[[ 1.]
 [ 1.]
 [ 1.]]
<class 'numpy.ndarray'>
3
[[ 1.]
 [ 1.]
 [ 1.]]
<class 'numpy.matrixlib.defmatrix.matrix'>
3
[[ 1.]
 [ 1.]
 [ 1.]]
<class 'numpy.ndarray'>
3
[[  2.00000000e+00   1.90000000e+00   1.80000000e+00   1.70000000e+00
    1.60000000e+00   1.50000000e+00   1.40000000e+00   1.30000000e+00
    1.20000000e+00   1.10000000e+00   1.00000000e+00   9.00000000e-01
    8.00000000e-01   7.00000000e-01   6.00000000e-01   5.00000000e-01
    4.00000000e-01   3.00000000e-01   2.00000000e-01   1.00000000e-01
   -1.77635684e-15  -1.00000000e-01  -2.00000000e-01  -3.00000000e-01
   -4.00000000e-01  -5.00000000e-01  -6.00000000e-01  -7.00000000e-01
   -8.00000000e-01  -9.00000000e-01  -1.00000000e+00  -1.10000000e+00
   -1.20000000e+00  -1.30000000e+00  -1.40000000e+00  -1.50000000e+00
   -1.60000000e+00  -1.70000000e+00  -1.80000000e+00  -1.90000000e+00
   -2.00000000e+00  -2.10000000e+00  -2.20000000e+00  -2.30000000e+00
   -2.40000000e+00  -2.50000000e+00  -2.60000000e+00  -2.70000000e+00
   -2.80000000e+00  -2.90000000e+00  -3.00000000e+00  -3.10000000e+00
   -3.20000000e+00  -3.30000000e+00  -3.40000000e+00  -3.50000000e+00
   -3.60000000e+00  -3.70000000e+00  -3.80000000e+00  -3.90000000e+00]]
[  4.00000000e+00   3.90000000e+00   3.80000000e+00   3.70000000e+00
   3.60000000e+00   3.50000000e+00   3.40000000e+00   3.30000000e+00
   3.20000000e+00   3.10000000e+00   3.00000000e+00   2.90000000e+00
   2.80000000e+00   2.70000000e+00   2.60000000e+00   2.50000000e+00
   2.40000000e+00   2.30000000e+00   2.20000000e+00   2.10000000e+00
   2.00000000e+00   1.90000000e+00   1.80000000e+00   1.70000000e+00
   1.60000000e+00   1.50000000e+00   1.40000000e+00   1.30000000e+00
   1.20000000e+00   1.10000000e+00   1.00000000e+00   9.00000000e-01
   8.00000000e-01   7.00000000e-01   6.00000000e-01   5.00000000e-01
   4.00000000e-01   3.00000000e-01   2.00000000e-01   1.00000000e-01
  -3.55271368e-15  -1.00000000e-01  -2.00000000e-01  -3.00000000e-01
  -4.00000000e-01  -5.00000000e-01  -6.00000000e-01  -7.00000000e-01
  -8.00000000e-01  -9.00000000e-01  -1.00000000e+00  -1.10000000e+00
  -1.20000000e+00  -1.30000000e+00  -1.40000000e+00  -1.50000000e+00
  -1.60000000e+00  -1.70000000e+00  -1.80000000e+00  -1.90000000e+00]

我们会发现:如果weight用numpy矩阵的话结果是一个长度为1的矩阵,所以用数组,既:去掉一层[]!!


猜你喜欢

转载自blog.csdn.net/weixin_42127577/article/details/80662883