機器學習實戰——預測數值型資料:迴歸 實現記錄
阿新 • • 發佈:2018-11-09
關於利用資料集繪圖建立模型
>>> 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