机器学习实战——预测数值型数据:回归 实现记录

关于利用数据集绘图建立模型


>>> import regression
>>> xArr, yArr= regression.loadDataSet('ex0.txt')
>>> ws= regression.standRegres(xArr,yArr)
>>> xMat =mat(xArr)
>>> yMat = mat(yArr)
>>> yHat = xMat*ws
>>> import matplotlib.pyplot as plt
>>> fig = plt.figure()
>>> ax = fig.add_subplot(111)
>>> ax.scatter(xMat[:,1].flatten().A[0],yMat.T[:,0].flatten().A[0])
<matplotlib.collections.PathCollection object at 0x0000023DD0AEFEF0>
>>> xCopy = xMat.copy()
>>> xCopy.sort(0)
>>> yHat  = xCopy *ws
>>> ax.plot(xCopy[:,1],yHat)
[<matplotlib.lines.Line2D object at 0x0000023DD08DD470>]
>>> plt.show()

flatten()函数用法

flatten是numpy.ndarray.flatten的一个函数,即返回一个折叠成一维的数组。但是该函数只能适用于numpy对象,即array或者mat,普通的list列表是不行的。 

a是个矩阵或者数组,a.flatten()就是把a降到一维,默认是按横的方向降 
那么a.flatten().A又是什么呢? 其实这是因为此时的a是个矩阵,降维后还是个矩阵,矩阵.A(等效于矩阵.getA())变成了数组

问题:关于矩阵套矩阵(未解决)

>>> x = matrix([[1,0],[0,0]])
>>> y = matrix([[2,2],[1,1]])
>>> z = y[x]
>>> z
matrix([[[1, 1],
         [2, 2]],

        [[2, 2],
         [2, 2]]])
>>> x = matrix([[1,2],[3,4]])
>>> z
matrix([[[1, 1],
         [2, 2]],

        [[2, 2],
         [2, 2]]])
>>> y = matrix([[2,2],[1,1]])
>>> z
matrix([[[1, 1],
         [2, 2]],

        [[2, 2],
         [2, 2]]])
>>> y = matrix([[1,1],[2,2]])
>>> 
>>> z
matrix([[[1, 1],
         [2, 2]],

        [[2, 2],
         [2, 2]]])
>>> y = matrix([[1,2],[2,1]])
>>> z
matrix([[[1, 1],
         [2, 2]],

        [[2, 2],
         [2, 2]]])
>>> z = x[y]





>>> x = matrix([[1,0],[0,0]])
>>> y = matrix([[2,2],[1,1]])
>>> z = y[x]
>>> z = x[y]
Traceback (most recent call last):
  File "<pyshell#129>", line 1, in <module>
    z = x[y]
  File "C:\Users\34856\AppData\Local\Programs\Python\Python36\lib\site-packages\numpy\matrixlib\defmatrix.py", line 190, in __getitem__
    out = N.ndarray.__getitem__(self, index)
IndexError: index 2 is out of bounds for axis 0 with size 2

猜你喜欢

转载自blog.csdn.net/ihiefoxboq/article/details/83049266