Repository navigation
Expand file tree
/
Copy pathtask_12.py
More file actions
229 lines (159 loc) · 5.93 KB
/
Copy pathtask_12.py
File metadata and controls
229 lines (159 loc) · 5.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
# -*- coding: utf-8 -*-
"""Task-12.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/github/Sourav61/Goeduhub-Assignments/blob/main/Task_12.ipynb
"""
from google.colab import drive
drive.mount('/content/drive')
"""Author: <a href = "https://github.com/Sourav61">Sourav Pahwa</a>
<br>ID: GO_STP_13420
<b>Train SVM classifier using sklearn digits dataset (i.e. from sklearn.datasets import load_digits) and then,</b>
Measure accuracy of your model using different kernels such as rbf and linear.
Tune your model further using regularization and gamma parameters and try to come up with highest accurancy score
Use 80% of samples as training data size
"""
# Commented out IPython magic to ensure Python compatibility.
import numpy as np
import pandas as pd
import seaborn as sns
import warnings
warnings.filterwarnings('ignore')
import matplotlib.pyplot as plt
# %matplotlib inline
from sklearn.datasets import load_digits
digits = load_digits()
digits.keys()
digits.DESCR
digits.images
digits.data
digits.target
digits.target_names
df = pd.DataFrame(digits.data)
df.head(10)
df.tail(10)
df.info()
df.describe(include='all')
df.dtypes
df.describe().style.background_gradient(cmap='PuBu', low=0, high=0, axis=0, subset=None, text_color_threshold=0.408, vmin=None, vmax=None)
def inspect_data(data):
return pd.DataFrame({"Data Type":data.dtypes,"No of Levels":data.apply(lambda x: x.nunique(),axis=0), "Levels":data.apply(lambda x: str(x.unique()),axis=0)})
inspect_data(df)
df.duplicated().sum()
df.isnull().sum(axis=0)
import missingno as msno
msno.matrix(df.sample(1797))
plt.show()
msno.bar(df.sample(1797))
plt.show()
digits.data.shape
digits.target.shape
plt.inferno()
plt.matshow(digits.images[0])
plt.show()
plt.hot()
plt.matshow(digits.images[1])
plt.show()
plt.hsv()
plt.matshow(digits.images[2])
plt.show()
plt.jet()
plt.matshow(digits.images[3])
plt.show()
plt.cool()
plt.matshow(digits.images[4])
plt.show()
plt.copper()
plt.matshow(digits.images[5])
plt.show()
plt.magma()
plt.matshow(digits.images[6])
plt.show()
plt.hist(df)
sns.displot(df)
plt.show()
sns.distplot(df)
plt.show()
sns.ecdfplot(data=df)
plt.legend()
fig = plt.figure(figsize=(20, 20))
fig.subplots_adjust(left=0, right=1, bottom=0, top=1, hspace=0.05, wspace=0.5)
for i in range(10):
ax = fig.add_subplot(5, 5, i + 1, xticks=[], yticks=[])
ax.imshow(digits.images[i], cmap=plt.cm.binary, interpolation='gaussian')
ax.text(0, 7, str(digits.target[i]))
fig = plt.figure(figsize=(20, 20))
fig.subplots_adjust(left=0, right=1, bottom=0, top=1, hspace=0.05, wspace=0.5)
for i in range(10):
ax = fig.add_subplot(5, 5, i + 1, xticks=[], yticks=[])
ax.imshow(digits.images[i], cmap=plt.cm.binary, interpolation='antialiased')
ax.text(0, 7, str(digits.target[i]))
fig = plt.figure(figsize=(20, 20))
fig.subplots_adjust(left=0, right=1, bottom=0, top=1, hspace=0.05, wspace=0.5)
for i in range(10):
ax = fig.add_subplot(5, 5, i + 1, xticks=[], yticks=[])
ax.imshow(digits.images[i], cmap=plt.cm.binary, interpolation='bilinear')
ax.text(0, 7, str(digits.target[i]))
fig = plt.figure(figsize=(20, 20))
fig.subplots_adjust(left=0, right=1, bottom=0, top=1, hspace=0.05, wspace=0.5)
for i in range(10):
ax = fig.add_subplot(5, 5, i + 1, xticks=[], yticks=[])
ax.imshow(digits.images[i], cmap=plt.cm.binary, interpolation='bicubic')
ax.text(0, 7, str(digits.target[i]))
fig = plt.figure(figsize=(20, 20))
fig.subplots_adjust(left=0, right=1, bottom=0, top=1, hspace=0.05, wspace=0.5)
for i in range(10):
ax = fig.add_subplot(5, 5, i + 1, xticks=[], yticks=[])
ax.imshow(digits.images[i], cmap=plt.cm.binary, interpolation='spline36')
ax.text(0, 7, str(digits.target[i]))
fig = plt.figure(figsize=(20, 20))
fig.subplots_adjust(left=0, right=1, bottom=0, top=1, hspace=0.05, wspace=0.5)
for i in range(10):
ax = fig.add_subplot(5, 5, i + 1, xticks=[], yticks=[])
ax.imshow(digits.images[i], cmap=plt.cm.binary, interpolation='hanning')
ax.text(0, 7, str(digits.target[i]))
fig = plt.figure(figsize=(20, 20))
fig.subplots_adjust(left=0, right=1, bottom=0, top=1, hspace=0.05, wspace=0.5)
for i in range(10):
ax = fig.add_subplot(5, 5, i + 1, xticks=[], yticks=[])
ax.imshow(digits.images[i], cmap=plt.cm.binary, interpolation='sinc')
ax.text(0, 7, str(digits.target[i]))
fig = plt.figure(figsize=(20, 20))
fig.subplots_adjust(left=0, right=1, bottom=0, top=1, hspace=0.05, wspace=0.5)
for i in range(10):
ax = fig.add_subplot(5, 5, i + 1, xticks=[], yticks=[])
ax.imshow(digits.images[i], cmap=plt.cm.binary, interpolation='blackman')
ax.text(0, 7, str(digits.target[i]))
from sklearn.model_selection import train_test_split
x=df
y=digits.target
xtrain, xtest, ytrain, ytest = train_test_split(x, y, test_size = 0.2, random_state = 9)
print(f"The shape of the x train is: {xtrain.shape}")
print(f"The shape of the x test is: {xtest.shape}")
print(f"The shape of the y train is: {ytrain.shape}")
print(f"The shape of the y test is: {ytest.shape}")
print(f"The length of the x train is: {len(xtrain)}")
print(f"The length of the x test is: {len(xtest)}")
print(f"The length of the y train is: {len(ytrain)}")
print(f"The length of the y test is: {len(ytest)}")
from sklearn.svm import SVC
model = SVC(kernel = 'linear')
model.fit(xtrain, ytrain)
pred = model.predict(xtest)
from sklearn.metrics import accuracy_score,recall_score,f1_score
print("accuracy = ", accuracy_score(ytest, pred))
print("recall_score = ",recall_score(ytest, pred,average=None))
print("F1 score = ",f1_score(ytest, pred,average=None))
from sklearn.metrics import confusion_matrix
cn = confusion_matrix(ytest, pred)
print(cn)
a_model = SVC(kernel = 'rbf',C=0.3)
a_model.fit(xtrain, ytrain)
a_pred = a_model.predict(xtest)
a = accuracy_score(ytest, a_pred)
a
b_model = SVC(kernel = 'poly',gamma='scale')
b_model.fit(xtrain, ytrain)
b_pred = b_model.predict(xtest)
c = accuracy_score(ytest, b_pred)
c