Download as pdf or txt
Download as pdf or txt
You are on page 1of 10

CSE4020 – Machine Learning

Lab Assessment -2

Name-Tanmay Mehrotra Regno-20BCE2251


MLDA2Q1

April 11, 2022

[50]: #Train SVM classifier using sklearn digits dataset (i.e. from sklearn.datasets␣
,→import

#load_digits) and then


#1. Measure the accuracy of your model using different kernels such as RBF,
#poly, and linear.
#2. Use 80% of samples as training data size.

import pandas as pd #Name-Tanmay Mehrotra ␣


,→

import numpy as np #Regno-20BCE2251


import sklearn
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.datasets import load_digits
d=load_digits()

[51]: p=pd.DataFrame(d.data)
from sklearn.model_selection import train_test_split
p['target']=d.target
x=p.drop(['target'],axis='columns')
y=p.target
x_train,x_test,y_train,y_test=train_test_split(x,y,test_size=0.2)

[52]: from sklearn.metrics import accuracy_score


from sklearn.svm import SVC
m1=SVC(kernel='rbf',random_state=0,probability=True)
pq1=m1.fit(x_train,y_train)
y1=pq1.predict(x_test)
print("Model Score of Kernal(rbf):",pq1.score(x_test,y_test))

Model Score of Kernal(rbf): 0.9861111111111112

[53]: m2=SVC(kernel='linear',random_state=0,probability=True)
pq2=m2.fit(x_train,y_train)
y2=pq2.predict(x_test)
print("Model Score of Kernal(linear) :", pq2.score(x_test,y_test))

1
Model Score of Kernal(linear) : 0.975

[54]: m3=SVC(kernel='poly',random_state=0,probability=True)
pq3=m3.fit(x_train,y_train)
y3=pq3.predict(x_test)
print("Model Score of Kernal(linear) :", pq3.score(x_test,y_test))

Model Score of Kernal(linear) : 0.9861111111111112

[55]: accuracy_score(y1,y_test)

[55]: 0.9861111111111112

[56]: accuracy_score(y2,y_test)

[56]: 0.975

[57]: accuracy_score(y3,y_test)

[57]: 0.9861111111111112

2
MLDA2Q21

April 11, 2022

[7]: #Implement a Multi-layer perceptron model and test the model using any data set␣
,→of your choice.

#The output should include Accuracy, Error rate, Precision, and recall rate
#along with the confusion matrix.

import pandas as pd #Name-Tanmay Mehrotra


import numpy as np #Regno-20BCE2251
import sklearn
from sklearn.datasets import load_iris
iris_data=load_iris()
d=pd.DataFrame(iris_data.data)
d['target']=iris_data.target
x=d.iloc[:,:-1].values
y=d['target'].values
from sklearn.model_selection import train_test_split
x_train,x_test,y_train,y_test=train_test_split(x,y,test_size=0.4)
from sklearn.neural_network import MLPClassifier
c=MLPClassifier(hidden_layer_sizes=(300,200,100,50),random_state=5,verbose=True,learning_rate_
,→01)

p=c.fit(x_train,y_train)

Iteration 1, loss = 1.13586233


Iteration 2, loss = 1.11480385
Iteration 3, loss = 1.21312960
Iteration 4, loss = 0.94415145
Iteration 5, loss = 0.72341031
Iteration 6, loss = 0.56105093
Iteration 7, loss = 0.45634314
Iteration 8, loss = 0.38866345
Iteration 9, loss = 0.35646019
Iteration 10, loss = 0.32543001
Iteration 11, loss = 0.28474306
Iteration 12, loss = 0.23658804
Iteration 13, loss = 0.18533420
Iteration 14, loss = 0.26306958
Iteration 15, loss = 0.49810609
Iteration 16, loss = 0.14090154
Iteration 17, loss = 0.56156263

