Professional Documents
Culture Documents
Image Classification
Image Classification
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
<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
import numpy as np
features = np.array(features)
labels = np.array(labels)
features.shape, labels.shape
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.
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, 0, 0, 0, 0, 0, 0, 0],
[ 0, 193, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1,
0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0],
[ 0, 0, 240, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[ 0, 0, 0, 219, 0, 0, 0, 0, 0, 0, 2, 0,
0,
0, 0, 0, 0, 0, 0, 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, 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)
#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
#knn classifier
knn_model = KNeighborsClassifier(n_neighbors = 24)
knn_model.fit(features, labels)
KNeighborsClassifier(n_neighbors=24)
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]