Last active
March 24, 2021 02:01
-
-
Save eloquentarduino/99511fabee78b1b60bd7b08fa4ea6e2b to your computer and use it in GitHub Desktop.
This file contains hidden or 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
""" | |
Install the dependencies with | |
pip install matplotlib sklearn mlxtend | |
""" | |
import matplotlib.pyplot as plt | |
from sklearn.decomposition import PCA | |
from mlxtend.plotting import plot_decision_regions | |
from sklearn.datasets import load_iris | |
from sklearn.svm import SVC | |
def plot_boundaries(classifier, X, y, classmap=None): | |
""" | |
classifier: an untrained classifier | |
X: features matrix | |
y: labels vector | |
classmap: a dict where the key is the class index, the value is the class name. | |
For example {0: "setosa", 1: "versicolor", 2: "virginica"} | |
""" | |
labels = None | |
if classmap is not None: | |
labels = classmap.values() | |
# project X into 2d | |
X = PCA(n_components=2).fit_transform(X) | |
plot_decision_regions(X, y.astype(np.uint8), | |
clf=classifier.fit(X, y), | |
legend=1, | |
custom_labels=labels) | |
plt.show() | |
if __name__ == '__main__': | |
"""Plot boundaries of SVM on the Iris dataset""" | |
X, y = load_iris(return_X_y=True) | |
clf = SVC(kernel='rbf') | |
plot_boundaries(clf, X, y) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment