Practical - 5
AIM :- I and F Pattern Classification Using
Perceptron
Importing Libraries
In [50]:
1 import numpy as np
2 import pandas as pd
3 from sklearn.linear_model import Perceptron
Dataset
In [51]:
1 %%html
2 <img src="I_and_F_pattern.png" width="40%">
Here, we are using -1 in place of 0
In [52]:
1 I = [1, 1, 1, -1, 1, -1, 1, 1, 1]
2 F = [1, 1, 1, 1, 1, 1, 1, -1, -1]
In [53]:
1 x = [I, F]
2 print(x)
[[1, 1, 1, -1, 1, -1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, -1, -1]]
In [54]:
1 y = [1, -1]
Creating the Model
In [55]:
1 model = Perceptron(tol=1e-1, random_state=0)
2 model.fit(x, y)
Out[55]:
Perceptron(tol=0.1)
In [56]:
1 # Getting the mean accuracy on the given test data and labels
2 model.score(x, y)
Out[56]:
1.0
Predicting the Class Labels
In [57]:
1 # Predicting class labels for samples in X.
2 model.predict(x)
Out[57]:
array([ 1, -1])
In [58]:
1 ycal = model.predict(x[0:1])
2 print('Predicted Class:', ycal)
Predicted Class: [1]
In [59]:
1 # Predict confidence scores for samples.
2 model.decision_function(x)
Out[59]:
array([ 8., -8.])
In [60]:
1 model.densify()
2 # model.densify() function converts coefficient matrix to dense array format.
Out[60]:
Perceptron(tol=0.1)
Getting the Parameters of Model
In [61]:
1 model.get_params()
Out[61]:
{'alpha': 0.0001,
'class_weight': None,
'early_stopping': False,
'eta0': 1.0,
'fit_intercept': True,
'l1_ratio': 0.15,
'max_iter': 1000,
'n_iter_no_change': 5,
'n_jobs': None,
'penalty': None,
'random_state': 0,
'shuffle': True,
'tol': 0.1,
'validation_fraction': 0.1,
'verbose': 0,
'warm_start': False}