-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
259 lines (199 loc) · 12.5 KB
/
Copy pathmain.py
File metadata and controls
259 lines (199 loc) · 12.5 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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
from sklearn.datasets import fetch_openml
mnist=fetch_openml('mnist_784', version=1, as_frame=False) #as_frame controls the format in which the dataset is returned. false=numpy array, true=pandas dataframe
print(mnist.keys()) #used to list all the components inside mnist
import numpy as np
k,s=mnist["data"], mnist["target"]
s=s.astype(np.uint8) #converting from string to integer
print(k.shape)
print(s.shape)
import matplotlib.pyplot as plt
digit=k[0]
digit_image=digit.reshape(28,28) #reshaping the image to 28x28 matrix
plt.imshow(digit_image, cmap="binary")
plt.axis("off")
plt.show()
k_train, k_test, s_train, s_test = k[:60000], k[60000:], s[:60000], s[60000:] #splitting the training and test set
s_train_5 = (s_train==5) #creating target vectors where it stores true if the target is 5, else false
s_test_5 = (s_test==5)
from sklearn.linear_model import SGDClassifier #using sgdclassifier for training and predicting the digit 5
sgd=SGDClassifier(random_state=42)
sgd.fit(k_train, s_train_5)
print(sgd.predict([digit]))
from sklearn.model_selection import cross_val_score #cross validation using crossvalscore
cvsc=cross_val_score(sgd, k_train, s_train_5, cv=3, scoring="accuracy")
print(cvsc.mean()) #95.7%
from sklearn.model_selection import cross_val_predict #creating a set of predictions
s_train_pred=cross_val_predict(sgd, k_train, s_train_5, cv=3)
from sklearn.metrics import confusion_matrix #using a confusion matrix
print(confusion_matrix(s_train_5, s_train_pred))
from sklearn.metrics import precision_score, recall_score #checking precision and accuracy score
print(precision_score(s_train_5, s_train_pred)) #83.71%
print(recall_score(s_train_5, s_train_pred)) #65.11%
from sklearn.metrics import f1_score #checking f1score
print(f1_score(s_train_5, s_train_pred)) #0.7325
s_scores=cross_val_predict(sgd, k_train, s_train_5, cv=3, method="decision_function") #decision scores used for making predictions
print(s_scores)
from sklearn.metrics import precision_recall_curve #using precision recall curve to compute precision and recall for all thresholds
precisions, recalls, thresholds = precision_recall_curve(s_train_5, s_scores) #gives an array of precision/recall values at different thresholds and the score values used to compute each point(precision,recall)
def plot_precision_recall_vs_threshold(precisions, recalls, thresholds, highlight_threshold=None): #function to plot precision and recall vs threshold
plt.figure(figsize=(10,8))
plt.plot(thresholds, precisions[:-1], "b--", label="precision") #excluding last precision point as it does not have a threshold/ "b--" = blue dashed line
plt.plot(thresholds, recalls[:-1], "g--", label="recall") #excluding last recall point as it does not have a threshold/ "g--" = green dashed line
if highlight_threshold is not None:
plt.axvline(x=highlight_threshold, color="r", linestyle="--", label=f"threshold={highlight_threshold:.2f}") #if a threshold is passed, draw a vertical dashed red line at that threshold
plt.xlabel("threshold")
plt.ylabel("score")
plt.legend()
plt.grid(True)
plt.show()
plot_precision_recall_vs_threshold(precisions, recalls, thresholds, highlight_threshold=0.0)
plt.figure(figsize=(10,8))
plt.plot(recalls, precisions, color="blue")
plt.axvline(x=0.70, color="red", linestyle="--", label="recall")
plt.xlabel("recall")
plt.ylabel("precision")
plt.legend()
plt.grid(True)
plt.show()
threshold_90_precision=thresholds[np.argmax(precisions>=0.90)] #checking for threshold value for 90% precision
print(threshold_90_precision) #3370.019
s_train_pred_90=(s_scores>=threshold_90_precision)
print(precision_score(s_train_5, s_train_pred_90)) #90%
print(recall_score(s_train_5, s_train_pred_90)) #47.99%
from sklearn.metrics import roc_curve #plotting roc curve
fpr, tpr, thresholds = roc_curve(s_train_5, s_scores)
def plot_roc_curve(fpr, tpr, label=None):
plt.plot(fpr, tpr, linewidth=2, label=label)
plt.plot([0,1], [0,1], 'k--')
plt.grid(True)
plt.xlabel("FPR")
plt.ylabel("TPR")
if plt.legend:
plt.legend()
plt.show()
plot_roc_curve(fpr, tpr)
from sklearn.metrics import roc_auc_score #checking auc score
print(roc_auc_score(s_train_5, s_scores)) #0.96
from sklearn.ensemble import RandomForestClassifier
forest_clf=RandomForestClassifier(random_state=42)
s_probas_forest=cross_val_predict(forest_clf, k_train, s_train_5, cv=3, method="predict_proba") #returns class probabilities in the form of a 2D array
s_scores_forest=s_probas_forest[:, 1] #gives the probability for positive class (digit==5)
fpr_forest, tpr_forest, thresholds_forest = roc_curve(s_train_5, s_scores_forest) #roc curve of random forest classifier
plt.plot(fpr, tpr, "b:", label="SGD") #plotting roc curve for sgdclassifier
plot_roc_curve(fpr_forest, tpr_forest, "Random Forest") #plotting roc curve for random forest classifier
plt.legend(loc="lower right")
plt.show()
print(roc_auc_score(s_train_5, s_scores_forest)) #0.99
s_forest_pred=cross_val_predict(forest_clf, k_train, s_train_5, cv=3)
print(precision_score(s_train_5, s_forest_pred)) #99.05%
print(recall_score(s_train_5, s_forest_pred)) #86.62%
from sklearn.svm import SVC #using support vector classifier for multiclass classifier using ovo strategy
svm_clf=SVC()
svm_clf.fit(k_train, s_train)
print(svm_clf.predict([digit]))
print(cross_val_score(sgd, k_train, s_train, cv=3, scoring="accuracy")) #comparing the accuracies
print(cross_val_score(forest_clf, k_train, s_train, cv=3, scoring="accuracy"))
from sklearn.preprocessing import StandardScaler #scaling the inputs
std_scl=StandardScaler()
k_trained_scaled=std_scl.fit_transform(k_train.astype(np.float64))
forest_mean=cross_val_score(forest_clf, k_trained_scaled, s_train, cv=3, scoring="accuracy")
print(forest_mean.mean()) #0.9644
s_train_pred=cross_val_predict(forest_clf, k_trained_scaled, s_train, cv=3)
conf_mx=confusion_matrix(s_train, s_train_pred) #confusion matrix
print(conf_mx)
plt.matshow(conf_mx, cmap=plt.cm.gray)
plt.show()
rows_sum=conf_mx.sum(axis=1, keepdims=True) #keepdims preserves the number of dimensions
norm_conf_mx=conf_mx/rows_sum
np.fill_diagonal(norm_conf_mx, 0)
plt.matshow(norm_conf_mx, cmap=plt.cm.gray)
plt.show()
def plot_digits(instances, images_per_row=10, **options):
size = 28
images_per_row = min(len(instances), images_per_row)
images = [instance.reshape(size, size) for instance in instances]
n_rows = (len(instances) - 1) // images_per_row + 1
row_images = []
for row in range(n_rows):
r_images = images[row * images_per_row : (row + 1) * images_per_row]
row_image = np.concatenate(r_images, axis=1)
row_images.append(row_image)
image = np.concatenate(row_images, axis=0)
plt.imshow(image, cmap="binary", **options)
plt.axis("off")
cl_a, cl_b = 4, 9
k_aa = k_train[(s_train == cl_a) & (s_train_pred == cl_a)]
k_ab = k_train[(s_train == cl_a) & (s_train_pred == cl_b)]
k_ba = k_train[(s_train == cl_b) & (s_train_pred == cl_a)]
k_bb = k_train[(s_train == cl_b) & (s_train_pred == cl_b)]
plt.figure(figsize=(8,8))
plt.subplot(221); plot_digits(k_aa[:25], images_per_row=5)
plt.subplot(222); plot_digits(k_ab[:25], images_per_row=5)
plt.subplot(223); plot_digits(k_ba[:25], images_per_row=5)
plt.subplot(224); plot_digits(k_bb[:25], images_per_row=5)
plt.show()
from sklearn.neighbors import KNeighborsClassifier #using kneighborsclassifier for multilabel classification
s_train_large = (s_train >= 7)
s_train_odd = (s_train %2 == 1)
s_multilabel = np.c_[s_train_large, s_train_odd]
knc = KNeighborsClassifier()
knc.fit(k_train, s_multilabel)
print(knc.predict([digit]))
noise=np.random.randint(0, 100, (len(k_train), 784))
k_train_mod = np.clip(k_train + noise, 0, 255)
noise2=np.random.randint(0, 100, (len(k_test), 784))
k_test_mod = np.clip(k_test + noise2, 0, 255)
s_train_mod = k_train_mod
s_test_mod = k_test_mod
plt.figure(figsize=(10,2))
for i in range(5):
img=k_train_mod[i].reshape(28, 28)
plt.subplot(1, 5, i + 1)
plt.imshow(img, cmap="gray")
plt.axis('off')
plt.suptitle(" first 5 training images")
plt.tight_layout()
plt.show()
plt.figure(figsize=(10,2))
for i in range(5):
img=k_test_mod[i].reshape(28, 28)
plt.subplot(1, 5, i + 1)
plt.imshow(img, cmap="gray")
plt.axis('off')
plt.suptitle(" first 5 testing images")
plt.tight_layout()
plt.show()
knc.fit(k_train_mod, k_train)
digit = 0 # Index of the test digit to denoise
noisy_digit = k_test_mod[digit].reshape(1, -1) # Shape (1, 784)
clean_digit = knc.predict(noisy_digit) # Output: array of shape (1, 784)
def plot_digit(image_vector, title=""):
image = image_vector.reshape(28, 28)
plt.imshow(image, cmap='gray')
plt.title(title)
plt.axis('off')
plt.show()
plot_digit(clean_digit[0], title="Predicted Clean Digit")
svm_clf.fit(k_trained_scaled, s_train)
svm_train_pred = cross_val_predict(svm_clf, k_trained_scaled, s_train, cv=3)
conf_mx2 = confusion_matrix(s_train, svm_train_pred)
print(conf_mx2)
print(precision_score(s_train, svm_train_pred, average="weighted" )) #0.9604
print(recall_score(s_train, svm_train_pred, average="weighted" )) #0.9602
svm_mean=cross_val_score(svm_clf, k_trained_scaled, s_train, cv=3, scoring="accuracy")
print(svm_mean.mean()) #0.9602
from sklearn.preprocessing import MultiLabelBinarizer #converting labels to multilabels
mlb = MultiLabelBinarizer(classes=range(10))
s_multilabel = mlb.fit_transform([[d] for d in s_train])
print(s_train[0])
print(s_multilabel[0])
forest_clf.fit(k_train, s_multilabel)
digit_pred = forest_clf.predict([k_train[0]])
print(digit_pred)
s_multilabel_test = mlb.transform([[d] for d in s_test])
s_multilabel_pred = forest_clf.predict(k_test)
from sklearn.metrics import accuracy_score
acc=accuracy_score(s_multilabel_test, s_multilabel_pred)
print(acc) #0.90
print(precision_score(s_multilabel_test, s_multilabel_pred, average="weighted" )) #0.99
print(recall_score(s_multilabel_test, s_multilabel_pred, average="weighted" )) #0.90