-
Notifications
You must be signed in to change notification settings - Fork 0
/
logistic_regression_and_image_classifivation.py
179 lines (85 loc) · 2.63 KB
/
logistic_regression_and_image_classifivation.py
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
#!/usr/bin/env python
# coding: utf-8
# In[1]:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
# In[2]:
from sklearn.datasets import load_digits
function = load_digits()
# In[3]:
# input variables or features
function.data.shape # X
X= function.data
# In[4]:
# output labesl
function.target.shape # y
y=function.target
# In[5]:
plt.figure(figsize=(40,15))
for index, (image, label) in enumerate(zip(function.data[0:10], function.target[0:40])):
plt.subplot(1,40,index+1)
plt.imshow(np.reshape(image,(8,8)),cmap=plt.cm.gray)
plt.title(label, fontsize=20)
# In[7]:
# splitting the dataset
from sklearn.model_selection import train_test_split
X_train,X_test,y_train,y_test = train_test_split(X,y, test_size = 0.2, random_state = 0)
# In[8]:
print("Train input data of X: ",X_train.shape)
print("Test input data of X: ",X_test.shape)
print("Train input data of y: ",y_train.shape)
print("Test input data of y: ",y_test.shape)
# In[9]:
# model train
from sklearn.linear_model import LogisticRegression
lr = LogisticRegression().fit(X_train, y_train)
lr
# In[10]:
lr.predict(X_test[0:50])
prediction = lr.predict(X_test)
prediction
# In[11]:
# acuracy prediction
score = lr.score(X_test,y_test)
print("The accuracy score is : ", score)
# In[12]:
# confusion matrix
from sklearn import metrics
cm = metrics.confusion_matrix(y_test, prediction)
cm
# In[13]:
fig = plt.figure(figsize = (9,9))
sns.heatmap(cm, annot=True, fmt=".3f", linewidths = .5, square = True, cmap = "Spectral");
plt.ylabel("actual output")
plt.xlabel("predicted output")
comple_title = "Accuracy Score: {0}".format(score)
plt.title(comple_title, size = 15);
# In[14]:
fig.savefig('accuracy.png')
# In[15]:
print(cm)
# In[16]:
# getting the miisqualified values
import numpy as np
import matplotlib.pyplot as plt
index = 0
misclassifiedIndexes = []
for label, predict in zip(y_test, prediction):
if label != predict:
misclassifiedIndexes.append(index)
index +=1
# In[24]:
fig2 = plt.figure(figsize = (30,8))
for plotIndex, badIndex in enumerate(misclassifiedIndexes[0:5]): # getting only 10 misqualified values
plt.subplot(1,5,plotIndex + 1)
plt.imshow(np.reshape(X_test[badIndex], (8,8)), cmap=plt.cm.gray)
plt.title("Predicted: {}, Actual: {},".format(prediction[badIndex], y_test[badIndex]), fontsize = 20 ) # as predicted values are equil to the actual values because my model accuracy is very good
# In[25]:
fig2.savefig("human vs machine behaviour with tayyab's model accuracy of 96%")
# In[ ]:
# In[ ]:
# In[ ]:
# In[ ]:
# In[ ]: