Created
February 28, 2018 13:05
-
-
Save barusan/b24180b48f9d941f8fcd20f660479e8f to your computer and use it in GitHub Desktop.
2-class 2-D SVM sample with iris dataset
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
#-*- coding: utf-8 -*- | |
import numpy as np | |
import matplotlib.pyplot as plt | |
from sklearn.datasets import load_iris | |
from sklearn.svm import LinearSVC | |
iris = load_iris() | |
mask = iris.target != 2 | |
X = iris.data[mask][:,[1,0]] | |
Y = iris.target[mask] | |
xmin, ymin = np.min(X, axis=0) - 0.1 | |
xmax, ymax = np.max(X, axis=0) + 0.1 | |
clf = LinearSVC(loss="hinge", C=100000) | |
clf.fit(X, Y) | |
a, b = clf.coef_[0] | |
c = clf.intercept_ | |
plt.plot(X[Y==0,0], X[Y==0,1], "b_") | |
plt.plot(X[Y==1,0], X[Y==1,1], "r+") | |
x = np.linspace(xmin, xmax, 1000) | |
for d in [-1, 0, 1]: | |
y = -(a*x+c+d)/b | |
plt.plot(x, y, "g-" if d == 0 else "g--") | |
plt.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment