| 209 | 2017-09-23 |
| 210 | """ |
| 211 | def showClassifer(dataMat, w, b): |
| 212 | #绘制样本点 |
| 213 | data_plus = [] #正样本 |
| 214 | data_minus = [] #负样本 |
| 215 | for i in range(len(dataMat)): |
| 216 | if labelMat[i] > 0: |
| 217 | data_plus.append(dataMat[i]) |
| 218 | else: |
| 219 | data_minus.append(dataMat[i]) |
| 220 | data_plus_np = np.array(data_plus) #转换为numpy矩阵 |
| 221 | data_minus_np = np.array(data_minus) #转换为numpy矩阵 |
| 222 | plt.scatter(np.transpose(data_plus_np)[0], np.transpose(data_plus_np)[1], s=30, alpha=0.7) #正样本散点图 |
| 223 | plt.scatter(np.transpose(data_minus_np)[0], np.transpose(data_minus_np)[1], s=30, alpha=0.7) #负样本散点图 |
| 224 | #绘制直线 |
| 225 | x1 = max(dataMat)[0] |
| 226 | x2 = min(dataMat)[0] |
| 227 | a1, a2 = w |
| 228 | b = float(b) |
| 229 | a1 = float(a1[0]) |
| 230 | a2 = float(a2[0]) |
| 231 | y1, y2 = (-b- a1*x1)/a2, (-b - a1*x2)/a2 |
| 232 | plt.plot([x1, x2], [y1, y2]) |
| 233 | #找出支持向量点 |
| 234 | for i, alpha in enumerate(alphas): |
| 235 | if abs(alpha) > 0: |
| 236 | x, y = dataMat[i] |
| 237 | plt.scatter([x], [y], s=150, c='none', alpha=0.7, linewidth=1.5, edgecolor='red') |
| 238 | plt.show() |
| 239 | |
| 240 | |
| 241 | """ |