from sklearn.linear_model import Perceptron
import numpy as np
import matplotlib.pyplot as plt

def plot_predictions_and_data(X, y_obs, model):
    min_x1 = np.min(X[:, 0])
    max_x1 = np.max(X[:, 0])

    min_x2 = np.min(X[:, 1])
    max_x2 = np.max(X[:, 1])

    x1, x2 = np.meshgrid(np.linspace(0, max_x1 * 1.2, 500),
                     np.linspace(0, max_x2 * 1.2, 500))
    y_pred = model.predict(np.c_[x1.ravel(), x2.ravel()])
    y_pred = y_pred.reshape(x1.shape)    
    cs = plt.contourf(x1, x2, y_pred, alpha = 0.4)
    plt.gca().scatter(X[:, 0], X[:, 1], c = y_obs, edgecolor='black')
X = np.array([[2, 4], [8, 2], [10, 5]])
y_obs = np.array([0, 0, 1])

psmall = Perceptron()
psmall.fit(X, y_obs)
plot_predictions_and_data(X, y_obs, psmall)

When I tried training an sklearn Perceptron classifier (code above) on very simple data, I got decision boundaries that don't make sense, shown below:

weird sklearn Perceptron output

Running the perceptron fit with verbose=1 didn't really give me any insight. I think I must be missing something very basic and important.

  • $\begingroup$ psmall = LogisticRegression() works $\endgroup$ – Haitao Du Jul 17 '20 at 19:21
  • $\begingroup$ This is actually basically what I'm curious about! I thought I understood the relationship between Perceptrons and logistic regression, but the fact that the Perceptron is failing here makes me think there's something very important that I'm not understanding. $\endgroup$ – Katalin Benedito Jul 17 '20 at 19:24
  • $\begingroup$ yes, I spent about 20 min on it now, and have no idea what was happening.... may be it is running on regression mode not classification mode? I am using R most of the time... $\endgroup$ – Haitao Du Jul 17 '20 at 19:34
  • $\begingroup$ @gunes could you provide any help? $\endgroup$ – Haitao Du Jul 18 '20 at 4:16
  • $\begingroup$ Interesting that increasing n_iter_no_change at least "fixes" this example. $\endgroup$ – rickhg12hs Jul 19 '20 at 3:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.