函数说明:绘制回归曲线和数据点 Parameters: 无 Returns: 无 Website: http://www.cuijiahua.com/ Modify: 2017-11-12
()
| 75 | plt.show() |
| 76 | |
| 77 | def plotRegression(): |
| 78 | """ |
| 79 | 函数说明:绘制回归曲线和数据点 |
| 80 | Parameters: |
| 81 | 无 |
| 82 | Returns: |
| 83 | 无 |
| 84 | Website: |
| 85 | http://www.cuijiahua.com/ |
| 86 | Modify: |
| 87 | 2017-11-12 |
| 88 | """ |
| 89 | xArr, yArr = loadDataSet('ex0.txt') #加载数据集 |
| 90 | ws = standRegres(xArr, yArr) #计算回归系数 |
| 91 | xMat = np.mat(xArr) #创建xMat矩阵 |
| 92 | yMat = np.mat(yArr) #创建yMat矩阵 |
| 93 | xCopy = xMat.copy() #深拷贝xMat矩阵 |
| 94 | xCopy.sort(0) #排序 |
| 95 | yHat = xCopy * ws #计算对应的y值 |
| 96 | fig = plt.figure() |
| 97 | ax = fig.add_subplot(111) #添加subplot |
| 98 | ax.plot(xCopy[:, 1], yHat, c = 'red') #绘制回归曲线 |
| 99 | ax.scatter(xMat[:,1].flatten().A[0], yMat.flatten().A[0], s = 20, c = 'blue',alpha = .5) #绘制样本点 |
| 100 | plt.title('DataSet') #绘制title |
| 101 | plt.xlabel('X') |
| 102 | plt.show() |
| 103 | |
| 104 | def plotlwlrRegression(): |
| 105 | """ |
nothing calls this directly
no test coverage detected