<jupyter_start><jupyter_text>Perceptron The perceptron algorithm was invented in 1958 at the Cornell Aeronautical Laboratory by <NAME>, funded by the United States Office of Naval Research.![](../assets/perceptron.png)  Mathematical formula of the perceptron $\large \hat{y} = 1, \text{if} \sum_{i=0}^{n} w_i x_i >= b $$\large \hat{y} = 0, \text{otherwise}$  Import Library<jupyter_code>import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from tqdm import tqdm
from sklearn.datasets import load_breast_cancer
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler, MinMaxScaler
from sklearn.metrics import accuracy_score

np.random.seed(0)<jupyter_output><empty_output><jupyter_text>Load Data<jupyter_code>dataset = load_breast_cancer()
dataframe = pd.DataFrame(dataset.data, columns=dataset.feature_names)
dataframe['target'] = dataset.target
print(dataframe.shape)<jupyter_output>(569, 31)<jupyter_text>Explore Data<jupyter_code>dataframe.head()
dataframe.describe()
plt.figure(figsize=(8, 4))
plt.plot(dataframe.describe().loc['min'].T, label='Minimun'),
plt.plot(dataframe.describe().loc['max'].T, label='Maximum'),
plt.plot(dataframe.describe().loc['mean'].T, label='Mean')
plt.xticks(rotation='vertical')
plt.legend()
plt.show()
record_counts = dataframe['target'].value_counts()
plt.bar(record_counts.keys(), record_counts)
plt.xticks(record_counts.keys())
plt.xlabel('Class')
plt.ylabel('No. of records')
plt.title('No. of records per class')
plt.show()<jupyter_output><empty_output><jupyter_text>Train-Test Split<jupyter_code>X = dataframe.drop(columns=['target'])
Y = dataframe['target']
X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size=0.1, stratify=Y, random_state=10)
print(X_train.shape, Y_train.shape, X_test.shape, Y_test.shape)
# Printing the train and test Y samples to ensure both the train and test contain similar no of target data
print(Y.mean(), Y_train.mean(), Y_test.mean())

# As you can see the train and test contain similar samples and this happened by providing the 'stratify' during split.<jupyter_output>0.6274165202108963 0.626953125 0.631578947368421<jupyter_text>Convert to NumPy Array<jupyter_code>X_train, X_test, Y_train, Y_test = X_train.values, X_test.values, Y_train.values, Y_test.values

print(type(X_train), type(X_test), type(Y_train), type(Y_test))<jupyter_output><class 'numpy.ndarray'> <class 'numpy.ndarray'> <class 'numpy.ndarray'> <class 'numpy.ndarray'><jupyter_text>Min-Max Scaler<jupyter_code>plt.figure(figsize=(8, 4))
plt.plot(X_train.min(axis=0), label='Minimun')
plt.plot(X_train.max(axis=0), label='Maximum')
plt.plot(X_train.mean(axis=0), label='Mean')
plt.legend()
plt.show()
fig, ((ax1, ax2), (ax3, ax4)) = plt.subplots(2, 2, figsize=(12, 6))
ax1.plot(X_train[:,0])
ax2.plot(X_train[:,3])
ax3.plot(X_train[:,7])
ax4.plot(X_train[:,11])
plt.show()
minmax_scaler = MinMaxScaler()
X_train = minmax_scaler.fit_transform(X_train)
X_test = minmax_scaler.transform(X_test)

standard_scaler = StandardScaler()
X_train = standard_scaler.fit_transform(X_train)
X_test = standard_scaler.transform(X_test)
plt.figure(figsize=(8, 4))
plt.plot(X_train.min(axis=0), label='Minimun')
plt.plot(X_train.max(axis=0), label='Maximum')
plt.plot(X_train.mean(axis=0), label='Mean')
plt.legend()
plt.show()
fig, ((ax1, ax2), (ax3, ax4)) = plt.subplots(2, 2, figsize=(12, 6))
ax1.plot(X_train[:,0])
ax2.plot(X_train[:,3])
ax3.plot(X_train[:,7])
ax4.plot(X_train[:,11])
plt.show()<jupyter_output><empty_output><jupyter_text>Model Class<jupyter_code>class Perceptron:
    def __init__(self):
        self.w = None
        self.b = None
        self.accuracy_map = {}
        self.best_accuracy = 0
        self.weight_matrix = []
    
    def model(self, x):
        return 1 if (np.dot(self.w, x) >= self.b) else 0

    def predict(self, X):
        Y = []
        for x in X:
            Y.append(self.model(x))
        return np.array(Y)

    def fit(self, X, Y, epochs=1, learning_rate=1):
        self.w = np.ones(X.shape[1])
        self.b = 0
        self._epochs = epochs

        for i in tqdm(range(epochs), total=epochs, unit='epoch', desc="Epoch:", ncols=80):
            for x, y in zip(X, Y):
                y_pred = self.model(x)

                if y == 1 and y_pred == 0:
                    self.w = self.w + learning_rate * x
                    self.b = self.b - learning_rate * 1
                elif y == 0 and y_pred == 1:
                    self.w = self.w - learning_rate * x
                    self.b = self.b + learning_rate * 1

            self.weight_matrix.append(self.w)
            self.accuracy_map[i] = accuracy_score(self.predict(X), Y)
            if self.accuracy_map[i] > self.best_accuracy:
                self.best_accuracy = self.accuracy_map[i]
                chkptw = self.w
                chkptb = self.b

        self.w = chkptw
        self.b = chkptb
        self.weight_matrix = np.array(self.weight_matrix)

        plt.plot(np.array(list(self.accuracy_map.values())).astype(float))
        plt.plot()<jupyter_output><empty_output><jupyter_text>Train Model<jupyter_code>model = Perceptron()
model.fit(X_train, Y_train, epochs=1000, learning_rate=0.001)
plt.plot(model.w)
plt.show()
print(accuracy_score(model.predict(X_train), Y_train))<jupyter_output>0.99609375<jupyter_text>Model Evaluation<jupyter_code>print(accuracy_score(model.predict(X_test), Y_test))<jupyter_output>0.9824561403508771
StarcoderdataJupyterStructuredCleanDedup