1
Iteration 18, loss = 0.08703412
Iteration 19, loss = 0.33382404
Iteration 20, loss = 0.22174270
Iteration 21, loss = 0.09159940
Iteration 22, loss = 0.27455652
Iteration 23, loss = 0.08668776
Iteration 24, loss = 0.14124290
Iteration 25, loss = 0.17052740
Iteration 26, loss = 0.08655693
Iteration 27, loss = 0.10469052
Iteration 28, loss = 0.10272025
Iteration 29, loss = 0.06300814
Iteration 30, loss = 0.08916777
Iteration 31, loss = 0.06790478
Iteration 32, loss = 0.04923651
Iteration 33, loss = 0.06736695
Iteration 34, loss = 0.04350207
Iteration 35, loss = 0.05918286
Iteration 36, loss = 0.04071848
Iteration 37, loss = 0.04682750
Iteration 38, loss = 0.03515883
Iteration 39, loss = 0.03702135
Iteration 40, loss = 0.03532809
Iteration 41, loss = 0.02916696
Iteration 42, loss = 0.02779138
Iteration 43, loss = 0.02728358
Iteration 44, loss = 0.02517511
Iteration 45, loss = 0.02472890
Iteration 46, loss = 0.01851500
Iteration 47, loss = 0.02178510
Iteration 48, loss = 0.01655151
Iteration 49, loss = 0.02101115
Iteration 50, loss = 0.01843805
Iteration 51, loss = 0.01647405
Iteration 52, loss = 0.01927557
Iteration 53, loss = 0.01417443
Iteration 54, loss = 0.01467645
Iteration 55, loss = 0.01461583
Iteration 56, loss = 0.01141482
Iteration 57, loss = 0.01188464
Iteration 58, loss = 0.01184237
Iteration 59, loss = 0.01027414
Iteration 60, loss = 0.00970161
Iteration 61, loss = 0.01025482
Iteration 62, loss = 0.00940186
Iteration 63, loss = 0.00834761
Iteration 64, loss = 0.00904266
Iteration 65, loss = 0.00888680

2
Iteration 66, loss = 0.00734439
Iteration 67, loss = 0.00787560
Iteration 68, loss = 0.00854322
Iteration 69, loss = 0.00683208
Iteration 70, loss = 0.00641535
Iteration 71, loss = 0.00732475
Iteration 72, loss = 0.00686232
Iteration 73, loss = 0.00582426
Iteration 74, loss = 0.00541585
Iteration 75, loss = 0.00587846
Iteration 76, loss = 0.00595643
Iteration 77, loss = 0.00497167
Iteration 78, loss = 0.00465793
Iteration 79, loss = 0.00502738
Iteration 80, loss = 0.00481429
Iteration 81, loss = 0.00426438
Iteration 82, loss = 0.00394243
Iteration 83, loss = 0.00411788
Iteration 84, loss = 0.00416635
Iteration 85, loss = 0.00378475
Iteration 86, loss = 0.00345166
Iteration 87, loss = 0.00325886
Iteration 88, loss = 0.00335069
Iteration 89, loss = 0.00370471
Iteration 90, loss = 0.00347912
Iteration 91, loss = 0.00294126
Iteration 92, loss = 0.00273832
Iteration 93, loss = 0.00295157
Iteration 94, loss = 0.00290188
Iteration 95, loss = 0.00246210
Iteration 96, loss = 0.00242256
Iteration 97, loss = 0.00254425
Iteration 98, loss = 0.00226858
Iteration 99, loss = 0.00210463
Iteration 100, loss = 0.00217621
Iteration 101, loss = 0.00202798
Iteration 102, loss = 0.00188004
Iteration 103, loss = 0.00191323
Iteration 104, loss = 0.00181331
Iteration 105, loss = 0.00170631
Iteration 106, loss = 0.00170011
Iteration 107, loss = 0.00165623
Iteration 108, loss = 0.00158155
Iteration 109, loss = 0.00149758
Iteration 110, loss = 0.00148878
Iteration 111, loss = 0.00143129
Iteration 112, loss = 0.00137345
Iteration 113, loss = 0.00135866

3
Iteration 114, loss = 0.00129263
Iteration 115, loss = 0.00126666
Iteration 116, loss = 0.00122972
Training loss did not improve more than tol=0.000100 for 10 consecutive epochs.
Stopping.

[9]: y_pred=p.predict(x_test)
y_pred

[9]: array([1, 2, 2, 2, 2, 1, 1, 1, 2, 1, 2, 2, 2, 1, 1, 2, 2, 0, 2, 1, 0, 1,
2, 2, 2, 1, 1, 0, 1, 1, 0, 1, 2, 1, 1, 2, 1, 0, 0, 2, 2, 1, 2, 1,
0, 1, 2, 2, 1, 2, 0, 1, 0, 0, 1, 0, 2, 1, 2, 0])

