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

import matplotlib.

pyplot as plt
z = 1
fig, plots = plt.subplots(3, 3, figsize=(9, 9))
for i in range(0,3):
for j in range(0,3):
img = plt.imread(f'{z}.JPG')
plots[i,j].imshow(img)
plots[i,j].axis(False)
z += 1

import matplotlib.pyplot as plt


z = 1
fig, plots = plt.subplots(2, 3, figsize=(10, 10))
for i in range(0,2):
for j in range(0,3):
img = plt.imread(f"weather{z}.jpg")
plots[i,j].imshow(img)
plots[i,j].axis(False)
z += 1

import matplotlib.pyplot as plt


z = 1
fig, plots = plt.subplots(2, 3, figsize=(10, 10))
for i in range(0,2):
for j in range(0,3):
img = plt.imread(f"elephant{z}.jpg")
plots[i,j].imshow(img)
plots[i,j].axis(False)
z += 1
import matplotlib.pyplot as plt
z = 1
fig, plots = plt.subplots(2, 3, figsize=(10, 10))
for i in range(0,2):
for j in range(0,3):
img = plt.imread(f"chess{z}.jpg")
plots[i,j].imshow(img)
plots[i,j].axis(False)
z += 1
import matplotlib.pyplot as plt
import random as rd
img = plt.imread(f"amer_sign2.png")
plt.imshow(img)

<matplotlib.image.AxesImage at 0x1f1a2b1d580>
#importing data
from sklearn.model_selection import train_test_split
from sklearn.metrics import confusion_matrix
from sklearn.metrics import accuracy_score
from sklearn.metrics import f1_score
from sklearn.neighbors import KNeighborsClassifier
from sklearn.tree import DecisionTreeClassifier
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import cross_val_score
from sklearn.model_selection import RepeatedStratifiedKFold
from numpy import mean, std

import pandas as pd
data = pd.read_csv("sign_mnist_train.csv")
train_data, test_data = train_test_split(data, test_size=0.2,
shuffle=False)

features = train_data.drop("label", axis=1)


labels = train_data['label']

features

pixel1 pixel2 pixel3 pixel4 pixel5 pixel6 pixel7 pixel8


pixel9 \
0 107 118 127 134 139 143 146 150
153
1 155 157 156 156 156 157 156 158
158
2 187 188 188 187 187 186 187 188
187
3 211 211 212 212 211 210 211 210
210
4 164 167 170 172 176 179 180 184
185
... ... ... ... ... ... ... ... ...
...
21959 131 134 137 139 141 144 146 146
147
21960 143 148 151 155 159 161 163 166
168
21961 184 185 185 185 185 186 185 186
185
21962 184 188 192 195 197 199 202 204
206
21963 76 79 87 99 108 114 118 121
124

pixel10 ... pixel775 pixel776 pixel777 pixel778 pixel779


\
0 156 ... 207 207 207 207 206

1 157 ... 69 149 128 87 94

2 186 ... 202 201 200 199 198

3 211 ... 235 234 233 231 230

4 186 ... 92 105 105 108 133

... ... ... ... ... ... ... ...

21959 149 ... 183 180 181 182 182

21960 169 ... 104 112 141 152 161

21961 184 ... 218 218 217 213 210

21962 206 ... 35 47 67 43 25

21963 127 ... 189 190 191 191 193

pixel780 pixel781 pixel782 pixel783 pixel784


0 206 206 204 203 202
1 163 175 103 135 149
2 199 198 195 194 195
3 226 225 222 229 163
4 163 157 163 164 179
... ... ... ... ... ...
21959 182 181 180 180 179
21960 180 183 166 171 183
21961 215 150 29 11 51
21962 40 61 63 45 45
21963 193 193 193 191 189

[21964 rows x 784 columns]

import numpy as np
features = np.array(features)
labels = np.array(labels)
features.shape, labels.shape

((21964, 784), (21964,))

model = LogisticRegression(multi_class='multinomial', solver='lbfgs')


model.fit(features, labels)

E:\anaconda3\lib\site-packages\sklearn\linear_model\_logistic.py:763:
ConvergenceWarning: lbfgs failed to converge (status=1):
STOP: TOTAL NO. of ITERATIONS REACHED LIMIT.

Increase the number of iterations (max_iter) or scale the data as


