(weights)
| 161 | 2017-08-30 |
| 162 | """ |
| 163 | def plotBestFit(weights): |
| 164 | dataMat, labelMat = loadDataSet() #加载数据集 |
| 165 | dataArr = np.array(dataMat) #转换成numpy的array数组 |
| 166 | n = np.shape(dataMat)[0] #数据个数 |
| 167 | xcord1 = []; ycord1 = [] #正样本 |
| 168 | xcord2 = []; ycord2 = [] #负样本 |
| 169 | for i in range(n): #根据数据集标签进行分类 |
| 170 | if int(labelMat[i]) == 1: |
| 171 | xcord1.append(dataArr[i,1]); ycord1.append(dataArr[i,2]) #1为正样本 |
| 172 | else: |
| 173 | xcord2.append(dataArr[i,1]); ycord2.append(dataArr[i,2]) #0为负样本 |
| 174 | fig = plt.figure() |
| 175 | ax = fig.add_subplot(111) #添加subplot |
| 176 | ax.scatter(xcord1, ycord1, s = 20, c = 'red', marker = 's',alpha=.5)#绘制正样本 |
| 177 | ax.scatter(xcord2, ycord2, s = 20, c = 'green',alpha=.5) #绘制负样本 |
| 178 | x = np.arange(-3.0, 3.0, 0.1) |
| 179 | y = (-weights[0] - weights[1] * x) / weights[2] |
| 180 | ax.plot(x, y) |
| 181 | plt.title('BestFit') #绘制title |
| 182 | plt.xlabel('X1'); plt.ylabel('X2') #绘制label |
| 183 | plt.show() |
| 184 | |
| 185 | if __name__ == '__main__': |
| 186 | dataMat, labelMat = loadDataSet() |
no test coverage detected