From bd1064a665bb8484041b00a163223db6d777a4f1 Mon Sep 17 00:00:00 2001 From: Jesus5657 Date: Thu, 12 Dec 2024 09:50:11 -0400 Subject: [PATCH] lab-svm --- .ipynb_checkpoints/lab-svm-checkpoint.ipynb | 531 ++++++++++++++++++++ lab-svm.ipynb | 10 +- 2 files changed, 538 insertions(+), 3 deletions(-) create mode 100644 .ipynb_checkpoints/lab-svm-checkpoint.ipynb diff --git a/.ipynb_checkpoints/lab-svm-checkpoint.ipynb b/.ipynb_checkpoints/lab-svm-checkpoint.ipynb new file mode 100644 index 0000000..ce4e913 --- /dev/null +++ b/.ipynb_checkpoints/lab-svm-checkpoint.ipynb @@ -0,0 +1,531 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# SVM (Support Vector Machines)\n", + "\n", + "Estimated time needed: **15-30** minutes\n", + "\n", + "## Objectives\n", + "\n", + "After completing this lab you will be able to:\n", + "\n", + "* Use scikit-learn to Support Vector Machine to classify\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "In this notebook, you will use SVM (Support Vector Machines) to build and train a model using human cell records, and classify cells to whether the samples are benign or malignant.\n", + "\n", + "SVM works by mapping data to a high-dimensional feature space so that data points can be categorized, even when the data are not otherwise linearly separable. A separator between the categories is found, then the data is transformed in such a way that the separator could be drawn as a hyperplane. Following this, characteristics of new data can be used to predict the group to which a new record should belong.\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "

Table of contents

\n", + "\n", + "
\n", + "
    \n", + "
  1. Load the Cancer data
  2. \n", + "
  3. Modeling
  4. \n", + "
  5. Evaluation
  6. \n", + "
  7. Practice
  8. \n", + "
