{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "c1bf31c4",
   "metadata": {},
   "source": [
    "# Support Vector Machine (SVM)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "ca037d5a",
   "metadata": {},
   "outputs": [],
   "source": [
    "#Uses linear SVM. After this, try various kernels (eg polynomial, radial basis, etc - see below) \n",
    "import numpy as np\n",
    "from sklearn import datasets\n",
    "from sklearn.pipeline import Pipeline\n",
    "from sklearn.preprocessing import StandardScaler\n",
    "from sklearn.svm import LinearSVC\n",
    "iris = datasets.load_iris()\n",
    "X = iris[\"data\"][:, (2, 3)] # use only 2 features -  petal length, petal width\n",
    "y = (iris[\"target\"]  == 2). astype( np.float64) # Iris virginica \n",
    "#X = iris[\"data\"][:, (0,1,2, 3)] # use all features\n",
    "#y = (iris[\"target\"]). astype( np.float64) # All classes of iris \n",
    "svm_clf = Pipeline([ (\"scaler\", StandardScaler()),\n",
    "                    (\"linear_svc\", LinearSVC( C = 1, loss =\"hinge\")), ])\n",
    "svm_clf.fit( X, y)\n",
    "\n",
    "#svm_clf.predict([[ 5.5, 1.7]]) #what class, whose petal length is 5.5cm and petal width is 1.7cm, does this iris belong to?\n",
    "y_pred = svm_clf.predict(X) \n",
    "\n",
    "# see Géron, Aurélien. Hands-On Machine Learning with Scikit-Learn, Keras, and TensorFlow (pp. 155-156). O'Reilly Media. Kindle Edition. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "53f14d1a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[97  3]\n",
      " [ 4 46]]\n",
      "              precision    recall  f1-score   support\n",
      "\n",
      "         0.0      0.960     0.970     0.965       100\n",
      "         1.0      0.939     0.920     0.929        50\n",
      "\n",
      "    accuracy                          0.953       150\n",
      "   macro avg      0.950     0.945     0.947       150\n",
      "weighted avg      0.953     0.953     0.953       150\n",
      "\n",
      "0.9533333333333334\n"
     ]
    }
   ],
   "source": [
    "# Print the confusion matrix\n",
    "from sklearn import metrics\n",
    "print(metrics.confusion_matrix(y, y_pred))\n",
    "\n",
    "# Print the precision and recall, among other metrics\n",
    "print(metrics.classification_report(y, y_pred, digits=3))\n",
    "print(metrics.accuracy_score(y, y_pred))\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6090c6e9",
   "metadata": {},
   "source": [
    "# This uses sklearn's general SVM with various kernels - see Geron"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "5765d827",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[98  2]\n",
      " [ 1 49]]\n",
      "              precision    recall  f1-score   support\n",
      "\n",
      "         0.0      0.990     0.980     0.985       100\n",
      "         1.0      0.961     0.980     0.970        50\n",
      "\n",
      "    accuracy                          0.980       150\n",
      "   macro avg      0.975     0.980     0.978       150\n",
      "weighted avg      0.980     0.980     0.980       150\n",
      "\n",
      "0.98\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "from sklearn import datasets\n",
    "from sklearn.pipeline import Pipeline\n",
    "from sklearn.preprocessing import StandardScaler\n",
    "from sklearn.svm import SVC\n",
    "iris = datasets.load_iris()\n",
    "X = iris[\"data\"][:, (0,1,2, 3)] # all features\n",
    "y = (iris[\"target\"] == 2). astype( np.float64) # Iris virginica\n",
    "#X = iris[\"data\"][:, (2, 3)] # petal length, petal width\n",
    "#y = (iris[\"target\"]). astype( np.float64) # Iris virginica\n",
    "\n",
    "\n",
    "poly_kernel_svm_clf = Pipeline([ (\"scaler\", StandardScaler()), (\"svm_clf\", SVC( kernel =\"poly\", degree = 3, coef0 = 1, C = 5)) ])\n",
    "poly_kernel_svm_clf.fit( X, y)\n",
    "y_pred = poly_kernel_svm_clf.predict(X) \n",
    "\n",
    "# Print the confusion matrix\n",
    "from sklearn import metrics\n",
    "print(metrics.confusion_matrix(y, y_pred))\n",
    "\n",
    "# Print the precision and recall, among other metrics\n",
    "print(metrics.classification_report(y, y_pred, digits=3))\n",
    "print(metrics.accuracy_score(y, y_pred))\n",
    "\n",
    "#Géron, Aurélien. Hands-On Machine Learning with Scikit-Learn, Keras, and TensorFlow (pp. 155-156). O'Reilly Media. Kindle Edition. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "c8ce20f9",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[50  0  0]\n",
      " [ 0 49  1]\n",
      " [ 0  0 50]]\n",
      "              precision    recall  f1-score   support\n",
      "\n",
      "         0.0      1.000     1.000     1.000        50\n",
      "         1.0      1.000     0.980     0.990        50\n",
      "         2.0      0.980     1.000     0.990        50\n",
      "\n",
      "    accuracy                          0.993       150\n",
      "   macro avg      0.993     0.993     0.993       150\n",
      "weighted avg      0.993     0.993     0.993       150\n",
      "\n",
      "0.9933333333333333\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "from sklearn import datasets\n",
    "from sklearn.pipeline import Pipeline\n",
    "from sklearn.preprocessing import StandardScaler\n",
    "from sklearn.svm import SVC\n",
    "iris = datasets.load_iris()\n",
    "X = iris[\"data\"][:, (0,1,2, 3)] # all features\n",
    "y = (iris[\"target\"]).astype(np.float64) # Iris virginica\n",
    "#X = iris[\"data\"][:, (2, 3)] # petal length, petal width\n",
    "#y = (iris[\"target\"]). astype( np.float64) # Iris virginica\n",
    "rbf_kernel_svm_clf = Pipeline([ (\"scaler\", StandardScaler()), (\"svm_clf\", SVC( kernel =\"rbf\", gamma = 5, C = 0.5)) ])\n",
    "rbf_kernel_svm_clf.fit( X, y)\n",
    "y_pred = rbf_kernel_svm_clf.predict(X) \n",
    "\n",
    "# Print the confusion matrix\n",
    "from sklearn import metrics\n",
    "print(metrics.confusion_matrix(y, y_pred))\n",
    "\n",
    "# Print the precision and recall, among other metrics\n",
    "print(metrics.classification_report(y, y_pred, digits=3))\n",
    "print(metrics.accuracy_score(y, y_pred))\n",
    "\n",
    "#Géron, Aurélien. Hands-On Machine Learning with Scikit-Learn, Keras, and TensorFlow (p. 160). O'Reilly Media. Kindle Edition. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "fd0b3bec",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ba57f706",
   "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.9.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
