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) print(psmall.predict(X)) 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:
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.