Perceptron algorithm not converging on linearly separable data

83 views Asked by At

I am working on a perceptron problem and I have made some fake data and the perceptron algorithm does not converge when the data is linearly separable.

Here is the fake data that is linearly separable.

np.random.seed(42)
linear_df = pd.DataFrame({
    'X1': np.round(np.concatenate([np.random.uniform(low = 0, high= 5, size=4), np.random.uniform(low=8, high=12, size=4)]), 1),
    'X2': np.round(np.concatenate([np.random.uniform(low = 0, high= 5, size=4), np.random.uniform(low=8, high=12, size=4)]),1),
    'Y': [1.0, 1.0, 1.0, 1.0, 0.0, 0.0, 0.0, 0.0]
})

then I run perceptron on it

clf = Perceptron(verbose=1, max_iter=1000)
X = linear_df[['X1', 'X2']]
y = linear_df['Y']
clf.fit(X, y)
linear_coef = clf.coef_
linear_bias = clf.intercept_[0]
print(clf.coef_)
print(clf.intercept_)
print(clf.score(X, y))

Convergence after 8 epochs took 0.00 seconds [[ 2.3 -2.6]] [17.] 0.5

But it says it converges after 8 Epochs and it does not produce the correct output.]

Here is the plot:

perceptron plot

Any ideas?

1

There are 1 answers

0
Tino D On

you would need to do some things, mainly for now, to get the best results, you can increase the sample size and that should be enough for the model to have a better dataset to train on:

np.random.seed(42) # for reproducibility
numSamples = 1000 # number of samples, adjust as needed
linear_df = pd.DataFrame({
    'X1': np.round(np.concatenate([np.random.uniform(low = 0, high= 5, size=numSamples//2), np.random.uniform(low=8, high=12, size=numSamples//2)]), 1),
    'X2': np.round(np.concatenate([np.random.uniform(low = 0, high= 5, size=numSamples//2), np.random.uniform(low=8, high=12, size=numSamples//2)]),1),
    'Y': np.concatenate([np.ones(numSamples//2), -np.ones(numSamples//2)])
})

This already gives the following results:

Convergence after 7 epochs took 0.00 seconds
[[-3.3 -0.6]]
[23.]
1.0

And the classes are easily separated:

Results

I would also advise you to always scale your data, try to research StandardScaler from sklearn.preprocessing.

Furthermore, play around with the number of samples, and see how your models get gradually worse.