{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "2cd87358-176a-4801-ba14-938ea40faa1d",
   "metadata": {},
   "source": [
    "# CFAR 10 Dataset"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "bc5ccd54-499e-4195-851f-c27273925dd3",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Downloading data from https://www.cs.toronto.edu/~kriz/cifar-10-python.tar.gz\n",
      "\u001b[1m170498071/170498071\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m21s\u001b[0m 0us/step\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "from tensorflow.keras.datasets import cifar10\n",
    "from sklearn.tree import DecisionTreeClassifier\n",
    "from sklearn.model_selection import train_test_split\n",
    "from sklearn.metrics import accuracy_score\n",
    "\n",
    "# Load dataset\n",
    "(X_train, y_train), (X_test, y_test) = cifar10.load_data()\n",
    "\n",
    "# Flatten images from 32x32x3 to 1D (3072 features)\n",
    "X_train = X_train.reshape(X_train.shape[0], -1)\n",
    "X_test = X_test.reshape(X_test.shape[0], -1)\n",
    "\n",
    "# Convert labels to 1D array\n",
    "y_train = y_train.flatten()\n",
    "y_test = y_test.flatten()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "fe5f515e-e659-4fb9-9edc-f219e9805dad",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Train a Decision Tree Classifier\n",
    "#=================================\n",
    "\n",
    "# Create Decision Tree model\n",
    "dt_model = DecisionTreeClassifier(random_state=42, max_depth=20)\n",
    "\n",
    "# Train the model\n",
    "dt_model.fit(X_train, y_train)\n",
    "\n",
    "# Make predictions\n",
    "y_pred = dt_model.predict(X_test)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "3bd05eda-f3ba-4630-af3d-6aaa0de6b119",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[371  53  89  55  88  43  38  55 138  70]\n",
      " [ 78 293  68  60  72  65  48  62 100 154]\n",
      " [105  36 209  86 171  98 131  90  38  36]\n",
      " [ 67  66 103 168 112 130 143  97  54  60]\n",
      " [ 55  38 172  72 260  87 157  88  34  37]\n",
      " [ 47  55 114 161 113 218 110  86  46  50]\n",
      " [ 31  48 111 115 154 101 293  75  36  36]\n",
      " [ 61  74 100  97 126 103  93 216  50  80]\n",
      " [145  88  55  59  54  58  35  52 351 103]\n",
      " [103 151  69  65  60  53  60  79  98 262]]\n",
      "              precision    recall  f1-score   support\n",
      "\n",
      "           0      0.349     0.371     0.360      1000\n",
      "           1      0.325     0.293     0.308      1000\n",
      "           2      0.192     0.209     0.200      1000\n",
      "           3      0.179     0.168     0.173      1000\n",
      "           4      0.215     0.260     0.235      1000\n",
      "           5      0.228     0.218     0.223      1000\n",
      "           6      0.264     0.293     0.278      1000\n",
      "           7      0.240     0.216     0.227      1000\n",
      "           8      0.371     0.351     0.361      1000\n",
      "           9      0.295     0.262     0.278      1000\n",
      "\n",
      "    accuracy                          0.264     10000\n",
      "   macro avg      0.266     0.264     0.264     10000\n",
      "weighted avg      0.266     0.264     0.264     10000\n",
      "\n"
     ]
    }
   ],
   "source": [
    "from sklearn import metrics\n",
    "# Display the metrics of the model's classification through confusion matrix and classification report\n",
    "# (accuracy / precision / recall / f1)\n",
    "print(metrics.confusion_matrix(y_test, y_pred))\n",
    "\n",
    "print(metrics.classification_report(y_test, y_pred, digits=3))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0379495a-27e5-41fc-ab40-75d0de55a642",
   "metadata": {},
   "source": [
    "**How to Improve**\n",
    "\n",
    "- Feature Selection: Reduce the number of features using PCA before applying the classifiers.\n",
    "- Hyperparameter Tuning: Use GridSearchCV or RandomizedSearchCV. Also algorithm specific e.g., for Decisiontree, the best max_depth, min_samples_split, etc.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "02a9c81a-0424-44dc-8c88-68fd39283088",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "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.12.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
