InĀ [1]:
# Code attribution: Yiyin Shen, Tyler Caraza-Harter
# Imports
import sklearn
import sklearn.linear_model
import sklearn.svm
import sklearn.neighbors
import sklearn.tree
import sklearn.naive_bayes
import sklearn.datasets
import sklearn.inspection
import numpy
import seaborn
InĀ [2]:
# Create a random dataset with two blobs
x, y = sklearn.datasets.make_moons(n_samples = 50, noise = 0.25, random_state = 42)
seaborn.relplot(x = x[:, 0], y = x[:, 1], hue = y)
Out[2]:
<seaborn.axisgrid.FacetGrid at 0x247c489ec90>
InĀ [3]:
# Perceptron
lp = sklearn.linear_model.Perceptron()
lp.fit(x, y)
plot = sklearn.inspection.DecisionBoundaryDisplay.from_estimator(lp, x, response_method = "predict")
plot.ax_.scatter(x[:, 0], x[:, 1], c = y, edgecolor = "black")
Out[3]:
<matplotlib.collections.PathCollection at 0x24788781710>
InĀ [4]:
# Logistic Regression
lr = sklearn.linear_model.LogisticRegression()
lr.fit(x, y)
plot = sklearn.inspection.DecisionBoundaryDisplay.from_estimator(lr, x, response_method = "predict")
plot.ax_.scatter(x[:, 0], x[:, 1], c = y, edgecolor = "black")
Out[4]:
<matplotlib.collections.PathCollection at 0x24788811710>
InĀ [5]:
# Logistic Regression with activation probabilities
lrp = sklearn.linear_model.LogisticRegression()
lrp.fit(x, y)
plot = sklearn.inspection.DecisionBoundaryDisplay.from_estimator(lrp, x, response_method = "predict_proba")
plot.ax_.scatter(x[:, 0], x[:, 1], c = y, edgecolor = "black")
Out[5]:
<matplotlib.collections.PathCollection at 0x24788837050>
InĀ [6]:
# Support Vector Machine
svm = sklearn.svm.LinearSVC(dual = False)
svm.fit(x, y)
plot = sklearn.inspection.DecisionBoundaryDisplay.from_estimator(svm, x, response_method = "predict")
plot.ax_.scatter(x[:, 0], x[:, 1], c = y, edgecolor = "black")
Out[6]:
<matplotlib.collections.PathCollection at 0x247888b3410>
InĀ [7]:
# Nearest Neighbors
knn = sklearn.neighbors.KNeighborsClassifier()
knn.fit(x, y)
plot = sklearn.inspection.DecisionBoundaryDisplay.from_estimator(knn, x, response_method = "predict")
plot.ax_.scatter(x[:, 0], x[:, 1], c = y, edgecolor = "black")
Out[7]:
<matplotlib.collections.PathCollection at 0x24789992d50>
InĀ [8]:
# Decision Trees (not linear in general)
dt = sklearn.tree.DecisionTreeClassifier()
dt.fit(x, y)
plot = sklearn.inspection.DecisionBoundaryDisplay.from_estimator(dt, x, response_method = "predict")
plot.ax_.scatter(x[:, 0], x[:, 1], c = y, edgecolor = "black")
Out[8]:
<matplotlib.collections.PathCollection at 0x247899db5d0>
InĀ [9]:
# Naive Bayes (not linear in general)
nb = sklearn.naive_bayes.GaussianNB()
nb.fit(x, y)
plot = sklearn.inspection.DecisionBoundaryDisplay.from_estimator(nb, x, response_method = "predict")
plot.ax_.scatter(x[:, 0], x[:, 1], c = y, edgecolor = "black")
Out[9]:
<matplotlib.collections.PathCollection at 0x24789a53410>