[10]: from sklearn.metrics import confusion_matrix,accuracy_score


cm=confusion_matrix(y_test,y_pred)
cm

[10]: array([[12, 0, 0],


[ 0, 23, 2],
[ 0, 1, 22]], dtype=int64)

[11]: accuracy_score(y_test,y_pred) #Accuracy

[11]: 0.95

[12]: from sklearn.metrics import recall_score #Recall Rate


recall_score(y_test,y_pred,average=None,zero_division=1)

[12]: array([1. , 0.92 , 0.95652174])

[13]: from sklearn.metrics import precision_score #Precision


precision_score(y_test,y_pred,average=None,zero_division=1)

[13]: array([1. , 0.95833333, 0.91666667])

[ ]:

4
MLDA2Q3

April 11, 2022

[2]: #Implement the K Nearest Neighbors algorithm and test the algorithm using any␣
,→data set of your choice.

#The output should include Accuracy, Error rate, Precision, and recall rate␣
,→along with the confusion matrix

import pandas as pd
import numpy as np #Name-Tanmay Mehrotra
import sklearn #Regno-20BCE2251
import matplotlib.pyplot as plt
d=pd.read_csv('Salary_Data.csv')
d.head()

[2]: YearsExperience Salary


0 1.1 39343.0
1 1.3 46205.0
2 1.5 37731.0
3 2.0 43525.0
4 2.2 39891.0

[19]: x=d.iloc[:,:-1].values
y=d.iloc[:,-1].values

[20]: x

[20]: array([[ 1.1],


[ 1.3],
[ 1.5],
[ 2. ],
[ 2.2],
[ 2.9],
[ 3. ],
[ 3.2],
[ 3.2],
[ 3.7],
[ 3.9],
[ 4. ],
[ 4. ],
[ 4.1],

1
[ 4.5],
[ 4.9],
[ 5.1],
[ 5.3],
[ 5.9],
[ 6. ],
[ 6.8],
[ 7.1],
[ 7.9],
[ 8.2],
[ 8.7],
[ 9. ],
[ 9.5],
[ 9.6],
[10.3],
[10.5]])

[21]: y

[21]: array([ 39343., 46205., 37731., 43525., 39891., 56642., 60150.,


54445., 64445., 57189., 63218., 55794., 56957., 57081.,
61111., 67938., 66029., 83088., 81363., 93940., 91738.,
98273., 101302., 113812., 109431., 105582., 116969., 112635.,
122391., 121872.])

[22]: from sklearn.model_selection import train_test_split


x_train,x_test,y_train,y_test=train_test_split(x,y,test_size=0.4)

[34]: from sklearn.preprocessing import StandardScaler


s1=StandardScaler()
x_train=s1.fit_transform(x_train)
x_test=s1.transform(x_test)

[38]: from sklearn.neighbors import KNeighborsClassifier


c=KNeighborsClassifier(n_neighbors=5,metric='minkowski',p=2)
q=c.fit(x_train,y_train)
y_pred=q.predict(x_test)

[38]: array([37731., 55794., 37731., 55794., 55794., 37731., 43525., 55794.,


91738., 66029., 43525., 37731.])

[40]: from sklearn.metrics import confusion_matrix,accuracy_score


cm=confusion_matrix(y_test,y_pred)
cm

[40]: array([[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],


[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],

2
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0]], dtype=int64)

[47]: accuracy_score(y_test,y_pred) #Accuracy

[47]: 0.0

[55]: from sklearn.metrics import recall_score #Recall Rate


recall_score(y_test,y_pred,average=None,zero_division=1)

[55]: array([1., 0., 0., 1., 0., 0., 1., 0., 0., 0., 0., 0., 0., 1., 1., 0., 0.])

[57]: from sklearn.metrics import precision_score #Precision


precision_score(y_test,y_pred,average=None,zero_division=1)

[57]: array([0., 1., 1., 0., 1., 1., 0., 1., 1., 1., 1., 1., 1., 0., 0., 1., 1.])

[ ]:

You might also like