Perceptron is a supervised learning algorithm for classification. It was originally proposed by Frank Rosenblatt and, refined and carefully analyzed by Marvin Minsky and Seymour Papert in 1969. The model is known as Perceptron Model.
Import Libraries
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
from sklearn.metrics import accuracy_score
%matplotlib inline
Perceptron Class
class Perceptron:
def __init__(self, learning_rate=0.01, n_iters=1000):
self.lr = learning_rate
self.n_iters = n_iters
self.activation_func = self._unit_step_func
self.weights = None
self.bias = None
def fit(self, X, y):
n_samples, n_features = X.shape
self.weights = np.zeros(n_features) #Initializing weights
self.bias = 0
y_ = np.array([1 if i > 0 else 0 for i in y])
for _ in range(self.n_iters):
for idx, x_i in enumerate(X):
net_input = np.dot(x_i, self.weights) + self.bias
y_predicted = self.activation_func(net_input)
# Perceptron update rule
update = self.lr * (y_[idx] - y_predicted)
self.weights += update * x_i
self.bias += update
def predict(self, X):
net_input = np.dot(X, self.weights) + self.bias
y_predicted = self.activation_func(net_input)
return y_predicted
def _unit_step_func(self, x):
return np.where(x>=0, 1, 0)
Read Data
df = pd.read_csv('https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data', header=None)
df.columns=['Sepal_length','Sepal_width','Petal_length','Petal_width','Species']
print(df[0:100])
y = df.iloc[0:100, 4].values
y = np.where(y == 'Iris-setosa', 0, 1)
# sepal length and petal length
X = df.iloc[0:100, [0,1,2,3]].values
Plotting
plt.scatter(X[:50, 0], X[:50, 1],
color='green', marker='x', label='setosa')
plt.scatter(X[50:100, 0], X[50:100, 1],
color='red', marker='o', label='versicolor')
plt.xlabel('sepal length')
plt.ylabel('petal length')
plt.legend(loc='upper right')
plt.show()
Applying Perceptron
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=5)
p = Perceptron(learning_rate=0.01, n_iters=1000)
p.fit(X_train, y_train)
predictions = p.predict(X_test)
print("True Values from the testing data:",y_test)
print("Predictions on the testing data:",predictions)
print("Perceptron classification accuracy", accuracy_score(y_test, predictions))