()
| 60 | print("SVM classification accuracy", accuracy(y_test, predictions)) |
| 61 | |
| 62 | def visualize_svm(): |
| 63 | def get_hyperplane_value(x, w, b, offset): |
| 64 | return (-w[0] * x + b + offset) / w[1] |
| 65 | |
| 66 | fig = plt.figure() |
| 67 | ax = fig.add_subplot(1, 1, 1) |
| 68 | plt.scatter(X[:, 0], X[:, 1], marker="o", c=y) |
| 69 | |
| 70 | x0_1 = np.amin(X[:, 0]) |
| 71 | x0_2 = np.amax(X[:, 0]) |
| 72 | |
| 73 | x1_1 = get_hyperplane_value(x0_1, clf.w, clf.b, 0) |
| 74 | x1_2 = get_hyperplane_value(x0_2, clf.w, clf.b, 0) |
| 75 | |
| 76 | x1_1_m = get_hyperplane_value(x0_1, clf.w, clf.b, -1) |
| 77 | x1_2_m = get_hyperplane_value(x0_2, clf.w, clf.b, -1) |
| 78 | |
| 79 | x1_1_p = get_hyperplane_value(x0_1, clf.w, clf.b, 1) |
| 80 | x1_2_p = get_hyperplane_value(x0_2, clf.w, clf.b, 1) |
| 81 | |
| 82 | ax.plot([x0_1, x0_2], [x1_1, x1_2], "y--") |
| 83 | ax.plot([x0_1, x0_2], [x1_1_m, x1_2_m], "k") |
| 84 | ax.plot([x0_1, x0_2], [x1_1_p, x1_2_p], "k") |
| 85 | |
| 86 | x1_min = np.amin(X[:, 1]) |
| 87 | x1_max = np.amax(X[:, 1]) |
| 88 | ax.set_ylim([x1_min - 3, x1_max + 3]) |
| 89 | |
| 90 | plt.show() |
| 91 | |
| 92 | visualize_svm() |
no test coverage detected