Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
---
title: "Perceptron In SciKit-Learn
author: "Damian Mingle"
date: 04/30/2018
---

The perceptron is a supervised learning algorithm which uses a set of function to learn from inputs to determine if they belong to some class or not. It was one of the earliest machine learning techniques used and still is at the center of many modern neural networks.

Preliminaries

# Load required libraries
from sklearn import datasets
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import Perceptron
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score

Load The Iris Data

# Load the iris dataset
iris = datasets.load_iris()

# Create our features and target data
features = iris.data
target = iris.target

View The Iris Data

# View the first five observations of our target data
target[:5]
array([0, 0, 0, 0, 0])
# View the first five observations of our feature data
# Observe the four independent variables (features)
features[:5]
array([[5.1, 3.5, 1.4, 0.2],
       [4.9, 3. , 1.4, 0.2],
       [4.7, 3.2, 1.3, 0.2],
       [4.6, 3.1, 1.5, 0.2],
       [5. , 3.6, 1.4, 0.2]])

Split The Iris Data Into Training And Test

# Split the data into 70% training data and 30% test data
features_train, features_test, target_train, target_test = train_test_split(features, target, test_size=0.2)

Preprocess The X Data By Scaling

# Train the scaler, which standarizes all the features to have mean=0 and unit variance
sc = StandardScaler()
sc.fit(features_train)
StandardScaler(copy=True, with_mean=True, with_std=True)
# Apply the scaler to the feature training data
features_train_std = sc.transform(features_train)

# Apply the SAME scaler to the feature test data
features_test_std = sc.transform(features_test)

Train A Perceptron Learner

# Create a perceptron object with the parameters: 60 iterations (epochs) over the data, and a learning rate of 0.15
ppn = Perceptron(max_iter=60, eta0=0.15, random_state=0)

# Train the perceptron
ppn.fit(features_train_std, target_train)
Perceptron(alpha=0.0001, class_weight=None, eta0=0.15, fit_intercept=True,
      max_iter=60, n_iter=None, n_jobs=1, penalty=None, random_state=0,
      shuffle=True, tol=None, verbose=0, warm_start=False)

Apply Trained Model to Test Data

# Apply the trained perceptron on the features data to make predicts for the target test data
target_pred = ppn.predict(features_test_std)

Compare Actual and Predicted Target

# View the predicted target test data
target_pred
array([0, 0, 0, 0, 0, 1, 0, 2, 1, 1, 0, 1, 2, 2, 2, 1, 2, 2, 2, 0, 1, 1,
       2, 1, 2, 2, 1, 0, 2, 1])
# View the true y test data
target_test
array([0, 0, 0, 0, 0, 1, 0, 2, 1, 1, 0, 1, 2, 2, 2, 0, 2, 2, 2, 0, 1, 1,
       2, 1, 2, 2, 1, 0, 2, 1])

Evaluation Metric: Accuracy

# View model accuracy
# Defined as (1.0 - (# wrong predictions / # total observations))
print('Accuracy: %.2f' % accuracy_score(target_test, target_pred))
Accuracy: 0.97
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.