函数说明:绘制多条局部加权回归曲线 Parameters: 无 Returns: 无 Website: http://www.cuijiahua.com/ Modify: 2017-11-15
()
| 102 | plt.show() |
| 103 | |
| 104 | def plotlwlrRegression(): |
| 105 | """ |
| 106 | 函数说明:绘制多条局部加权回归曲线 |
| 107 | Parameters: |
| 108 | 无 |
| 109 | Returns: |
| 110 | 无 |
| 111 | Website: |
| 112 | http://www.cuijiahua.com/ |
| 113 | Modify: |
| 114 | 2017-11-15 |
| 115 | """ |
| 116 | font = FontProperties(fname=r"c:\windows\fonts\simsun.ttc", size=14) |
| 117 | xArr, yArr = loadDataSet('ex0.txt') #加载数据集 |
| 118 | yHat_1 = lwlrTest(xArr, xArr, yArr, 1.0) #根据局部加权线性回归计算yHat |
| 119 | yHat_2 = lwlrTest(xArr, xArr, yArr, 0.01) #根据局部加权线性回归计算yHat |
| 120 | yHat_3 = lwlrTest(xArr, xArr, yArr, 0.003) #根据局部加权线性回归计算yHat |
| 121 | xMat = np.mat(xArr) #创建xMat矩阵 |
| 122 | yMat = np.mat(yArr) #创建yMat矩阵 |
| 123 | srtInd = xMat[:, 1].argsort(0) #排序,返回索引值 |
| 124 | xSort = xMat[srtInd][:,0,:] |
| 125 | fig, axs = plt.subplots(nrows=3, ncols=1,sharex=False, sharey=False, figsize=(10,8)) |
| 126 | |
| 127 | axs[0].plot(xSort[:, 1], yHat_1[srtInd], c = 'red') #绘制回归曲线 |
| 128 | axs[1].plot(xSort[:, 1], yHat_2[srtInd], c = 'red') #绘制回归曲线 |
| 129 | axs[2].plot(xSort[:, 1], yHat_3[srtInd], c = 'red') #绘制回归曲线 |
| 130 | axs[0].scatter(xMat[:,1].flatten().A[0], yMat.flatten().A[0], s = 20, c = 'blue', alpha = .5) #绘制样本点 |
| 131 | axs[1].scatter(xMat[:,1].flatten().A[0], yMat.flatten().A[0], s = 20, c = 'blue', alpha = .5) #绘制样本点 |
| 132 | axs[2].scatter(xMat[:,1].flatten().A[0], yMat.flatten().A[0], s = 20, c = 'blue', alpha = .5) #绘制样本点 |
| 133 | |
| 134 | #设置标题,x轴label,y轴label |
| 135 | axs0_title_text = axs[0].set_title(u'局部加权回归曲线,k=1.0',FontProperties=font) |
| 136 | axs1_title_text = axs[1].set_title(u'局部加权回归曲线,k=0.01',FontProperties=font) |
| 137 | axs2_title_text = axs[2].set_title(u'局部加权回归曲线,k=0.003',FontProperties=font) |
| 138 | |
| 139 | plt.setp(axs0_title_text, size=8, weight='bold', color='red') |
| 140 | plt.setp(axs1_title_text, size=8, weight='bold', color='red') |
| 141 | plt.setp(axs2_title_text, size=8, weight='bold', color='red') |
| 142 | |
| 143 | plt.xlabel('X') |
| 144 | plt.show() |
| 145 | |
| 146 | def lwlr(testPoint, xArr, yArr, k = 1.0): |
| 147 | """ |
no test coverage detected