{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "DA42OJmJ1PUv"
      },
      "outputs": [],
      "source": [
        "import pandas as pd\n",
        "import numpy as np\n",
        "import matplotlib.pyplot as plt\n",
        "from sklearn.model_selection import train_test_split"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "GOMh84gPs2Ba"
      },
      "outputs": [],
      "source": [
        "\n",
        "# Load data for Hand-written digits and split data (train:test = 7:3)\n",
        "data = pd.read_csv('/content/sample_data/mnist_test.csv')\n",
        "data = np.array(data)\n",
        "total_samples = 10000\n",
        "train_ratio = 0.7\n",
        "X = data[:total_samples, 1:] / 255\n",
        "y = data[:total_samples, 0]\n",
        "\n",
        "# Splitting data into train and test sets\n",
        "X_train, X_test, Y_train, Y_test = train_test_split(X, y, test_size=(1 - train_ratio), random_state=42)\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 255
        },
        "id": "JOlBCHQZs2Bb",
        "outputId": "b44e7955-bf0e-47d7-8866-ec7d5a15e7fc"
      },
      "outputs": [
        {
          "output_type": "error",
          "ename": "NameError",
          "evalue": "ignored",
          "traceback": [
            "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
            "\u001b[0;31mNameError\u001b[0m                                 Traceback (most recent call last)",
            "\u001b[0;32m<ipython-input-3-487ec6b951ad>\u001b[0m in \u001b[0;36m<cell line: 11>\u001b[0;34m()\u001b[0m\n\u001b[1;32m      9\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     10\u001b[0m \u001b[0;31m# Calculate confusion matrix\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 11\u001b[0;31m \u001b[0mcf\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mconfusion_matrix\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mY_test\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mY_test_pred\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m     12\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     13\u001b[0m \u001b[0;31m# Calculate precision, recall, and f1-score manually\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
            "\u001b[0;31mNameError\u001b[0m: name 'confusion_matrix' is not defined"
          ]
        }
      ],
      "source": [
        "\n",
        "from sklearn.svm import SVC\n",
        "\n",
        "# Train (X_train, Y_train) using SVM\n",
        "svm_digit = SVC(kernel='sigmoid', gamma=0.001, coef0=1)\n",
        "svm_digit.fit(X_train, Y_train)\n",
        "\n",
        "# Predict on test data\n",
        "Y_test_pred = svm_digit.predict(X_test)\n",
        "\n",
        "# Calculate confusion matrix\n",
        "cf = confusion_matrix(Y_test, Y_test_pred)\n",
        "\n",
        "# Calculate precision, recall, and f1-score manually\n",
        "tp = np.diag(cf)\n",
        "fp = np.sum(cf, axis=0) - tp\n",
        "fn = np.sum(cf, axis=1) - tp\n",
        "\n",
        "precision = np.mean(tp / (tp + fp))\n",
        "recall = np.mean(tp / (tp + fn))\n",
        "f1_score = 2 * (precision * recall) / (precision + recall)\n",
        "\n",
        "# Calculate accuracy using the built-in method of the SVM model\n",
        "accuracy = svm_digit.score(X_test, Y_test)\n",
        "\n",
        "print('Test accuracy: %.2f%%' % (accuracy * 100))\n",
        "print('Test precision: %.2f%%' % (precision * 100))\n",
        "print('Test recall: %.2f%%' % (recall * 100))\n",
        "print('Test f1-score: %.2f%%' % (f1_score * 100))"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "0lgvWAacs2Bd"
      },
      "outputs": [],
      "source": [
        "# Check the performance (accuracy, precision, recall, f1-score) of your trained SVM for (X_test, Y_test)\n",
        "y_test_pred = svm_digit.predict(X_test)\n",
        "accuracy = (np.sum(Y_test == y_test_pred).astype(float) / X_test.shape[0])\n",
        "\n",
        "\n",
        "\n",
        "print('Test accuracy: %.2f%%' % (accuracy))\n",
        "print('Test precision: %.2f%%' % (precision))\n",
        "print('Test recall: %.2f%%' % (recall))\n",
        "print('Test f1-score: %.2f%%' % (f1_score))"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "w31DByoWs2Be"
      },
      "outputs": [],
      "source": [
        "# Check the confusion matrix\n",
        "from sklearn.metrics import confusion_matrix, ConfusionMatrixDisplay\n",
        "cf = confusion_matrix(Y_test, y_test_pred)\n",
        "print(cf)\n",
        "disp = ConfusionMatrixDisplay.from_predictions(Y_test , y_test_pred)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "Gfmhtghvs2Bg"
      },
      "outputs": [],
      "source": []
    }
  ],
  "metadata": {
    "colab": {
      "provenance": []
    },
    "kernelspec": {
      "display_name": "ML4ME",
      "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.8.18"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}