Newer
Older
{
"cells": [
{
"cell_type": "code",
"source": [
"# IGNORE THIS CELL WHICH CUSTOMIZES LAYOUT AND STYLING OF THE NOTEBOOK !\n",
"%matplotlib inline\n",
"%config InlineBackend.figure_format = 'retina'\n",
"import warnings\n",
"\n",
"import matplotlib.pyplot as plt\n",
"\n",
"warnings.filterwarnings(\"ignore\", category=FutureWarning)\n",
"warnings.filterwarnings(\"ignore\", message=\"X does not have valid feature names, but [a-zA-Z]+ was fitted with feature names\", category=UserWarning)\n",
" \n",
"warnings.filterwarnings = lambda *a, **kw: None\n",
"from IPython.core.display import HTML\n",
"\n",
"HTML(open(\"custom.html\", \"r\").read())"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"!pip install pandas scikit-learn\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Chapter 6: An overview of classifiers, Part 1\n",
"\n",
"<span style=\"font-size: 150%;\">Nearest Neighbors and linear-based methods</span>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"This script gives a quick hands-on overview of **how different types of classifiers work, their advantages and their disadvantages**. This should give you an idea of a concept behind each classifier type as well as when and which classifier type to use.\n",
"\n",
"For the sake of visualisation we continue with 2 dimensional data examples. For different classifiers we'll be looking at their decision surfaces. Let's start with some helper functions for that:"
]
},
{
"cell_type": "code",
"execution_count": null,
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
"metadata": {},
"outputs": [],
"source": [
"import matplotlib\n",
"import matplotlib.pyplot as plt\n",
"import numpy as np\n",
"\n",
"\n",
"def samples_color(ilabels, colors=[\"steelblue\", \"chocolate\"]):\n",
" \"\"\"Return colors list from labels list given as indices.\"\"\"\n",
" return [colors[int(i)] for i in ilabels]\n",
"\n",
"\n",
"def plot_decision_surface(\n",
" features_2d,\n",
" labels,\n",
" classifier,\n",
" preprocessing=None,\n",
" plt=plt,\n",
" marker=\".\",\n",
" N=100,\n",
" alpha=0.2,\n",
" colors=[\"steelblue\", \"chocolate\"],\n",
" title=None,\n",
" test_features_2d=None,\n",
" test_labels=None,\n",
" test_s=60,\n",
"):\n",
" \"\"\"Plot a 2D decision surface for a already trained classifier.\"\"\"\n",
"\n",
" # sanity check\n",
" assert len(features_2d.columns) == 2\n",
"\n",
" # pandas to numpy array; get min/max values\n",
" xy = np.array(features_2d)\n",
" min_x, min_y = xy.min(axis=0)\n",
" max_x, max_y = xy.max(axis=0)\n",
"\n",
" # create mesh of NxN points; tech: `N*1j` is spec for including max value\n",
" XX, YY = np.mgrid[min_x : max_x : N * 1j, min_y : max_y : N * 1j]\n",
" points = np.c_[XX.ravel(), YY.ravel()] # shape: (N*N)x2\n",
" # points = pd.DataFrame(points, columns=[\"x\", \"y\"])\n",
"\n",
" # apply scikit-learn API preprocessing\n",
" if preprocessing is not None:\n",
" points = preprocessing.transform(points)\n",
"\n",
" # classify grid points\n",
" classes = classifier.predict(points)\n",
"\n",
" # plot classes color mesh\n",
" ZZ = classes.reshape(XX.shape) # shape: NxN\n",
" plt.pcolormesh(\n",
" XX,\n",
" YY,\n",
" ZZ,\n",
" alpha=alpha,\n",
" cmap=matplotlib.colors.ListedColormap(colors),\n",
" shading=\"auto\",\n",
" )\n",
" # plot points\n",
" plt.scatter(\n",
" xy[:, 0],\n",
" xy[:, 1],\n",
" marker=marker,\n",
" color=samples_color(labels, colors=colors),\n",
" )\n",
" # set title\n",
" if title:\n",
" if hasattr(plt, \"set_title\"):\n",
" plt.set_title(title)\n",
" else:\n",
" plt.title(title)\n",
" # plot test points\n",
" if test_features_2d is not None:\n",
" assert test_labels is not None\n",
" assert len(test_features_2d.columns) == 2\n",
" test_xy = np.array(test_features_2d)\n",
" plt.scatter(\n",
" test_xy[:, 0],\n",
" test_xy[:, 1],\n",
" s=test_s,\n",
" facecolors=\"none\",\n",
" linewidths=2,\n",
" color=samples_color(test_labels),\n",
" );"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Since the latest version, `sklearn` offers its own method for visualizing decision boundaries: `DecisionBoundaryDisplay`. Documentation for this method can be found here: https://scikit-learn.org/stable/modules/generated/sklearn.inspection.DecisionBoundaryDisplay.html."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Nearest Neighbors\n",
"\n",
"The idea is very simple: to classify a sample $x$ look for **$N$ closest samples in the training data** (by default, using the Euclidean distance) and take **majority of their labels** as a result.\n",
"\n",
"This method does well where the fast linear classifiers would fail, such as with the XOR dataset:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import pandas as pd\n",
"\n",
"\n",
"df = pd.read_csv(\"data/xor.csv\")\n",
"df.head(2)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"features_2d = df.loc[:, (\"x\", \"y\")]\n",
"labelv = df[\"label\"]\n",
"\n",
"plt.figure(figsize=(5, 5))\n",
"plt.scatter(features_2d.iloc[:, 0], features_2d.iloc[:, 1], color=samples_color(labelv));"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"You will find the Nearest Neighbors method in the `sklearn.neighbors` module."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from sklearn.model_selection import train_test_split\n",
"from sklearn.neighbors import KNeighborsClassifier\n",
"\n",
"\n",
"X_train, X_test, y_train, y_test = train_test_split(\n",
" features_2d, labelv, random_state=10\n",
")\n",
"X_train"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
"source": [
"# Let's use 5 neighbors to learn\n",
"classifier = KNeighborsClassifier(n_neighbors=5)\n",
"classifier.fit(X_train, y_train)\n",
"\n",
"print(\"train score: {:.2f}%\".format(100 * classifier.score(X_train, y_train)))\n",
"print(\"test score: {:.2f}%\".format(100 * classifier.score(X_test, y_test)))\n",
"\n",
"plt.figure(figsize=(5, 5))\n",
"plot_decision_surface(\n",
" features_2d,\n",
" labelv,\n",
" classifier,\n",
" test_features_2d=X_test,\n",
" test_labels=y_test,\n",
")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"About the plot: **the points surrounded with a circle are from the test data set** (not used for learning), all other points belong to the training data.\n",
"\n",
"We can query directly for the closest neighbors of a point. Let's check neighborhood of the origin:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# distances and row indices of neighbours around (0, 0):\n",
"dist_nn, ind_nn = classifier.kneighbors([[0, 0]])\n",
"\n",
"# tech: simplify dimensions\n",
"ind_nn = ind_nn.squeeze()\n",
"dist_nn = dist_nn.squeeze()\n",
"\n",
"# build data frame with neighbours\n",
"df = X_train.iloc[ind_nn, :].copy()\n",
"df[\"label\"] = y_train.iloc[ind_nn]\n",
"df[\"dist\"] = dist_nn\n",
"df"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Exercise section\n",
"\n",
"Load the beers dataset and experiment with a number of neighbors (`n_neighbors`) as well as with the Manhattan distance norm `p = 1` (`2` is Euclidian distance)."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import pandas as pd\n",
"from sklearn.model_selection import train_test_split\n",
"from sklearn.neighbors import KNeighborsClassifier\n",
"from sklearn.pipeline import make_pipeline\n",
"from sklearn.preprocessing import StandardScaler\n",
"\n",
"\n",
"df = pd.read_csv(\"data/beers.csv\")\n",
"features = df.iloc[:, :-1]\n",
"labelv = df.iloc[:, -1]\n",
"\n",
"n_neighbors_values = [2, 3, 5, 10, 20]\n",
"p_values = [1, 2]\n",
"# ..."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"tags": [
"solution"
]
},
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
"source": [
"# SOLUTION\n",
"import pandas as pd\n",
"from sklearn.model_selection import train_test_split\n",
"from sklearn.neighbors import KNeighborsClassifier\n",
"from sklearn.pipeline import make_pipeline\n",
"from sklearn.preprocessing import StandardScaler\n",
"\n",
"\n",
"df = pd.read_csv(\"data/beers.csv\")\n",
"features = df.iloc[:, :-1]\n",
"labelv = df.iloc[:, -1]\n",
"\n",
"\n",
"X_train, X_test, y_train, y_test = train_test_split(features, labelv, random_state=10)\n",
"\n",
"p_values = [1, 2]\n",
"n_neighbors_values = [2, 3, 5, 10, 20]\n",
"\n",
"for p in p_values:\n",
" print(f\"#### Norm L{p}\")\n",
" for n_neighbors in n_neighbors_values:\n",
"\n",
" print(\"n_neighbors =\", n_neighbors)\n",
"\n",
" pipeline = make_pipeline(\n",
" StandardScaler(), KNeighborsClassifier(p=p, n_neighbors=n_neighbors)\n",
" )\n",
" pipeline.fit(X_train, y_train)\n",
"\n",
" print(f\" train score: {100 * pipeline.score(X_train, y_train):.2f}%\")\n",
" print(f\" test score: {100 * pipeline.score(X_test, y_test):.2f}%\")\n",
"\n",
" print()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Optional exercise\n",
"\n",
"\n",
"Instead of choosing a number of neighbors you can also specify a radius within which samples make decision, or center of a closest class. Compare decision surface for these methods, as represented by [`RadiusNeighborsClassifier`](https://scikit-learn.org/stable/modules/generated/sklearn.neighbors.RadiusNeighborsClassifier.html#sklearn.neighbors.RadiusNeighborsClassifier) and [`NearestCentroid`](https://scikit-learn.org/stable/modules/generated/sklearn.neighbors.NearestCentroid.html#sklearn.neighbors.NearestCentroid) classifiers in the scikit-learn library.\n",
"\n",
"<div class=\"alert alert-block alert-info\">\n",
"<i class=\"fa fa-info-circle\"></i>\n",
" Choice of an specific querying algorithm (<code>algorithm</code> parameter) becomes important with larger datasets; see: <a href=\"https://scikit-learn.org/stable/modules/neighbors.html#choice-of-nearest-neighbors-algorithm\"><em>Choice of Nearest Neighbors Algorithm</em>.</a>.\n",
"</div>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Few words about optimization and regularization in ML\n",
"\n",
"Many machine learning algorithms adapt internal weights (or similar parameters) to match predictions on the training data as good as possible.\n",
"\n",
"Finding these weights can be formulated as an optimzation problem which **minimizes a cost function**. Solution is usually computed in iterative improvements.\n",
"\n",
"<table>\n",
" <tr><td><img src=\"./images/cost_minimization_iterative.png\" width=400px></td></tr>\n",
" <tr><td><center><sub>Source: <a href=\"https://towardsdatascience.com/machine-learning-fundamentals-via-linear-regression-41a5d11f5220\">https://towardsdatascience.com/machine-learning-fundamentals-via-linear-regression-41a5d11f5220</a></sub></center></td></tr>\n",
"</table>\n",
"\n",
"\n",
"As the optimization only considers training data, taking only classification error/loss as a cost function introduces overfitting. We've seen this before in the overfitting chapter. To compensate for overfitting we can add penalty for more complex models.\n",
"\n",
"### The cost function\n",
"\n",
"<div class=\"alert alert-block alert-warning\">\n",
"<i class=\"fa fa-info-circle\"></i> The cost function to minimize consists of an term measuring a <strong>classification loss</strong> and an additional <strong>regularization penalty</strong>:\n",
"\n",
"$$\\text{cost} = \\text{classification_loss} + \\lambda \\cdot \\text{regularization_penalty}$$\n",
"\n",
"</div>\n",
"\n",
"\n",
"The **regularization weight $\\lambda$** allows to balance out both terms and must be chosen depending on the actual algorithm and the data. In general:\n",
"\n",
"\n",
"* $\\lambda$ close to `0`, $$\\text{cost} \\approx \\text{classification_loss},$$ implies more focus on training data, thus, more complex models and possible overfitting,\n",
"\n",
"\n",
"* $\\lambda$ very large, $$\\text{cost} \\approx \\lambda\\cdot\\text{regularization_penalty},$$ implies less focus on training data, thus, simpler models and possible underfitting.\n",
"\n",
"\n",
"Weighting-in regularization penalty relates to [Occam's razor](https://en.wikipedia.org/wiki/Occam%27s_razor) which states **_\"simpler solutions are more likely to be correct than complex ones.\"_**"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Logistic Regression\n",
"\n",
"The name is misleading: a) despite \"logistic\" the method is linear, b) despite \"regression\" (as in unsupervised learning), it's a classification method.\n",
"\n",
"The method learns weights $w_1,\\cdots,w_n$ for sum of features and the threshold $b$, i.e. to learn a spearation hyper-plane:\n",
"\n",
"$$\n",
"\\text{class}~0:\\quad w_1 \\cdot \\text{feature}_1 + \\ldots + w_n \\cdot \\text{feature}_n \\geq b\n",
"$$\n",
"$$\n",
"\\text{class}~1:\\quad w_1 \\cdot \\text{feature}_1 + \\ldots + w_n \\cdot \\text{feature}_n \\lt b\n",
"$$\n",
"\n",
"Then, to classify, transform the potentially **unlimited range of the weighed sum to a probability** of belonging to one of the two classes using the **logistic function**:\n",
"\n",
"\n",
"$$\n",
"p\\left(x_1,\\cdots,x_n\\right)=\\frac{1}{1+\\exp\\left(b - \\sum_{i=1}^{n} w_i \\cdot x_i\\right)}.\n",
"$$\n",
"\n",
"which looks like that:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# classification threshold\n",
"threshold = 3\n",
"# results of weighted sum (around the threshold)\n",
"weighted_sum = np.linspace(-10 + threshold, 10 + threshold, 100)\n",
"# logistic function\n",
"plt.plot(weighted_sum, 1 / (1 + np.exp(threshold - weighted_sum)))\n",
"plt.axvline(x=threshold, linestyle=\"--\")\n",
"\n",
"plt.ylabel(\"probability that sample's class is 0\")\n",
"plt.xlabel(\"weighted sum of features values\");"
]
},
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<div class=\"alert alert-block alert-info\">\n",
"<p><i class=\"fa fa-info-circle\"></i> Mathematical details\n",
"\n",
"The linear regression uses the following loss function to fit the weights: \n",
"\n",
" <div style=\"padding-left: 3em;\"> \n",
" \n",
"\\$\n",
" l = \\sum_i l_i\n",
"\\$\n",
" </div>\n",
" \n",
"with\n",
" \n",
"$$\n",
" l_i = \\begin{cases}\n",
" -\\log p(x_i; w) \\,\\,\\,\\,\\,\\,\\,\\,\\,\\,\\,\\,\\,\\,\\,\\, \\textrm{if} \\,\\, y_i = 1 \\\\\n",
" -\\log (1-p(x_i; w)) \\,\\,\\,\\, \\textrm{if} \\,\\, y_i = 0 \n",
" \\end{cases}\n",
"$$\n",
" \n",
"So for positive samples with $y_i = 1$ the computed probabilty should be close to $1$ and for negative samples with $y_i = 0$ the probability should be close to $0$ to achieve a low loss value.\n",
"\n",
"The full loss function can be rewritten as\n",
" \n",
"\n",
"\n",
"$$ L = - \\sum_i \\log p(x_i) y_i - \\left(1 - \\log p(x_i) \\right) \\left( 1 - y_i \\right) $$\n",
" \n",
"and is also called <strong>cross entropy loss</strong>.\n",
" \n",
"\n",
"The regularization penalty is usually $l^1$ or $l^2$ loss and thus the full cost function is of the form\n",
"\n",
"$$\n",
" - C \\sum_i \\log p(x_i) y_i - \\left(1 - \\log p(x_i) \\right) \\left( 1 - y_i \\right) \\; + \\; \\sum \\left| w_i \\right|^e \\,\\,\\, \\textrm{with} \\; e \\in \\{1, 2\\}\n",
"$$\n",
" \n",
"</p>\n",
"</div>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Demonstration\n",
"\n",
"Let's use a (almost) line-separable dataset:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import pandas as pd\n",
"\n",
"df = pd.read_csv(\"data/line_separable_2d.csv\")\n",
"df.head(2)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"features_2d = df.loc[:, (\"x\", \"y\")]\n",
"labelv = df[\"label\"]\n",
"\n",
"plt.figure(figsize=(5, 5))\n",
"plt.scatter(features_2d.iloc[:, 0], features_2d.iloc[:, 1], color=samples_color(labelv));"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"You can find the Logistic Regression method in the `sklearn.linear_model` module.\n",
"\n",
"In scikit-learn `LogisticRegression` the regularization weight is passed here in \"inverse\", as a classification weight parameter `C` (default `1`), meaning that it multiplies the classification loss, not the regularization penalty:\n",
"\n",
"$$\\text{cost} = \\text{C}\\cdot\\text{classification_loss} + \\text{regularization_penalty}$$\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from sklearn.linear_model import LogisticRegression\n",
"from sklearn.model_selection import train_test_split\n",
"\n",
"\n",
"X_train, X_test, y_train, y_test = train_test_split(features_2d, labelv, random_state=0)\n",
"\n",
"classifier = LogisticRegression(C=1, random_state=0)\n",
"classifier.fit(X_train, y_train)\n",
"print(\"train score: {:.2f}%\".format(100 * classifier.score(X_train, y_train)))\n",
"print(\"test score: {:.2f}%\".format(100 * classifier.score(X_test, y_test)))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Feature weights are available via `.coef_` attribute, whereas threshold is the negated `.intercept_` attribute. With these we can plot separation line.\n",
"\n",
"Let's see how does it look like and what happens if we put more weight on the classification loss (increase `C` parameter)."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
"source": [
"fig, ax_arr = plt.subplots(ncols=2, nrows=1, figsize=(2 * 5, 5))\n",
"\n",
"plot_decision_surface(\n",
" features_2d,\n",
" labelv,\n",
" classifier,\n",
" test_features_2d=X_test,\n",
" test_labels=y_test,\n",
" plt=ax_arr[0],\n",
" title=\"C=1\",\n",
")\n",
"\n",
"print(\"feature weights:\", classifier.coef_)\n",
"\n",
"\n",
"def plot_separation_line(features_2d, linear_classifier, plt=plt):\n",
" \"\"\"Plot a separation line for 2D dataset\"\"\"\n",
"\n",
" assert hasattr(linear_classifier, \"coef_\")\n",
"\n",
" w = linear_classifier.coef_[0]\n",
" b = -linear_classifier.intercept_ # NOTE: intercept = negative threshold\n",
"\n",
" # separation line: w[0] * x + w[1] * y - b == 0\n",
" feat_x = features_2d.iloc[:, 0]\n",
" x = np.linspace(np.min(feat_x), np.max(feat_x), 2)\n",
" y = (b - w[0] * x) / w[1]\n",
" plt.plot(x, y, color=\"k\", linestyle=\":\")\n",
"\n",
"\n",
"plot_separation_line(features_2d, classifier, plt=ax_arr[0])\n",
"\n",
"\n",
"print()\n",
"print()\n",
"print(\"With C=100\")\n",
"print()\n",
"\n",
"classifier = LogisticRegression(C=100, random_state=0)\n",
"classifier.fit(X_train, y_train)\n",
"print(\"train score: {:.2f}%\".format(100 * classifier.score(X_train, y_train)))\n",
"print(\"test score: {:.2f}%\".format(100 * classifier.score(X_test, y_test)))\n",
"print(\"feature weights:\", classifier.coef_)\n",
"\n",
"plot_decision_surface(\n",
" features_2d,\n",
" labelv,\n",
" classifier,\n",
" test_features_2d=X_test,\n",
" test_labels=y_test,\n",
" plt=ax_arr[1],\n",
" title=\"C=100\",\n",
")\n",
"plot_separation_line(features_2d, classifier, plt=ax_arr[1])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Exercise section\n",
"\n",
"1. Why does the test score drop when we penalize more misclassifications?\n",
"2. For the higher dimensional beers dataset experiment with both `C` and `penalty` parameters of the linear regression classfier. Compare scores and the resulting weights. What does the `l1` penalty do? What is the sweet spot of the \"inverse regularization\" `C`?\n",
" "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"tags": [
"solution"
]
},
"outputs": [],
"source": [
"# SOLUTION 1\n",
"\n",
"# With C=100 we try hard to get all training points correctly classified, whereas with C=1\n",
"# you can see we allow misclassification in training, in order to possibly get more general\n",
"# model and avoid overfitting.\n",
"#\n",
"# You can see in the test data, that reverse - one misclassfied point with C=100.\n",
"# If we would have that point for for training, the line would look more like in C=1 case.\n",
"# (Go regularization!)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import pandas as pd\n",
"from sklearn.linear_model import LogisticRegression\n",
"from sklearn.model_selection import train_test_split\n",
"from sklearn.pipeline import make_pipeline\n",
"from sklearn.preprocessing import StandardScaler\n",
"\n",
"\n",
"df = pd.read_csv(\"data/beers.csv\")\n",
"features = df.iloc[:, :-1]\n",
"labelv = df.iloc[:, -1]\n",
"\n",
"C_values = [0.01, 0.1, 1, 10, 100, 1000]\n",
"penalty_values = [\"l1\", \"l2\"]\n",
"# ..."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"tags": [
"solution"
]
},
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
"source": [
"# SOLUTION 2\n",
"import pandas as pd\n",
"from sklearn.linear_model import LogisticRegression\n",
"from sklearn.model_selection import train_test_split\n",
"from sklearn.pipeline import make_pipeline\n",
"from sklearn.preprocessing import StandardScaler\n",
"\n",
"\n",
"df = pd.read_csv(\"data/beers.csv\")\n",
"print(df.head(2))\n",
"\n",
"features = df.iloc[:, :-1]\n",
"labelv = df.iloc[:, -1]\n",
"\n",
"X_train, X_test, y_train, y_test = train_test_split(features, labelv, random_state=10)\n",
"\n",
"C_values = [0.01, 0.1, 1, 10, 100, 1000]\n",
"penalty_values = [\"l1\", \"l2\"]\n",
"\n",
"print()\n",
"for norm in penalty_values:\n",
" print(\"#### Norm\", norm)\n",
" print()\n",
" for C in C_values:\n",
" print(\"C:\", C)\n",
" # Note: use non-default solver for L1 penalty support\n",
" # Note: increase max iterations 10x for solver's convergence\n",
" pipeline = make_pipeline(\n",
" StandardScaler(),\n",
" LogisticRegression(\n",
" C=C, solver=\"liblinear\", penalty=norm, dual=False, max_iter=10000\n",
" ),\n",
" )\n",
" pipeline.fit(X_train, y_train)\n",
" print(f\" train score: {100 * pipeline.score(X_train, y_train):.2f}%\")\n",
" print(f\" test score: {100 * pipeline.score(X_test, y_test):.2f}%\")\n",
" print(\" weights:\", pipeline[-1].coef_[0])\n",
" print()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<div class=\"alert alert-block alert-info\">\n",
"\n",
"<p><i class=\"fa fa-info-circle\"></i> \n",
"The <strong>classification loss</strong> in logistic regression is a so called <em>negative-log likelihood</em>, i.e. a negative logarithm of the logistic probability above:\n",
"<p/>\n",
" \n",
"<p>\n",
"$$ \\text{classification_loss} = -\\log(p(x^k; p^k)) = \\log{\\left(1+\\exp{\\left(y^k\\left(b - \\sum_{i=1}^{n} w_i x_i^k\\right)\\right)}\\right)}$$\n",
"<p/>\n",
"\n",
"<p>\n",
"where $y^k$ is -1 or 1, representing class of $k$-th sample from the training data, corresponding, respectively, to class below and above the threshold (the separation line).\n",
"\n",
"The $+/-$ sign for the class penalizes missclassifications. If sample is below the threshold $\\sum_{i=1}^{n} w_i x_i^k < b$ and have the correct class $y^k = -1$, then we have $\\exp{\\left(\\text{negative value}\\right)}$ giving small loss. In case of misclassification $\\exp{\\left(\\text{positive value}\\right)}$ gives a much bigger loss.\n",
"</p>\n",
"</div>\n",
"\n",
"<div class=\"alert alert-block alert-info\">\n",
"<p><i class=\"fa fa-info-circle\"></i> \n",
"The <strong>reqularization penalty</strong> in logistic regression is a <em>norm of the learnt weights</em>, denoted as:\n",
"\n",
"<p>\n",
"$$\\text{regularization_penalty} = \\left\\lVert w \\right\\rVert_p$$\n",
"</p>\n",
"\n",
"<p>\n",
"Using <em>L1 norm</em> ($p=1$, Manhattan distance from origin, which is sum of absolute weight values) is know for finding sparse solutions, i.e. eliminating features (weight equal to 0) when they are have low significance. With the default <em>L2 norm</em> ($p=2$, Euclidian distance from origin, which is square root of sum of squared weight values), weights of insignificant features would have small non-zero values instead.\n",
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
"</p>\n",
"\n",
"<p>\n",
"In <code>LogisticRegression</code> class, <code>penalty</code> parameter allows to specify type of norm to use.\n",
"</p>\n",
"\n",
"<p>\n",
"Note that any solution weights and its threshold can be scaled to give the same result. Thus the regularization penalty not only prevents overfitting but also ensures a unique solution.\n",
"</p>\n",
"\n",
"</div>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Linear SVM\n",
"\n",
"Support-Vector Machine (SVM) classifier tries to separate two classes with a line by **finding data points (support vectors) lying closest to the separation plane**. These points determine separation plane (weights and threshold/intercept).\n",
"\n",
"The weights are learned such that the **margin between support vectors of different classes is maximized**.\n",
"\n",
"<table>\n",
" <tr><td><img src=\"./images/svm_margin.png\" width=400px></td></tr>\n",
" <tr><td><center><sub>Source: <a href=\"https://en.wikipedia.org/wiki/Support-vector_machine\">https://en.wikipedia.org/wiki/Support-vector_machine</a></sub></center></td></tr>\n",
"</table>\n",
"\n",
"Like in linear regression the classification is based on a weighted sum of the features (and margin maximization corresponds to minimization of the regularization penalty). \n",
"\n",
"Analogously to the Nearest Neighbors method the data points (support vectors) decide the class of a new data sample."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Demonstration\n",
"\n",
"Let's try it out on the line-separable dataset.\n",
"\n",
"You will find the SVM method in the `sklearn.svm` module."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
"source": [
"import pandas as pd\n",
"from sklearn.model_selection import train_test_split\n",
"from sklearn.svm import LinearSVC\n",
"\n",
"\n",
"df = pd.read_csv(\"data/line_separable_2d.csv\")\n",
"features_2d = df.loc[:, (\"x\", \"y\")]\n",
"labelv = df[\"label\"]\n",
"\n",
"X_train, X_test, y_train, y_test = train_test_split(features_2d, labelv, random_state=0)\n",
"\n",
"classifier = LinearSVC(C=1)\n",
"classifier.fit(X_train, y_train)\n",
"print(\"train score: {:.2f}%\".format(100 * classifier.score(X_train, y_train)))\n",
"print(\"test score: {:.2f}%\".format(100 * classifier.score(X_test, y_test)))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Again, let's see how does the separation line look like here and what happens if we put more weight on the classification loss (increase `C` parameter)."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
"source": [
"fig, ax_arr = plt.subplots(ncols=2, nrows=1, figsize=(2 * 5, 5))\n",
"\n",
"plot_decision_surface(\n",
" features_2d,\n",
" labelv,\n",
" classifier,\n",
" test_features_2d=X_test,\n",
" test_labels=y_test,\n",
" plt=ax_arr[0],\n",
" title=\"C=1\",\n",
")\n",
"\n",
"print(\"feature weights:\", classifier.coef_)\n",
"\n",
"\n",
"def plot_margins(features_2d, linear_classifier, plt=plt):\n",
" \"\"\"Plot a separation line and margin lines for 2D dataset\"\"\"\n",
"\n",
" assert hasattr(linear_classifier, \"coef_\")\n",
"\n",
" w = linear_classifier.coef_[0]\n",
" b = -linear_classifier.intercept_ # NOTE: intercept = negative threshold\n",
"\n",
" # separation line: w[0] * x + w[1] * y - b == 0\n",
" feat_x = features_2d.iloc[:, 0]\n",
" x = np.linspace(np.min(feat_x), np.max(feat_x), 2)\n",
" y = (b - w[0] * x) / w[1]\n",
" plt.plot(x, y, color=\"k\", linestyle=\":\")\n",
"\n",
" # margin lines: w[0] * x + w[1] * y - b == +/-1\n",
" y = ((b - 1) - w[0] * x) / w[1]\n",
" plt.plot(x, y, color=\"r\", linestyle=\":\")\n",
" y = ((b + 1) - w[0] * x) / w[1]\n",
" plt.plot(x, y, color=\"r\", linestyle=\":\")\n",
"\n",
"\n",
"plot_margins(features_2d, classifier, plt=ax_arr[0])\n",
"\n",
"\n",
"print()\n",
"print()\n",
"print(\"With C=100\")\n",
"print()\n",
"\n",
"# higher C = more narrow (\"harder\") margin\n",
"# Note: increase max iterations 50x for solver's convergence\n",
"classifier = LinearSVC(C=100, max_iter=50000)\n",
"classifier.fit(X_train, y_train)\n",
"print(\"train score: {:.2f}%\".format(100 * classifier.score(X_train, y_train)))\n",
"print(\"test score: {:.2f}%\".format(100 * classifier.score(X_test, y_test)))\n",
"print(\"feature weights:\", classifier.coef_)\n",
"\n",
"plot_decision_surface(\n",
" features_2d,\n",
" labelv,\n",
" classifier,\n",
" test_features_2d=X_test,\n",
" test_labels=y_test,\n",
" plt=ax_arr[1],\n",
" title=\"C=100\",\n",
")\n",
"plot_margins(features_2d, classifier, plt=ax_arr[1]);"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Why are there **training data samples within the margin**?\n",
"\n",
"This is because the scikit-learn `LinearSVC` implementation by default uses so called **\"soft margins\"**, (\"hinge\" loss function in `loss` parameter). They allow support vectors to lie within the -1, 1 margin (with appropriately lower weights than -1, 1).\n",
"\n",
"You can control \"softness\" or \"hardness\" of classification loss by, respectively, decreasing or increasing its weight (parameter `C` of the `LinearSVC` class)."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Exercise section\n",
"\n",
"1. It looks like we did train our classifier \"perfectly\" with \"harder\" margins. Why is the score then lower then previously?\n",
"2. For the higher dimensional beers dataset experiment with both `C` and `penalty` parameters of the linear SVM classfier (note: set `dual=False` to work with `penalty='l1'`). Compare scores and the resulting weights.\n",
" "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import pandas as pd\n",
"from sklearn.model_selection import train_test_split\n",
"from sklearn.pipeline import make_pipeline\n",
"from sklearn.preprocessing import StandardScaler\n",
"from sklearn.svm import LinearSVC\n",
"\n",
"\n",
"df = pd.read_csv(\"data/beers.csv\")\n",
"features = df.iloc[:, :-1]\n",
"labelv = df.iloc[:, -1]\n",
"\n",
"C_values = [0.01, 0.1, 1, 10, 100, 1000]\n",
"penalty_values = [\"l1\", \"l2\"]\n",
"\n",
"# ..."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"tags": [
"solution"
]
},
"outputs": [],
"source": [
"# SOLUTION 1\n",
"\n",
"# Again, with C=100 we've just tried to hard to get all training points correctly classified,\n",
"# but this time it meant essentially no points within the margin. Thus, by overfitting we\n",
"# lost the linear trend in the data, which is represented by the one test data sample that\n",
"# just did not make it over the separation line (and a bit overrepresented by the other quite\n",
"# badly misclassfied test sample)."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"tags": [
"solution"
]
},
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"source": [
"# SOLUTION 2\n",
"import pandas as pd\n",
"from sklearn.model_selection import train_test_split\n",
"from sklearn.pipeline import make_pipeline\n",
"from sklearn.preprocessing import StandardScaler\n",
"from sklearn.svm import LinearSVC\n",
"\n",
"\n",
"df = pd.read_csv(\"data/beers.csv\")\n",
"print(df.head(2))\n",
"\n",
"features = df.iloc[:, :-1]\n",
"labelv = df.iloc[:, -1]\n",
"\n",
"X_train, X_test, y_train, y_test = train_test_split(features, labelv, random_state=10)\n",
"\n",
"C_values = [0.01, 0.1, 1, 10, 100, 1000]\n",
"penalty_values = [\"l1\", \"l2\"]\n",
"\n",
"print()\n",
"for norm in penalty_values:\n",
" print(\"#### Norm\", norm)\n",
" print()\n",
" for C in C_values:\n",
" print(\"C:\", C)\n",
" # Note: increase max iterations 10x for solver's convergence\n",
" pipeline = make_pipeline(\n",
" StandardScaler(),\n",