{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "cfa58c45",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "from sklearn.model_selection import train_test_split\n",
    "from sklearn.preprocessing import StandardScaler\n",
    "from sklearn.neighbors import KNeighborsClassifier\n",
    "from sklearn.tree import DecisionTreeClassifier\n",
    "from sklearn.metrics import classification_report, confusion_matrix\n",
    "# needed to load seaborn to get confusion matrix to load\n",
    "import seaborn as sns\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "# Load the training dataset\n",
    "# the r is used to make the access line easier\n",
    "train_data = pd.read_csv(r'fashion-mnist_train.csv')\n",
    "\n",
    "# Load the test dataset\n",
    "test_data = pd.read_csv(r'fashion-mnist_test.csv')\n",
    "\n",
    "# Split the training data into features and labels\n",
    "X_train_full = train_data.drop('label', axis=1)\n",
    "y_train_full = train_data['label']\n",
    "\n",
    "# Split the test data into features and labels\n",
    "X_test = test_data.drop('label', axis=1)\n",
    "y_test = test_data['label']\n",
    "\n",
    "# Split the training data again into training and validation sets (10% of the training data for validation)\n",
    "# 10% as per the recommendation \n",
    "X_train, X_val, y_train, y_val = train_test_split(X_train_full, y_train_full, test_size=0.1, random_state=42)\n",
    "\n",
    "# Standardize the features\n",
    "# normalisation of the datase\n",
    "scaler = StandardScaler()\n",
    "X_train_scaled = scaler.fit_transform(X_train)\n",
    "X_val_scaled = scaler.transform(X_val)\n",
    "X_test_scaled = scaler.transform(X_test)\n",
    "\n",
    "# Function to evaluate and print classification reports for a classifier\n",
    "def evaluate_classifier(classifier, X_train, y_train, X_val, y_val, X_test, y_test, classifier_name):\n",
    "    classifier.fit(X_train, y_train)\n",
    "\n",
    "    # Evaluate on training set\n",
    "    y_train_pred = classifier.predict(X_train)\n",
    "    print(f\"--- {classifier_name} - Training Set Evaluation ---\")\n",
    "    print(classification_report(y_train, y_train_pred))\n",
    "\n",
    "    # Evaluate on validation set\n",
    "    y_val_pred = classifier.predict(X_val)\n",
    "    print(f\"--- {classifier_name} - Validation Set Evaluation ---\")\n",
    "    print(classification_report(y_val, y_val_pred))\n",
    "\n",
    "    # Evaluate on test set\n",
    "    y_test_pred = classifier.predict(X_test)\n",
    "    print(f\"--- {classifier_name} - Test Set Evaluation ---\")\n",
    "    print(classification_report(y_test, y_test_pred))\n",
    "\n",
    "    # Plot confusion matrix for the test set\n",
    "    conf_matrix = confusion_matrix(y_test, y_test_pred)\n",
    "    sns.heatmap(conf_matrix, annot=True, fmt='d', cmap='Blues', xticklabels=['Class 0', 'Class 1'], yticklabels=['Class 0', 'Class 1'])\n",
    "    plt.title(f\"Confusion Matrix - {classifier_name}\")\n",
    "    plt.show()\n",
    "\n",
    "# Implement and evaluate K-Nearest Neighbors Classifier\n",
    "knn_classifier = KNeighborsClassifier(n_neighbors=5)\n",
    "evaluate_classifier(knn_classifier, X_train_scaled, y_train, X_val_scaled, y_val, X_test_scaled, y_test, \"K-Nearest Neighbors\")\n",
    "\n",
    "# Implement and evaluate Decision Tree Classifier\n",
    "tree_classifier = DecisionTreeClassifier(random_state=42)\n",
    "evaluate_classifier(tree_classifier, X_train_scaled, y_train, X_val_scaled, y_val, X_test_scaled, y_test, \"Decision Tree\")\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
