text_plain_output_1.png
from sklearn.metrics import classification_report, confusion_matrix

from sklearn.model_selection import GridSearchCV

from sklearn.neighbors import KNeighborsClassifier

from sklearn.pipeline import make_pipeline

from sklearn.preprocessing import StandardScaler

import matplotlib.pyplot as plt

import pandas as pd

import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)

import random

import random

import seaborn as sns

data = pd.read_csv('/kaggle/input/fashionmnist/fashion-mnist_train.csv')

import random

classes = ['T-shirt', 'Trouser', 'Pullover', 'Dress', 'Coat', 'Sandal', 'Shirt', 'Sneaker', 'Bag', 'A-boot']
samples_per_class = 10
num_classes = len(classes)

fig, axs = plt.subplots(samples_per_class, num_classes, figsize=(10, 10))
fig.subplots_adjust(hspace=0.5)

for i in range(samples_per_class):
    for j in range(num_classes):
        cls = classes[j]
        ind = random.choice((y_train == j).nonzero()[0])
        img = X_train[ind].reshape((28, 28))
        axs[i, j].imshow(img, cmap=plt.cm.gray)
        axs[i, j].axis('off')
        if i == 0:
            axs[i, j].set_title(cls)

plt.show()

pipe = make_pipeline(StandardScaler(), KNeighborsClassifier())

param_grid = {'kneighborsclassifier__n_neighbors': range(1, 30)}

cv = GridSearchCV(pipe, param_grid=param_grid, cv=5)
cv.fit(X_train, y_train)

mean_scores = cv.cv_results_['mean_test_score']

y_pred = cv.predict(X_test)
print(classification_report(y_test, y_pred))
print(confusion_matrix(y_test, y_pred))