# Import necessary libraries
from sklearn.model_selection import train_test_split
from sklearn.neighbors import KNeighborsClassifier
from sklearn.metrics import accuracy_score, classification_report
from sklearn.datasets import fetch_openml
import numpy as np
# Load the MNIST Fashion dataset using scikit-learn's built-in datasets
fashion_mnist = fetch_openml(name="Fashion-MNIST", as_frame=False, parser='auto')
# Split the dataset into features (X) and labels (y)
X = fashion_mnist.data.astype('float32') / 255.0  # Normalize pixel values to [0, 1]
y = fashion_mnist.target.astype('int')
# Split the dataset into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

# Initialize the KNN classifier
knn_classifier = KNeighborsClassifier(n_neighbors=3)  # You can adjust the number of neighbors as needed

# Train the classifier
knn_classifier.fit(X_train, y_train)

# Make predictions on the test set
y_pred = knn_classifier.predict(X_test)

# Evaluate the classifier
accuracy = accuracy_score(y_test, y_pred)
print(f"Accuracy: {accuracy:.4f}")

# Print classification report
print("Classification Report:")
print(classification_report(y_test, y_pred))
