1
0
mirror of https://github.com/gsi-upm/sitc synced 2024-11-12 18:02:28 +00:00
sitc/ml2/3_7_SVM.ipynb

1179 lines
123 KiB
Plaintext
Raw Normal View History

2016-03-28 12:03:08 +00:00
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"![](images/EscUpmPolit_p.gif \"UPM\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Course Notes for Learning Intelligent Systems"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Department of Telematic Engineering Systems, Universidad Politécnica de Madrid, © 2016 Carlos A. Iglesias"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## [Introduction to Machine Learning II](3_0_0_Intro_ML_2.ipynb)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Introduction SVM "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"In this notebook we are going to train a classifier with the preprocessed Titanic dataset. \n",
"\n",
"We are going to use the dataset we obtained in the [pandas munging notebook](3_3_Data_Munging_with_Pandas.ipynb) for simplicity. You can try some of the techniques learnt in the previous notebook."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Load and clean"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"# General import and load data\n",
"import pandas as pd\n",
"import numpy as np\n",
"\n",
"from pandas import Series, DataFrame\n",
"\n",
"# Training and test spliting\n",
"from sklearn.cross_validation import train_test_split\n",
"from sklearn import preprocessing\n",
"\n",
"# Estimators\n",
"from sklearn.svm import SVC\n",
"\n",
"# Evaluation\n",
"from sklearn import metrics\n",
"from sklearn.cross_validation import cross_val_score, KFold, StratifiedKFold\n",
"from sklearn.metrics import classification_report\n",
"from sklearn.metrics import roc_curve\n",
"from sklearn.metrics import roc_auc_score\n",
"\n",
"# Optimization\n",
"from sklearn.grid_search import GridSearchCV\n",
"\n",
"# Visualisation\n",
"import seaborn as sns\n",
"import matplotlib.pyplot as plt\n",
"sns.set(color_codes=True)\n",
"\n",
"\n",
"# if matplotlib is not set inline, you will not see plots\n",
"#alternatives auto gtk gtk2 inline osx qt qt5 wx tk\n",
"#%matplotlib auto\n",
"#%matplotlib qt\n",
"%matplotlib inline\n",
"%run plot_learning_curve"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>PassengerId</th>\n",
" <th>Survived</th>\n",
" <th>Pclass</th>\n",
" <th>Sex</th>\n",
" <th>Age</th>\n",
" <th>SibSp</th>\n",
" <th>Parch</th>\n",
" <th>Fare</th>\n",
" <th>Embarked</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>1</td>\n",
" <td>0</td>\n",
" <td>3</td>\n",
" <td>0</td>\n",
" <td>22.0</td>\n",
" <td>1</td>\n",
" <td>0</td>\n",
" <td>7.2500</td>\n",
" <td>0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>2</td>\n",
" <td>1</td>\n",
" <td>1</td>\n",
" <td>1</td>\n",
" <td>38.0</td>\n",
" <td>1</td>\n",
" <td>0</td>\n",
" <td>71.2833</td>\n",
" <td>1</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>3</td>\n",
" <td>1</td>\n",
" <td>3</td>\n",
" <td>1</td>\n",
" <td>26.0</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>7.9250</td>\n",
" <td>0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>4</td>\n",
" <td>1</td>\n",
" <td>1</td>\n",
" <td>1</td>\n",
" <td>35.0</td>\n",
" <td>1</td>\n",
" <td>0</td>\n",
" <td>53.1000</td>\n",
" <td>0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>5</td>\n",
" <td>0</td>\n",
" <td>3</td>\n",
" <td>0</td>\n",
" <td>35.0</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>8.0500</td>\n",
" <td>0</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" PassengerId Survived Pclass Sex Age SibSp Parch Fare Embarked\n",
"0 1 0 3 0 22.0 1 0 7.2500 0\n",
"1 2 1 1 1 38.0 1 0 71.2833 1\n",
"2 3 1 3 1 26.0 0 0 7.9250 0\n",
"3 4 1 1 1 35.0 1 0 53.1000 0\n",
"4 5 0 3 0 35.0 0 0 8.0500 0"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"#We get a URL with raw content (not HTML one)\n",
2016-03-29 09:21:43 +00:00
"url=\"https://raw.githubusercontent.com/gsi-upm/sitc/master/ml2/data-titanic/train.csv\"\n",
2016-03-28 12:03:08 +00:00
"df = pd.read_csv(url)\n",
"df.head()\n",
"\n",
"\n",
"#Fill missing values\n",
"df['Age'].fillna(df['Age'].mean(), inplace=True)\n",
"df['Sex'].fillna('male', inplace=True)\n",
"df['Embarked'].fillna('S', inplace=True)\n",
"\n",
"# Encode categorical variables\n",
"df['Age'] = df['Age'].fillna(df['Age'].median())\n",
"df.loc[df[\"Sex\"] == \"male\", \"Sex\"] = 0\n",
"df.loc[df[\"Sex\"] == \"female\", \"Sex\"] = 1\n",
"df.loc[df[\"Embarked\"] == \"S\", \"Embarked\"] = 0\n",
"df.loc[df[\"Embarked\"] == \"C\", \"Embarked\"] = 1\n",
"df.loc[df[\"Embarked\"] == \"Q\", \"Embarked\"] = 2\n",
"\n",
"# Drop colums\n",
"df.drop(['Cabin', 'Ticket', 'Name'], axis=1, inplace=True)\n",
"\n",
"#Show proprocessed df\n",
"df.head()"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"PassengerId int64\n",
"Survived int64\n",
"Pclass int64\n",
"Sex object\n",
"Age float64\n",
"SibSp int64\n",
"Parch int64\n",
"Fare float64\n",
"Embarked object\n",
"dtype: object"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"#Check types are numeric\n",
"df.dtypes"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We have still two columns as objects, so we change the type."
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"PassengerId int64\n",
"Survived int64\n",
"Pclass int64\n",
"Sex int64\n",
"Age float64\n",
"SibSp int64\n",
"Parch int64\n",
"Fare float64\n",
"Embarked int64\n",
"dtype: object"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df['Sex'] = df['Sex'].astype(np.int64)\n",
"df['Embarked'] = df['Embarked'].astype(np.int64)\n",
"df.dtypes"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"PassengerId False\n",
"Survived False\n",
"Pclass False\n",
"Sex False\n",
"Age False\n",
"SibSp False\n",
"Parch False\n",
"Fare False\n",
"Embarked False\n",
"dtype: bool"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"#Check there are not missing values\n",
"df.isnull().any()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Train and test splitting"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We use the same techniques we applied in the Iris dataset. \n",
"\n",
"Nevertheless, we need to remove the column 'Survived' "
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"# Features of the model\n",
"features = ['Pclass', 'Sex', 'Age', 'SibSp', 'Parch', 'Fare', 'Embarked']\n",
"# Transform dataframe in numpy arrays\n",
"X = df[features].values\n",
"y = df['Survived'].values\n",
"\n",
"\n",
"\n",
"# Test set will be the 25% taken randomly\n",
"X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25, random_state=33)\n",
"\n",
"# Preprocess: normalize\n",
"#scaler = preprocessing.StandardScaler().fit(X_train)\n",
"#X_train = scaler.transform(X_train)\n",
"#X_test = scaler.transform(X_test)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Define model"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"\n",
"types_of_kernels = ['linear', 'rbf', 'poly']\n",
"\n",
"kernel = types_of_kernels[0]\n",
"gamma = 3.0\n",
"\n",
"# Create kNN model\n",
"model = SVC(kernel=kernel, probability=True, gamma=gamma)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Train and evaluate"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"#This step will take some time \n",
"# Train - This is not needed if you use K-Fold\n",
"\n",
"model.fit(X_train, y_train)\n",
"\n",
"predicted = model.predict(X_test)\n",
"expected = y_test"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"0.81165919282511212"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Accuracy\n",
"metrics.accuracy_score(expected, predicted)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Ok, we get around 82% of accuracy! (results depend on the splitting)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Null accuracy"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We can evaluate the accuracy if the model always predict the most frequent class, following this [refeference](http://blog.kaggle.com/2015/10/23/scikit-learn-video-9-better-evaluation-of-classification-models/)."
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"0 134\n",
"1 89\n",
"dtype: int64"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Count number of samples per class\n",
"s_y_test = Series(y_test)\n",
"s_y_test.value_counts()"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"0.3991031390134529"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Mean of ones\n",
"y_test.mean()"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"0.60089686098654704"
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Mean of zeros\n",
"1 - y_test.mean() \n"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"0.60089686098654704"
]
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Calculate null accuracy (binary classification coded as 0/1)\n",
"max(y_test.mean(), 1 - y_test.mean())"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"0 0.600897\n",
"dtype: float64"
]
},
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Calculate null accuracy (multiclass classification)\n",
"s_y_test.value_counts().head(1) / len(y_test)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"So, since our accuracy was 0.82 is better than the null accuracy."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Confussion matrix and F-score"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We can obtain more information from the confussion matrix and the metric F1-score.\n",
"In a confussion matrix, we can see:\n",
"\n",
"||**Predicted**: 0| **Predicted: 1**|\n",
"|---------------------------|\n",
"|**Actual: 0**| TN | FP |\n",
"|**Actual: 1**| FN|TP|\n",
"\n",
"* **True negatives (TN)**: actual negatives that were predicted as negatives\n",
"* **False positives (FP)**: actual negatives that were predicted as positives\n",
"* **False negatives (TN)**: actual positives that were predicted as negatives\n",
"* **True negatives (TN)**: actual positives that were predicted as posiives\n",
"\n",
"We can calculate several metrics from the confussion matrix\n",
"\n",
"* **Recall** (also called *sensitivity*): when the actual value is positive, how often the prediction is correct? \n",
"(TP / (TP + FN))\n",
"* **Specificity**: when the actual value is negative, how often the prediction is correct? (TN / (TN + FP))\n",
"* **False Positive Rate**: when the actual value is negative, how often the prediction is incorrect? (FP / (TN + FP))\n",
"* **Precision**: when a positive value is predicted, how many times is correct? (TP / (TP + FP)\n",
"A good metric is F1-score: 2TP / (2TP + FP + FN)"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[115 19]\n",
" [ 23 66]]\n"
]
}
],
"source": [
"# Confusion matrix\n",
"print(metrics.confusion_matrix(expected, predicted))"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
" precision recall f1-score support\n",
"\n",
" 0 0.83 0.86 0.85 134\n",
" 1 0.78 0.74 0.76 89\n",
"\n",
"avg / total 0.81 0.81 0.81 223\n",
"\n"
]
}
],
"source": [
"# Report\n",
"print(classification_report(expected, predicted))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## ROC (Receiver Operating Characteristic ) and AUC (Area Under the Curve)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The [ROC](https://en.wikipedia.org/wiki/Receiver_operating_characteristic) curve illustrates the performance of a binary classifier system as its discrimination threshold is varied."
]
},
{
"cell_type": "code",
"execution_count": 25,
"metadata": {
"collapsed": false,
"scrolled": true
},
"outputs": [
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAXkAAAEZCAYAAABy91VnAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAH+pJREFUeJzt3XmYXFWd//F39ZJOp9PZSAMJSwhLvoJAZJMkLAF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K9uYi1r\n1nZ/QTVFirKyVL/KIjIcKeSlS6+va+T/3bi4INsqL0tRXpZ7mKTy8rIe1xGR3lPIS5cakhb8vruN\nxfbs33tBp+yiNxKJlIpCXnawxFfz0LLXaEye2HzLlPGccezeJS6ViPSVQl528OATK/nbSxsAKEul\n2ENPbIoMaQr5yK3e2Nyri6dNyYXS718ym/LyVLcvhhaRoUEhH7FV65uYe8Njvf5eeVmKyooy3c0i\nEgGFfMQ2NW0BwpC20/bI/+LpnruMVsCLREIhPwzsP2U8Z87ep9TFEJESUIeriEjE1JKPyLr6lu1v\nJAJ4Y11TCUsjIoOBQj4Sqzc08aV5XV9kLVf/usiw1WPIm1kKuB6YDrQA57n7iqzlZwOXAG3Aze7+\ngwEqq+SwqTG04KdOqmW/rHeKlpenOHb65FIVS0RKLJ+W/Bygyt1nmdmRwDXJvIyrgf2BJuCvZna7\nu9cXvqiSjwP2mqCLrCKyXT4XXo8G7gVw98XA4Z2WPwmMB6qT6XTBSiciIv2ST0t+DJDdMm8zszJ3\nb0+mnwGWAJuBX7r7wLwVWXawoaF1h4usqzboIquIvFk+Ib8JyB5GcHvAm9lBwCnAFKAR+ImZnenu\nd+baYF2dRiXM6EtdvLGukc9fv5B0F+dMtaNHDtn6HarlHgiqiw6qi/7JJ+QXAqcCd5jZDGB51rJ6\nQl98q7unzWw1oesmpzV
"text/plain": [
"<matplotlib.figure.Figure at 0x7f97c0ef4390>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"y_pred_prob = model.predict_proba(X_test)[:,1]\n",
"fpr, tpr, thresholds = roc_curve(y_test, y_pred_prob)\n",
"plt.plot(fpr, tpr)\n",
"plt.xlim([0.0, 1.0])\n",
"plt.ylim([0.0, 1.0])\n",
"plt.title('ROC curve for Titanic')\n",
"plt.xlabel('False Positive Rate (1 - Recall)')\n",
"plt.xlabel('True Positive Rate (Sensitivity)')\n",
"plt.grid(True)"
]
},
{
"cell_type": "code",
"execution_count": 26,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"array([ 0.74750054, 0.74312762, 0.74298741, 0.73808718, 0.73799308,\n",
" 0.73743733, 0.73736981, 0.73735128, 0.73729214, 0.73709628,\n",
" 0.73699794, 0.73675548, 0.73659304, 0.73639721, 0.73623377,\n",
" 0.73612635, 0.73607305, 0.73572436, 0.7356707 , 0.735536 ,\n",
" 0.73544523, 0.73407999, 0.73200457, 0.7316892 , 0.73139765,\n",
" 0.73080287, 0.20382799, 0.20324215, 0.20255542, 0.202325 ,\n",
" 0.19998395, 0.19993953, 0.19986688, 0.19983705, 0.19891076,\n",
" 0.19881374, 0.19872727, 0.19868889, 0.1986448 , 0.19860251,\n",
" 0.19851757, 0.19851517, 0.19851124, 0.19850688, 0.19843776,\n",
" 0.19841942, 0.19831147, 0.19830402, 0.19816605, 0.19815391,\n",
" 0.19813555, 0.19813539, 0.19803009, 0.19801409, 0.19800118,\n",
" 0.1978783 , 0.19785132, 0.19784528, 0.19783312, 0.19782026,\n",
" 0.19780287, 0.19776301, 0.19774832, 0.19770726, 0.19759125,\n",
" 0.19756794, 0.197232 , 0.19720558, 0.1971321 , 0.197085 ,\n",
" 0.19652697, 0.19651513, 0.19193059, 0.18794571])"
]
},
"execution_count": 26,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"#Threshold used by the decision function, thresholds[0] is the number of \n",
"thresholds"
]
},
{
"cell_type": "code",
"execution_count": 29,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"array([<matplotlib.axes._subplots.AxesSubplot object at 0x7f97bea20710>,\n",
" <matplotlib.axes._subplots.AxesSubplot object at 0x7f97be73c8d0>], dtype=object)"
]
},
"execution_count": 29,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAYQAAAEICAYAAABfz4NwAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAElpJREFUeJzt3XmQpHV9x/H3zG4vx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f8BxoBjM/Pqct7ltO81SJJGRE+FEBF/\nBNyemV+nXQb7zpoD1vYXTZI0SL0+h3AGsBgRLwCeAXwOaC45fxKY6WRQsznZY4QHrFt3cFf7j68a\no9mcpNFodHW5KrIOYmZdc0cp6ygapeNr1urmzs5OdLX/+vUTtf2feiqE8jwBABGxFTgT+OuIOCEz\nrwJOBrZ2Mmt6eq6XCA8yM7Nr+Z2WWFxoMT0911UhNJuTlWSte2Zdc0ct6ygapeNr1urm7tixs+v9\nu7nubtZDX68y2sdbgfMjogFcD1xS4WxJUs36LoTMPHHJ5sZ+50mShsM3pkmSAAtBklRYCJIkwEKQ\nJBUWgiQJsBAkSYWFIEkCLARJUmEhSJIAC0GSVFgIkiTAQpAkFRaCJAmwECRJhYUgSQIsBElSYSFI\nkgALQZJUWAiSJMBCkCQVFoIkCbAQJEmFhSBJAiwESVJhIUiSAAtBklSsHnYASdWbn5/nne/7OIes\nfyx79swvu//uXbOc+4ZXsG7d+gGk00plIUj7oVarxe27DuL2xuM62n/vru3s3r275lRa6XoqhIhY\nDVwEPBFYA7wH+C/gYmAR2JaZm6qJKEkahF6fQ3gl8H+ZeQLwIuDjwBZgc2ZuAMYj4rSKMkqSBqDX\nQvgC8K7y9SpgHjg2M68up10OnNRnNknSAPX0kFFm7gKIiEngn4B3AB9cssscsLbvdJKkgen5SeWI\neALwz8DHM/MfIuKvlpw
"text/plain": [
"<matplotlib.figure.Figure at 0x7f97c0ef4c88>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"#Histogram of probability vs actual\n",
"dprob = pd.DataFrame(data = {'probability':y_pred_prob, 'actual':y_test})\n",
"dprob.probability.hist(by=dprob.actual, sharex=True, sharey=True)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"ROC curve helps to select a threshold to balance sensitivity and recall."
]
},
{
"cell_type": "code",
"execution_count": 30,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"#Function to evaluate thresholds of the ROC curve\n",
"def evaluate_threshold(threshold):\n",
" print('Sensitivity:', tpr[thresholds > threshold][-1])\n",
" print('Recall:', 1 - fpr[thresholds > threshold][-1])"
]
},
{
"cell_type": "code",
"execution_count": 31,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Sensitivity: 0.0786516853933\n",
"Recall: 0.992537313433\n"
]
}
],
"source": [
"evaluate_threshold(0.74)"
]
},
{
"cell_type": "code",
"execution_count": 32,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Sensitivity: 0.741573033708\n",
"Recall: 0.880597014925\n"
]
}
],
"source": [
"evaluate_threshold(0.5)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"By default, the thresdhold to decide a class is 0.5, If we modify it, we should use the new thresdhold.\n",
"\n",
"threshold = 0.8\n",
"\n",
"predicted = model.predict_proba(X) > threshold"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"AUC is the percentage of the ROC plot underneath the curve. Represents the likelihood that the predictor assigns a higher predicted probability to the positive observation. A simple rule to evaluate a classifier based on this summary value is the following:\n",
"* .90-1 = very good (A)\n",
"* .80-.90 = good (B)\n",
"* .70-.80 = not so good (C)\n",
"* .60-.70 = poor (D)\n",
"* .50-.60 = fail (F)"
]
},
{
"cell_type": "code",
"execution_count": 26,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0.799890994466\n"
]
}
],
"source": [
"# AUX\n",
"print(roc_auc_score(expected, predicted))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Train and Evaluate with K-Fold"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"This is alternative to splitting the dataset into train and test. It will run k times slower than the other method, but it will be more accurate."
]
},
{
"cell_type": "code",
"execution_count": 33,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Scores in every iteration [ 0.81564246 0.80337079 0.78089888 0.73595506 0.80337079]\n",
"Accuracy: 0.79 (+/- 0.06)\n"
]
}
],
"source": [
"# This step will take some time\n",
"# Cross-validation\n",
"cv = KFold(X.shape[0], n_folds=5, shuffle=False, random_state=33)\n",
"# StratifiedKFold has is a variation of k-fold which returns stratified folds:\n",
"# each set contains approximately the same percentage of samples of each target class as the complete set.\n",
"#cv = StratifiedKFold(y, n_folds=3, shuffle=False, random_state=33)\n",
"scores = cross_val_score(model, X, y, cv=cv)\n",
"print(\"Scores in every iteration\", scores)\n",
"print(\"Accuracy: %0.2f (+/- %0.2f)\" % (scores.mean(), scores.std() * 2))\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We get 78% of success with K-Fold, quite good!"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We can plot the [learning curve](http://scikit-learn.org/stable/auto_examples/model_selection/plot_learning_curve.html). The traning scores decreases with the number of samples. The cross-validation reaches the training score at the end. It seems we will not get a better result with more samples."
]
},
{
"cell_type": "code",
"execution_count": 35,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"<module 'matplotlib.pyplot' from '/home/cif/anaconda3/lib/python3.5/site-packages/matplotlib/pyplot.py'>"
]
},
"execution_count": 35,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAY8AAAEZCAYAAABvpam5AAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAIABJREFUeJzs3XecXFX9//HXuXdmtmVLyqaHIO1AQhJKKCKCoIhIgKgo\n+AXpVbBhoYgK+gX9CV9EQg0dFFAEQUFAOooYekvChxJKIL3tZuuUe39/3Lu7s7uzZXZ3dmZ3P08f\nmJk79945meze95x6je/7KKWUUtlw8l0ApZRSQ4+Gh1JKqaxpeCillMqahodSSqmsaXgopZTKmoaH\nUkqprEXyXQA19FlrpwNvikh5Ht77QuAdEfnDYL93vlhrHwB+JCJvWWsfAb4pIhuste8DXxORl3s4\n/kl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jZan+NyKqWUysJgjbZqGRN/PEGT\nVJmI3BBOTjqBYMLSe8DJ4YSx3wD7E8wXOFdEHuvufbTmoZRS2etPzWNYzPPQ8FBKqewVcrOVUkqp\nYUjDQymlVNY0PJRSSmVNw0MppVTWNDyUUkplTZdkV2qEqn3+v2x48AHiK1cQmzSZMQfPo2L3PXs+\nsAtXXnk5IkvZsGE9TU1NTJkylaqq0fzyl7/u8dh33nmbZ599huOOOynj64sWPceaNas55JD5GV9X\ng0+H6io1AtU+/19WLby20/aJp5zWrwABeOihB/joow859dQzet5Z5VV/hupqzUOpYWjt3Xex+cUX\nunw9uWlTxu2rbryedffcnfG18rm7Uf31IzO+1p1XXnmJa65ZQCwW49BDv0IsFuPee+8mlUphjOHi\niy/hvffe5b777uHCCy/myCO/wuzZO/HRRx8yZsxYLrrotzz88IN8+OEHzJ//NS644KdMmDCBjz/+\nmB12mMmPfnQONTWbuPDC80kkEkybtgUvv/wid93119YyxONxfv7zc6ivr6epqYlTTvk2u+22Bw88\ncB/33Xcvnuex9977cMIJp/DPfz7E3XffSSxWxNSp0/jxj8/j0Ucf5sEH/4bv+5x44qnU1GziT3+6\nA9d1mT17pxEZlBoeSo1EqS5u497V9n5KJOIsXHgLALfffguXXPJ7ioqKuOSSi1m06DnGjavGmOBL\n8MqVK7jyyoWMG1fNt799EkuXLgZoff3jjz/i8suvJhaLccQR89m4cQN/+MMt7LPP55g//3BeeGER\nL7zwfLv3/+STj6mpqeH//m8BGzduYPnyj9i4cSN/+MNt3H77n4hGo1x33VWsWrWKm25ayC233Elx\ncTELFvyO+++/l9LSUsrLK/j1ry+ltraWb3/7JG688XaKior41a9+zosvPs/cubvn5LMrVBoeSg1D\n1V8/sttawge/OJ/4Jx932h6bOo0tL/jVgJdniy2mtz4ePbqKiy66gOLiYpYv/5Add2x/R9+qqirG\njasGoLp6PPF4+7sST5kyjeLiYgDGjh1Hc3OcDz74gIMOCm4tP2dO51uwf+pTW3HooV/hggvOI5lM\ncfjhR7BixSdsvfXWRKNRAE499QzeemsJn/rU1q3nnzNnZ154YREzZsxs/Tt88slyNm3ayI9//D18\n36exsZFPPvlYw0MpNfyNOXhexj6PMV8+OCfvZ0wwsLO+vo4bb1zIvfc+iO/7/OAH/Wvuaemz3Xrr\nrXnzzdfYZpttefPN1zvtt2zZuzQ0NPDb317O+vXrOP30E7n++lv58MMPSSaTRCIRzj//bM488wd8\n8MEympubKCoq5tVXX2LatC0AcJzg7zBp0hQmTJjI7353Fa7r8tBDD7DttrZff4+hSMNDqRGopVN8\nwz8ebBtt9eWD+91Z3pOyslHMnj2HU045jkjEpby8knXr1jJx4qS0vdr6cFuaqtKlb2t5fNRRx/Kr\nX/2cJ598nLFjxxGJuO2
"text/plain": [
"<matplotlib.figure.Figure at 0x7f4a65d42278>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"plot_learning_curve(model, \"Learning curve with K-Fold\", X, y, cv=cv)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Train and Optimize"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"In this section we are going to provide an alternative version of the previous one with optimization"
]
},
{
"cell_type": "code",
"execution_count": 42,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0.811659192825\n"
]
}
],
"source": [
"#Tune parameters\n",
"gammas = np.logspace(-6, -1, 10)\n",
"gs = GridSearchCV(model, param_grid=dict(gamma=gammas))\n",
"gs.fit(X_train, y_train)\n",
"scores = gs.score(X_test, y_test)\n",
"print(scores)"
]
},
{
"cell_type": "code",
"execution_count": 45,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"<module 'matplotlib.pyplot' from '/home/cif/anaconda3/lib/python3.5/site-packages/matplotlib/pyplot.py'>"
]
},
"execution_count": 45,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAY8AAAEZCAYAAABvpam5AAAABHNCSVQICAgIfAhkiA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xx9/FNd12WuvvTn55NN57rlneOih+4lEihg3bjy//OXFPP/8LJ5++h94nscpp5xBbe0G\nHnzwPhzHYerUHQdlotTkodRglOjiybZdre+lWCzKjBkzAbj33plcddVfKCoq4qqrrmT27DcYMaIa\ny/JvgpcvX8YNN8xgxIhqzj77VBYsmAfQun3p0sVce+1NRCIRjj76cNavX8df/zqTvff+BocffiRv\nvTWbt956s937f/nlUmpra/nTn65n/fp1LFmymPXr1/PXv97Dvfc+SDgc5tZbb2TFihXceecMZs68\nn+LiYq6//s888cSjlJaWUl5ewR/+cDV1dXWcffap3HHHvRQVFfH73/+GOXPeZPr0r+bkuytUmjyU\nGoCqjzpmk6WERb+9lOiXSzutj4wbzxaX/T7r8UyYMLH19dChVVxxxWUUFxezZMkX7LDD1Hb7VlVV\nMWJENQDV1SOJRqPtto8dO57i4mIAhg8fQUtLlEWLFnHggYcAMG3aTp3e/ytf2ZJDDz2Cyy67mHg8\nwZFHHs2yZV+y1VZbEQ6HATjjjHP46KP5fOUrW7Wef9q0nXjrrdlMmjS59TN8+eUSNmxYzy9/+RM8\nz6OpqYkvv1yqyUMpNfANO+jgtG0ew757UJq9e8+y/I6dDQ313HHHDB599Gk8z+NnP+tddU+yzXar\nrbbiww/fZ+utt+HDDz/otN/ChZ/S2NjIH/94LWvXruGss07httvu5osvviAejxMKhbj00gs499yf\nsWjRQlpamikqKua9995
"text/plain": [
"<matplotlib.figure.Figure at 0x7f4a65a12b00>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"# Refine model\n",
"model = SVC(kernel='linear', gamma=gs.best_estimator_.gamma)\n",
"plot_learning_curve(model, \"optimized with GridSearch\", X, y, cv=cv)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Visualise"
]
},
{
"cell_type": "code",
"execution_count": 36,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAXIAAAEKCAYAAAAPVd6lAAAABHNCSVQICAgIf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pZk5taI+Djw5xEB8HBmfqdDvUqSGpg1yDOzCnxyyuKn6+YfBN7d3rYkSa3y\nA0GSVDiDXJIKZ5BLUuEMckkqnEEuSYUzyCWpcAa5JBXOIJekwhnkklQ4g1ySCmeQS1LhDHJJKpxB\nLkmFM8glqXCtfB/5Ue25415lT3+14VzjpfPrTdlTpdMbeJM+r3P0hvrpws+uVUfb8wnNezru1a62\nUX6QP3bCCSw69NP5bkOSXjO0YGlXt1d8kL+ydC2vLD21A2s+ig5F3pCjrf/O9zPnI7Y5t3S0Padw\ndPY0V0fhPlSOwp5mUHyQV3v65rsFSZpXvtkpSYUzyCWpcAa5JBXOIJekwhnkklQ4g1ySCmeQS1Lh\nDHJJKpxBLkmFM8glqXAGuSQVziCXpMIZ5JJUuFm//TAiKsDtwFnAIeC6zNzVoO4bwJ7M/ELbu5Qk\nNdXKEfkmoD8z1wNbgFunFkTEJ4DT29ybJKkFrQT5BuABgMx8BFhXPxkRFwDnAd9oe3eSpFm1EuTL\ngH1147GI6AGIiBOA3wd+l6PyNh+SdOxr5Q5Bw8BA3bgnM8cn/vuDwCrgfuBtwOKIeCoz/7S9bUqS\nmmklyLcDVwHfiojzgZ2TE5n5NeBrABHxMSAMcUnqrlaC/B5gY0RsnxhfGxGbgSWZubVzrUmSWjFr\nkGdmFfjklMVPN6j7z+1qSpLUOj8QJEmFM8glqXAGuSQVziCXpMIZ5JJUOINckgpnkEtS4QxySSqc\nQS5JhTPIJalwBrkkFc4
"text/plain": [
"<matplotlib.figure.Figure at 0x7f4a65c13588>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"<matplotlib.figure.Figure at 0x7f4a65d21e10>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAXIAAAEKCAYAAAAPVd6lAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAIABJREFUeJzt3X1wHPd93/H37t4jDgeAB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/+vAx4AQ+JK19q/aVKuchRjH7pGI/QOO8nyI\n98oc4yeLcLxQjnD4VAcu6nY57eNcTx7AlbO3mq6Ve4C8tfYW4H7ggZPWfwZ4N3Ab8HFjjMZDrSF1\nH47kHesaHrcc6d0QB3i5FFMLoDrwFuKgR1vjLmb9xKMMTj/T7UpkDVlNkN8GPAxgrX0M2HHS+qeA\ndUBxfrk3O19TqhgnAX7TRECuR7tTIBk3/kI5Gaky18Ot8Uw4TRA31CKXE6wmyIeAqUXLoTFm8c89\nAzwB7AG+Ya2dbmF90gKDkdezfeILbDkm9GGudHHvnY6/SK4xAUCjB09ykrO3miCfBsqLf8ZaGwMY\nY64C3gdsBd4KbDLG/HKri5TViXHEffiBaCrjeGUgOZBbLb6l2+W0Va5xBAe6ZJ2cYDVBvgt4L4Ax\n5iaSlveCKWAOqFtrHXCIpJtFOmw2cOzaEGHLvXVRiJU4HE+PRODB62M7wOvdEbVe3CTTnCLMjPT0\npw45c6sZfvgQcKcxZtf88n3GmHuBkrV2pzHmi8D3jDF14KfAX7enVFmKw/HqQHKiT+RDKUzu66VZ\nDE/n5VLMZM5Ry2/u+VZqEFZwXkAjr4my5ESec539KH748ExLN/jJjVe18ulSZS5wPDUccaTgyMRw\n1VTABdXebZGebC5wPDo
"text/plain": [
"<matplotlib.figure.Figure at 0x7f4a65bc4588>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAXIAAAEKCAYAAAAPVd6lAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAIABJREFUeJzt3X9wJOdd5/F39/T0jH5rpdWu7Y1jOxg/68TGsr0+ex0n\nseMf4RKnboECypALmBgHKCB1SSjOqUqRgjoqXIgpJyEkuaV8x+WOHJXDoYDguCAXSDbEBodNtoz9\nOMExwevdlVa7+q2Z6R/P/dEj7aw0Wmm9MyO19HlVqUbdPZr5tjT6zDNPP92P55xDRETyy9/oAkRE\n5M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fAe1cDgLb7W6z3CO\nQjLX9CzTavliquWLN6AoOV/bLsgdjsSj6bjpF7sTnhlIWd4rcvlc8+uJvHrO55IFn+6YpifdSPtV\nfMd4yTFeyrpLqg0fhOJC71J4R+GOpqMttisvjShXXqJr/t/wXMLEzjfq95NjWzrIZwLHy10p8wXH\nQgEW6reXzntcN7Vy18tpdnJMdwLdsUd34tEdQ98qLeyu1MsGcEvH1LyspX2y5DgZpmeN6iklUCld\nTC0cJgqHSQvljSt0kyrEM3TNf59y5WU8Uhw+lfIleC7BKchzK/dB/r2ehAS4ssmBwdnA8XzfmaQt\nJdlp6F1J82C+qOJzUUXdH5tJ7GX93CfDLLynimcuQJUNDdxBFA5TC4e3zxUEL0DvjCWMJkj8Lha6\nLqXStQfnh2v/oGxquQ/y6SA7Nf3KuZXbhmoe+08WKCfZ9bDV/bH5xV42q9Biq3uyeOZMSs9BFA5S\nK2Yt7qg4sH2HBr5Cc70
"text/plain": [
"<matplotlib.figure.Figure at 0x7f4a65bc4550>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"# Plot with standard configuration of SVM\n",
"%run plot_svm\n",
"plot_svm(df)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Any value in the blue survived while anyone in the read did not. Checkout the graph for the linear transformation. It created its decision boundary right on 50%! "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# References"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"* [Titanic Machine Learning from Disaster](https://www.kaggle.com/c/titanic/forums/t/5105/ipython-notebook-tutorial-for-titanic-machine-learning-from-disaster)\n",
"* [API SVC scikit-learn](http://scikit-learn.org/stable/modules/generated/sklearn.svm.SVC.html)\n",
"* [Better evaluation of classification models](http://blog.kaggle.com/2015/10/23/scikit-learn-video-9-better-evaluation-of-classification-models/)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Licence"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The notebook is freely licensed under under the [Creative Commons Attribution Share-Alike license](https://creativecommons.org/licenses/by/2.0/). \n",
"\n",
"© 2016 Carlos A. Iglesias, Universidad Politécnica de Madrid."
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"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.5.1+"
}
},
"nbformat": 4,
"nbformat_minor": 0
}