I'm stumped as to why this example doesn't do a better job fitting the data, I suspect it has to do with my interpretation of the perceptron object's coefficients. Note that I'm interested in the sklearn object's implementation, not necessarily the math behind a perceptron.

It may help to know the example below yields:

perc.coef_ = array([[ -34. , -681.9,  222. ]])

Poor fit (see code below)

import seaborn as sns
from sklearn.linear_model import Perceptron
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.backends.backend_pdf import PdfPages

# clean data
df = sns.load_dataset('penguins')
df_no_torg = df.loc[df['island'] != 'Torgersen', :]

# extract data
x = df_no_torg.loc[:, ('bill_length_mm', 'flipper_length_mm')].to_numpy()
y = df_no_torg.loc[:, 'island'].to_numpy() == 'Biscoe'
# manual bias term (one less thing to consider)
x = np.hstack((np.ones((x.shape[0], 1)), x))

# init / fit perceptron
perc = Perceptron(fit_intercept=False)
perc.fit(x, y)

def get_boundary(x):
    w = perc.coef_[0]
    m = - w[1] / w[2]
    b = - w[0] / w[2]
    return m * x + b

# plot results
x = np.linspace(30, 60, 100)
b = get_boundary(x)
with PdfPages('penguin_scatter_bound.pdf') as pdf:
    plt.figure(figsize=(10, 5))
    plt.plot(x, b, label='boundary')
    sns.scatterplot(data=df_no_torg, x='bill_length_mm', y='flipper_length_mm', hue='island')

1 Answer 1


The problem here lies in the hyperparameters. perc.n_iter_ is 6 after running your code; according to the defaults in the api, the default value for n_iter_no_change is 5, so it looks like there was no improvement for the first five iterations and training simply terminated (despite the documentation also saying that n_iter_no_change only matters when early_stopping is True).

By playing around with the number of iterations and step size, I was able to get a much better fit (perc.fit(x, y, tol=None, max_iter=20000)).enter image description here


Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.