\n", + "
\n", + "
\n", + "
\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "!pip install scikit-learn==0.23.1" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "import piplite\n", + "await piplite.install(['pandas'])\n", + "await piplite.install(['matplotlib'])\n", + "await piplite.install(['numpy'])\n", + "await piplite.install(['scikit-learn'])\n", + "await piplite.install(['scipy'])\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "import pandas as pd\n", + "import pylab as pl\n", + "import numpy as np\n", + "import scipy.optimize as opt\n", + "from sklearn import preprocessing\n", + "from sklearn.model_selection import train_test_split\n", + "%matplotlib inline \n", + "import matplotlib.pyplot as plt" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "from pyodide.http import pyfetch\n", + "\n", + "async def download(url, filename):\n", + " response = await pyfetch(url)\n", + " if response.status == 200:\n", + " with open(filename, \"wb\") as f:\n", + " f.write(await response.bytes())\n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "button": false, + "new_sheet": false, + "run_control": { + "read_only": false + } + }, + "source": [ + "

Load the Cancer data

\n", + "The example is based on a dataset that is publicly available from the UCI Machine Learning Repository (Asuncion and Newman, 2007)[http://mlearn.ics.uci.edu/MLRepository.html]. The dataset consists of several hundred human cell sample records, each of which contains the values of a set of cell characteristics. The fields in each record are:\n", + "\n", + "| Field name | Description |\n", + "| ----------- | --------------------------- |\n", + "| ID | Clump thickness |\n", + "| Clump | Clump thickness |\n", + "| UnifSize | Uniformity of cell size |\n", + "| UnifShape | Uniformity of cell shape |\n", + "| MargAdh | Marginal adhesion |\n", + "| SingEpiSize | Single epithelial cell size |\n", + "| BareNuc | Bare nuclei |\n", + "| BlandChrom | Bland chromatin |\n", + "| NormNucl | Normal nucleoli |\n", + "| Mit | Mitoses |\n", + "| Class | Benign or malignant |\n", + "\n", + "
\n", + "
\n", + "\n", + "For the purposes of this example, we're using a dataset that has a relatively small number of predictors in each record. To download the data, we will use `!wget` to download it from IBM Object Storage." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "button": false, + "new_sheet": false, + "run_control": { + "read_only": false + } + }, + "outputs": [], + "source": [ + "#Click here and press Shift+Enter\n", + "path=\"https://cf-courses-data.s3.us.cloud-object-storage.appdomain.cloud/IBMDeveloperSkillsNetwork-ML0101EN-SkillsNetwork/labs/Module%203/data/cell_samples.csv\"" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "button": false, + "new_sheet": false, + "run_control": { + "read_only": false + } + }, + "source": [ + "## Load Data From CSV File\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "await download(path, \"cell_samples.csv\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "button": false, + "new_sheet": false, + "run_control": { + "read_only": false + } + }, + "outputs": [], + "source": [ + "cell_df = pd.read_csv(\"cell_samples.csv\")\n", + "cell_df.head()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "The ID field contains the patient identifiers. The characteristics of the cell samples from each patient are contained in fields Clump to Mit. The values are graded from 1 to 10, with 1 being the closest to benign.\n", + "\n", + "The Class field contains the diagnosis, as confirmed by separate medical procedures, as to whether the samples are benign (value = 2) or malignant (value = 4).\n", + "\n", + "Let's look at the distribution of the classes based on Clump thickness and Uniformity of cell size:\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "ax = cell_df[cell_df['Class'] == 4][0:50].plot(kind='scatter', x='Clump', y='UnifSize', color='DarkBlue', label='malignant');\n", + "cell_df[cell_df['Class'] == 2][0:50].plot(kind='scatter', x='Clump', y='UnifSize', color='Yellow', label='benign', ax=ax);\n", + "plt.show()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Data pre-processing and selection\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Let's first look at columns data types:\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "cell_df.dtypes" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "It looks like the **BareNuc** column includes some values that are not numerical. We can drop those rows:\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "cell_df = cell_df[pd.to_numeric(cell_df['BareNuc'], errors='coerce').notnull()]\n", + "cell_df['BareNuc'] = cell_df['BareNuc'].astype('int')\n", + "cell_df.dtypes" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "feature_df = cell_df[['Clump', 'UnifSize', 'UnifShape', 'MargAdh', 'SingEpiSize', 'BareNuc', 'BlandChrom', 'NormNucl', 'Mit']]\n", + "X = np.asarray(feature_df)\n", + "X[0:5]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "We want the model to predict the value of Class (that is, benign (=2) or malignant (=4)). As this field can have one of only two possible values, we need to change its measurement level to reflect this.\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "cell_df['Class'] = cell_df['Class'].astype('int')\n", + "y = np.asarray(cell_df['Class'])\n", + "y [0:5]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Train/Test dataset\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "We split our dataset into train and test set:\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "X_train, X_test, y_train, y_test = train_test_split( X, y, test_size=0.2, random_state=4)\n", + "print ('Train set:', X_train.shape, y_train.shape)\n", + "print ('Test set:', X_test.shape, y_test.shape)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "

Modeling (SVM with Scikit-learn)

\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "The SVM algorithm offers a choice of kernel functions for performing its processing. Basically, mapping data into a higher dimensional space is called kernelling. The mathematical function used for the transformation is known as the kernel function, and can be of different types, such as:\n", + "\n", + "```\n", + "1.Linear\n", + "2.Polynomial\n", + "3.Radial basis function (RBF)\n", + "4.Sigmoid\n", + "```\n", + "\n", + "Each of these functions has its characteristics, its pros and cons, and its equation, but as there's no easy way of knowing which function performs best with any given dataset. We usually choose different functions in turn and compare the results. Let's just use the default, RBF (Radial Basis Function) for this lab.\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "from sklearn import svm\n", + "clf = svm.SVC(kernel='rbf')\n", + "clf.fit(X_train, y_train) " + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "After being fitted, the model can then be used to predict new values:\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "yhat = clf.predict(X_test)\n", + "yhat [0:5]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "

Evaluation

\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "from sklearn.metrics import classification_report, confusion_matrix\n", + "import itertools" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "def plot_confusion_matrix(cm, classes,\n", + " normalize=False,\n", + " title='Confusion matrix',\n", + " cmap=plt.cm.Blues):\n", + " \"\"\"\n", + " This function prints and plots the confusion matrix.\n", + " Normalization can be applied by setting `normalize=True`.\n", + " \"\"\"\n", + " if normalize:\n", + " cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis]\n", + " print(\"Normalized confusion matrix\")\n", + " else:\n", + " print('Confusion matrix, without normalization')\n", + "\n", + " print(cm)\n", + "\n", + " plt.imshow(cm, interpolation='nearest', cmap=cmap)\n", + " plt.title(title)\n", + " plt.colorbar()\n", + " tick_marks = np.arange(len(classes))\n", + " plt.xticks(tick_marks, classes, rotation=45)\n", + " plt.yticks(tick_marks, classes)\n", + "\n", + " fmt = '.2f' if normalize else 'd'\n", + " thresh = cm.max() / 2.\n", + " for i, j in itertools.product(range(cm.shape[0]), range(cm.shape[1])):\n", + " plt.text(j, i, format(cm[i, j], fmt),\n", + " horizontalalignment=\"center\",\n", + " color=\"white\" if cm[i, j] > thresh else \"black\")\n", + "\n", + " plt.tight_layout()\n", + " plt.ylabel('True label')\n", + " plt.xlabel('Predicted label')" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# Compute confusion matrix\n", + "cnf_matrix = confusion_matrix(y_test, yhat, labels=[2,4])\n", + "np.set_printoptions(precision=2)\n", + "\n", + "print (classification_report(y_test, yhat))\n", + "\n", + "# Plot non-normalized confusion matrix\n", + "plt.figure()\n", + "plot_confusion_matrix(cnf_matrix, classes=['Benign(2)','Malignant(4)'],normalize= False, title='Confusion matrix')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "You can also easily use the **f1\\_score** from sklearn library:\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "from sklearn.metrics import f1_score\n", + "f1_score(y_test, yhat, average='weighted') " + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Let's try the jaccard index for accuracy:\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "from sklearn.metrics import jaccard_score\n", + "jaccard_score(y_test, yhat,pos_label=2)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "

Practice

\n", + "Can you rebuild the model, but this time with a __linear__ kernel? You can use __kernel='linear'__ option, when you define the svm. How the accuracy changes with the new kernel function?\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "clf2 = svm.SVC(kernel='linear')\n", + "clf2.fit(X_train, y_train) \n", + "yhat2 = clf2.predict(X_test)\n", + "print(\"Avg F1-score: %.4f\" % f1_score(y_test, yhat2, average='weighted'))\n", + "print(\"Jaccard score: %.4f\" % jaccard_score(y_test, yhat2,pos_label=2))" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "
Click here for the solution\n", + "\n", + "```python\n", + "clf2 = svm.SVC(kernel='linear')\n", + "clf2.fit(X_train, y_train) \n", + "yhat2 = clf2.predict(X_test)\n", + "print(\"Avg F1-score: %.4f\" % f1_score(y_test, yhat2, average='weighted'))\n", + "print(\"Jaccard score: %.4f\" % jaccard_score(y_test, yhat2,pos_label=2))\n", + "\n", + "```\n", + "\n", + "
\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Thank you for completing this lab!" + ] + } + ], + "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": 4 +} diff --git a/lab-svm.ipynb b/lab-svm.ipynb index bbbfd6b..ce4e913 100644 --- a/lab-svm.ipynb +++ b/lab-svm.ipynb @@ -48,7 +48,7 @@ "metadata": {}, "outputs": [], "source": [ - "#!pip install scikit-learn==0.23.1" + "!pip install scikit-learn==0.23.1" ] }, { @@ -474,7 +474,11 @@ "metadata": {}, "outputs": [], "source": [ - "# write your code here\n" + "clf2 = svm.SVC(kernel='linear')\n", + "clf2.fit(X_train, y_train) \n", + "yhat2 = clf2.predict(X_test)\n", + "print(\"Avg F1-score: %.4f\" % f1_score(y_test, yhat2, average='weighted'))\n", + "print(\"Jaccard score: %.4f\" % jaccard_score(y_test, yhat2,pos_label=2))" ] }, { @@ -519,7 +523,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.11.8" + "version": "3.12.7" } }, "nbformat": 4,