from sklearn.linear_model import Perceptron
import matplotlib.pyplot as plt
import numpy as np
from itertools import product
data = [[0, 0], [0, 1], [1, 0], [1, 1]]
labels = [0,0,0,1]
classifier = Perceptron(random_state = 20)
classifier.fit(data,labels)
Perceptron(random_state=20)In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook.
Perceptron(random_state=20)
x_values = np.linspace(0, 1, 100)
y_values = np.linspace(0, 1, 100)
point_grid = list(product(x_values, y_values))
distances = classifier.decision_function(point_grid)
abs_distances = [abs(pt) for pt in distances]
distances_matrix = np.reshape(abs_distances, (100,100))
heatmap = plt.pcolormesh(x_values, y_values,distances_matrix)
plt.colorbar(heatmap)
plt.scatter([point[0] for point in data],[point[1] for point in data], c=labels)
plt.show()
tests = classifier.predict([[0.1,0.1],[0.5,0.5],[0.9,0.9],[0.99,0.1]])
print(tests)
[0 0 1 0]
labels = [0,1,1,1]
classifier = Perceptron(random_state = 20)
classifier.fit(data,labels)
Perceptron(random_state=20)In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook.
Perceptron(random_state=20)
x_values = np.linspace(0, 1, 100)
y_values = np.linspace(0, 1, 100)
point_grid = list(product(x_values, y_values))
distances = classifier.decision_function(point_grid)
abs_distances = [abs(pt) for pt in distances]
distances_matrix = np.reshape(abs_distances, (100,100))
heatmap = plt.pcolormesh(x_values, y_values,distances_matrix)
plt.colorbar(heatmap)
plt.scatter([point[0] for point in data],[point[1] for point in data], c=labels)
plt.show()
tests = classifier.predict([[0.1,0.1],[0.5,0.5],[0.9,0.9],[0.99,0.1]])
print(tests)
[0 1 1 1]