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_blobs(n_samples = 50, centers = 2, random_state = 42)
seaborn.relplot(x = x[:, 0], y = x[:, 1], hue = y)
Out[2]:
<seaborn.axisgrid.FacetGrid at 0x1d8cfac68d0>
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 0x1d8cfbee8d0>
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 0x1d8d1c2ac10>
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 0x1d8d1cb2bd0>
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 0x1d8d1d067d0>
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 0x1d8d1dc67d0>
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 0x1d8d2f5b5d0>
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 0x1d8d3003e90>