{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "58d7cd40",
   "metadata": {},
   "source": [
    "# Support Vector Machine (SVM)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "3e527e6f",
   "metadata": {},
   "outputs": [],
   "source": [
    "#Uses linear SVM. After this, try various kernels (eg polynomial, radial basis, etc) \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)] # petal length, petal width\n",
    "y = (iris[\"target\"]  == 2). astype( np.float64) # Iris virginica \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]]) \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": 10,
   "id": "7043913d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[98  2]\n",
      " [ 4 46]]\n",
      "              precision    recall  f1-score   support\n",
      "\n",
      "         0.0      0.961     0.980     0.970       100\n",
      "         1.0      0.958     0.920     0.939        50\n",
      "\n",
      "    accuracy                          0.960       150\n",
      "   macro avg      0.960     0.950     0.955       150\n",
      "weighted avg      0.960     0.960     0.960       150\n",
      "\n",
      "0.96\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": "code",
   "execution_count": 13,
   "id": "e8341871",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[98  2]\n",
      " [ 4 46]]\n",
      "              precision    recall  f1-score   support\n",
      "\n",
      "         0.0      0.961     0.980     0.970       100\n",
      "         1.0      0.958     0.920     0.939        50\n",
      "\n",
      "    accuracy                          0.960       150\n",
      "   macro avg      0.960     0.950     0.955       150\n",
      "weighted avg      0.960     0.960     0.960       150\n",
      "\n",
      "0.96\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\"][:, (2, 3)] # petal length, petal width\n",
    "y = (iris[\"target\"] == 2). 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 = 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": 14,
   "id": "44e054ae",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[98  2]\n",
      " [ 4 46]]\n",
      "              precision    recall  f1-score   support\n",
      "\n",
      "         0.0      0.961     0.980     0.970       100\n",
      "         1.0      0.958     0.920     0.939        50\n",
      "\n",
      "    accuracy                          0.960       150\n",
      "   macro avg      0.960     0.950     0.955       150\n",
      "weighted avg      0.960     0.960     0.960       150\n",
      "\n",
      "0.96\n"
     ]
    }
   ],
   "source": [
    "rbf_kernel_svm_clf = Pipeline([ (\"scaler\", StandardScaler()), (\"svm_clf\", SVC( kernel =\"rbf\", gamma = 5, C = 0.001)) ])\n",
    "rbf_kernel_svm_clf.fit( X, y)\n",
    "y_pred = 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": "e8f87c03",
   "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
}
