Error in user YAML: (<unknown>): did not find expected key while parsing a block mapping at line 1 column 1
---
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.
# 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 dataset
iris = datasets.load_iris()
# Create our features and target data
features = iris.data
target = iris.target
# 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 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)
# 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)
# 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 the trained perceptron on the features data to make predicts for the target test data
target_pred = ppn.predict(features_test_std)
# 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])
# View model accuracy
# Defined as (1.0 - (# wrong predictions / # total observations))
print('Accuracy: %.2f' % accuracy_score(target_test, target_pred))
Accuracy: 0.97