Perceptron Project: AND Heatmap not Displaying Correctly

I recently completed the Perceptron Skill Path Project and the heat map of the separator of the AND gate displays differently than that of the ‘get help’ video. I have reviewed the code and it appears to match. Please advise if this is due to error, or the cause of the difference.

Code:

import codecademylib3_seaborn
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]

plt.scatter([point[0] for point in data], 
            [point[1] for point in data],
            c=labels)

classifier = Perceptron(max_iter=40)
classifier.fit(data, labels)

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))

print(classifier.score(data, labels))
print(classifier.decision_function([[0, 0], [1, 1], [0.5, 0.5]]))

heatmap = plt.pcolormesh(x_values, y_values, distances_matrix)

plt.colorbar(heatmap)

plt.show()

My heat map:


Get Help heat map:

Any explanation is greatly appreciated!

1 Like

I’m afraid I’ve not actually completed this lesson but does this link help at all-


The original poster was getting slightly different values to those shown in the developer’s video. The linked post above suggested the order may be slightly different. Perhaps you’re coming across a similar issue.

Looks like the order fit generates different models (see the distinct coefficients):

Data: [[0, 0], [0, 1], [1, 0], [1, 1]]
Labels: [0, 0, 0, 1]
Score: 1.0
DV: [-2.  2.  0.]
Coef: [[2. 2.]]


Data: [[1, 1], [0, 0], [0, 1], [1, 0]]
Labels: [1, 0, 0, 0]
Score: 1.0
DV: [-2.   1.  -0.5]
Coef: [[2. 1.]]


Data: [[0, 0], [0, 1], [1, 1], [1, 0]]
Labels: [0, 0, 1, 0]
Score: 1.0
DV: [-3.  1. -1.]
Coef: [[2. 2.]]


Data: [[0, 0], [1, 1], [0, 1], [1, 0]]
Labels: [0, 1, 0, 0]
Score: 1.0
DV: [-4.   1.  -1.5]
Coef: [[3. 2.]]

The model is p(x) = 1 if a*x + b > 0; 0 otherwise.

1 Like

I ran a 1000 iteration simulation where I generated a certain number of data points (4 or 10000) and fit the Perceptron. The results are below:

2 Likes

Yes, that is helpful in explaining the difference. Thank you for the help!

This helps to visualize the link provided above - thank you!