shown in:
https://scikit-learn.org/stable/modules/preprocessing.html
Please also refer to the documentation for alternative solver options:

https://scikit-learn.org/stable/modules/linear_model.html#logistic-
regression
n_iter_i = _check_optimize_result(

LogisticRegression(multi_class='multinomial')

test_lables = np.array(test_data['label'])
test_features = np.array(test_data.drop('label', axis=1))
predictions = model.predict(test_features)
# Accuracy of the model
accuracy_score(test_lables, predictions)

0.9541067200874158

# Confusion Matrix
confusion_matrix(test_lables, predictions)

array([[226, 0, 0, 0, 7, 0, 0, 0, 0, 0, 0, 0,
0,
1, 0, 0, 0, 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, 0, 0, 0,
0,
0, 214, 3, 0, 0, 0, 0, 0, 0, 0, 1],
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0,
0, 2, 281, 0, 1, 0, 0, 0, 0, 7, 0],
[ 0, 0, 0, 1, 0, 0, 0, 0, 6, 2, 0, 0,
0,
0, 0, 1, 240, 4, 0, 9, 0, 0, 0, 0],
[ 1, 0, 0, 0, 1, 0, 0, 3, 7, 0, 0, 0,
1,
0, 0, 0, 0, 207, 0, 0, 3, 0, 0, 0],
[ 0, 0, 0, 0, 0, 0, 8, 0, 1, 0, 0, 0,
0,
1, 0, 0, 0, 0, 220, 0, 0, 0, 0, 0],
[ 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0,
0,
0, 0, 0, 5, 0, 0, 217, 3, 0, 0, 0],
[ 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 1, 0,
0,
0, 0, 0, 7, 0, 0, 2, 205, 1, 0, 0],
[ 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0,
0,
0, 0, 3, 1, 0, 0, 3, 1, 232, 0, 0],
[ 0, 0, 0, 4, 0, 1, 8, 8, 5, 0, 6, 0,
0,
0, 0, 0, 11, 0, 0, 0, 0, 1, 196, 0],
[ 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0,
0,
0, 0, 0, 0, 4, 0, 0, 0, 0, 0, 205]],
dtype=int64)

f1_score(test_lables, predictions, average=None)

array([0.97835498, 0.99484536, 0.997921 , 0.96263736, 0.97282609,


0.94779116, 0.95475113, 0.95711061, 0.91743119, 0.93453725,
0.95934959, 0.96613995, 0.97767857, 0.98081023, 0.97940503,
0.96563574, 0.89552239, 0.93877551, 0.95032397, 0.93939394,
0.94470046, 0.95473251, 0.85964912, 0.98321343])

#decision tree
tree_model = DecisionTreeClassifier(max_depth = 20)
tree_model.fit(features, labels)

DecisionTreeClassifier(max_depth=20)

tree_predictions = tree_model.predict(test_features)
accuracy_score(test_lables, tree_predictions)

0.8614095793116008

f1_score(test_lables, tree_predictions, average=None)

array([0.90336134, 0.88831169, 0.92946058, 0.86652079, 0.87608069,


0.87169043, 0.89749431, 0.89841986, 0.87218045, 0.84301075,
0.84164859, 0.84164859, 0.85972851, 0.91189427, 0.91375291,
0.92699491, 0.76571429, 0.83561644, 0.77099237, 0.80349345,
0.7860262 , 0.8 , 0.90677966, 0.88151659])

#knn classifier
knn_model = KNeighborsClassifier(n_neighbors = 24)
knn_model.fit(features, labels)

KNeighborsClassifier(n_neighbors=24)

accuracy = knn_model.score(test_features, test_lables)


print(accuracy)

0.9630304134037516
knn_predictions = knn_model.predict(test_features)
print(f"accuracy: {accuracy_score(test_lables, knn_predictions)}")
print(f"F1-Score: {f1_score(test_lables, knn_predictions,
average=None)}")

accuracy: 0.9630304134037516
F1-Score: [0.98275862 0.95049505 0.99170124 0.96746204 0.98360656
0.98373984
0.97706422 0.97052154 0.96666667 0.95343681 0.9874477 0.95280899
0.96017699 0.98706897 0.99770115 0.99655172 0.91053678 0.95842451
0.9375 0.91313131 0.89156627 0.95560254 0.95560254 0.97831325]

You might also like