Skip to content

Commit 8f62996

Browse files
committed
edits, updates
1 parent 2168001 commit 8f62996

File tree

1 file changed

+66
-25
lines changed

1 file changed

+66
-25
lines changed

notebooks/16_machine_learning_algorithms.ipynb

Lines changed: 66 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,13 @@
1414
{
1515
"cell_type": "markdown",
1616
"id": "ccfadb82-4be0-407d-a54e-2198f75c2bf1",
17-
"metadata": {},
17+
"metadata": {
18+
"editable": true,
19+
"slideshow": {
20+
"slide_type": ""
21+
},
22+
"tags": []
23+
},
1824
"source": [
1925
"## Introduction\n",
2026
"**$k$-nearest neighbors** is, for very good reasons, one of the most commonly known machine learning algorithms. It is relatively intuitive and simple, yet still powerful enough to find plenty of use cases even today (despite having much fancier techniques on the market).\n",
@@ -34,40 +40,80 @@
3440
"\n",
3541
"---\n",
3642
"\n",
37-
"### Pros, Cons, Caveats\n",
43+
"## Pros, Cons, Caveats\n",
3844
"Conceptually, the k-nearest neighbors algorithm is rather simple and intuitive. However, there are a few important aspects to consider when applying this algorithm.\n",
3945
"\n",
40-
"First of all, k-nearest kneighbors is a distance-based algorithm. This means that we have to ensure that closer really means \"more similar\" which is not as simple as it may sound. We have to decide on a *distance metric*, that is the measure (or function) by which we calculate the distance between data points. We can use common metrics like the Euclidean distance, but there are many different options to choose from.\n",
46+
"\n",
47+
"### Caveats\n",
48+
"Let's consider a situation as in {numref}`fig_knn_caveats`A. Here we see that a change in $k$ can lead to entirely different predictions for certain data points. In general, kNN predictions can be highly unstable close to border regions, and they also tend to be highly sensitive to the local density of data points. The latter can be a problem if we have far more points in one category than in another.\n",
49+
"\n",
50+
"```{figure} ../images/fig_knn_caveats.png\n",
51+
":name: fig_knn_caveats\n",
52+
"\n",
53+
"k-nearest neighbors has a few important caveats. **A** its predictions can change with changing $k$, and generally are very density sensitive. **B** it suffers (as many machine learning models) from overconfidence, which simply means that it will confidently output predictions even for data points that are entirely different from the training data (or even physically impossible).\n",
54+
"```\n",
55+
"\n",
56+
"Another common problem with kNN -but also many other models- is called **over-confidence** ({numref}`fig_knn_caveats`B). The algorithm described here creates its predictions on the $k$ closest neighbors. But for very unusual inputs or even entirely impossible inputs, the algorithm will still find $k$ closest neighbors and make a prediction. So if you ask for the shoe size of a person of 6.20m and 840 kg, your model might confidently answer your question and say: 48 (if nothing bigger occurred in the data). So much for the \"intelligent\" in *artificial intelligence* ...\n",
57+
"\n",
58+
"Finally, k-nearest neighbors is a distance-based algorithm. This means that we have to ensure that closer really means \"more similar\" which is not as simple as it may sound. We have to decide on a *distance metric*, that is, the measure (or function) by which we calculate the distance between data points. We can use common metrics like the Euclidean distance, but there are many different options to choose from.\n",
4159
"Even more critical is the proper *scaling* of our features. Just think of an example. We want to predict the shoe size of a person based on the person's height (measured in $m$) and weight (measured in $kg$). This means that we have two features here: height and weight. For a prediction on a new person, we simply need his/her height and weight. Then k-NN will compare those values to all known (\"learned\") data points in our model and find the closest $k$ other people. If we now use the Euclidean distance, the distance $d$ will simply be\n",
4260
"\n",
4361
"$$\n",
4462
" d = \\sqrt{(w_1 - w_2) ^ 2 + (h_1 - h_2) ^ 2}\n",
4563
"$$\n",
46-
"where $w$ and $h$ are the weights and heights of person 1 and 2.\n",
64+
"where $w$ and $h$ are the weights and heights of person 1 and 2. Let's say we have person-1 with 1.73m and 81kg and person-2 with 1.89m and 79kg.\n",
4765
"\n",
48-
"Try to answer the following question: What is the problem here?\n",
66+
"Take a moment to answer the following question: What is the problem here?\n",
4967
"\n",
5068
"...?"
5169
]
5270
},
5371
{
5472
"cell_type": "markdown",
55-
"id": "54f4d3ba-3770-44c2-98be-c554df69cd96",
56-
"metadata": {},
73+
"id": "be9b139e-42f1-4a51-aeef-c2e65757b906",
74+
"metadata": {
75+
"editable": true,
76+
"slideshow": {
77+
"slide_type": ""
78+
},
79+
"tags": [
80+
"toggle"
81+
]
82+
},
5783
"source": [
58-
"Ok. The issue here is, that the weights are in kilograms ($kg$), so we are talking about values like 50, 60, 80, 100. The height, however, is measured in meters ($m$) such that values are many times smaller. As a result, having two people differ one meter in height (which is a lot) will count no more than one kilogram difference (which is close to nothing). Clearly not what we intuitively mean by \"nearest neighbors\"!\n",
84+
"Ok. The issue here is, that the weights are in kilograms ($kg$), so we are talking about values like 50, 60, 80, 100. The height, however, is measured in meters ($m$) such that values are many times smaller. As a result, having two people differ one meter in height (which is a lot) will count no more than one kilogram difference (which is close to nothing). Clearly not what we intuitively mean by \"nearest neighbors\"!"
85+
]
86+
},
87+
{
88+
"cell_type": "markdown",
89+
"id": "fc59bfe8-681d-4a38-a2fa-71e540558e57",
90+
"metadata": {
91+
"editable": true,
92+
"slideshow": {
93+
"slide_type": ""
94+
},
95+
"tags": []
96+
},
97+
"source": [
98+
"### Data Scaling\n",
5999
"\n",
60100
"The solution to this is a proper **scaling** of our data. Often, we will simply apply one of the following two scaling methods:\n",
61-
"1. MinMax Scaling - this means we linearly rescale our data such that the lowest occurring value becomes 0 and the highest value becomes 1.\n",
62-
"2. Standard Scaling - here we rescale our data such that the mean value will be 0 and the standard deviation will be 1.\n",
101+
"1. **MinMax Scaling** - We linearly rescale our data such that the lowest occurring value becomes 0 and the highest value becomes 1.\n",
102+
"2. **Standard Scaling** - We rescale our data such that the mean value will be 0 and the standard deviation will be 1.\n",
63103
"\n",
64-
"Both methods might give you values that look awkward at first. Standard scaling, for instance, gives both positive and negative values so that our height values in the example could be -1.04 or +0.27. But don't worry, the scaling is really only meant to be used for the machine learning algorithm itself."
104+
"Both methods might produce values that look awkward at first. Standard scaling, for instance, gives positive and negative values so that our height values in the example could be -1.04 or +0.27. But don't worry, the scaling is only meant to be used for the machine learning algorithm itself. For manual inspection or visualizations of the data we would still use the data in its original scaling."
65105
]
66106
},
67107
{
68108
"cell_type": "markdown",
69109
"id": "97cf5881-c242-4d4b-8dd9-e5fdc7723f1a",
70-
"metadata": {},
110+
"metadata": {
111+
"editable": true,
112+
"slideshow": {
113+
"slide_type": ""
114+
},
115+
"tags": []
116+
},
71117
"source": [
72118
"Once we scaled our data, and maybe also picked the right distance metric (or used a good default, which will do for a start), we are technically good to apply k-NN.\n",
73119
"\n",
@@ -77,24 +123,19 @@
77123
"This is the model's main parameter and we are free to choose any value we like. And there is no simple best choice that always works. In practice, the choice of $k$ will depend on the number of data points we have, but also on the distribution of data and the number of classes or parameter ranges. We usually want to pick odd values here to avoid draws as much as possible (imagine two nearest neighbors are \"spam\" and two are \"no-spam\"). But whether 3, 5, 7, or 13 is the best choice will depend on our specific task at hand. \n",
78124
"\n",
79125
"\n",
80-
"In machine learning, we call such a thing a **hyperparameter** (or: fitting parameter). These are parameters that are not *learned* by the model, but have to be defined by us. We are free to change its value, and it might have a considerable impact on the quality of our predictions, or our \"model performance\". Ideally, we would compare several different models with different parameters and pick the one that performed best. We will later see, that machine learning models often have many hyperparameters. Luckily, not all of them are equally sensitive so they will often already be set to more-or-less ok-ish default values in libraries such as Scikit-Learn.\n",
81-
"\n",
82-
"#### Caveats\n",
83-
"Let's consider a situation as in {numref}`fig_knn_caveats`A. Here we see that a change in $k$ can lead to entirely different predictions for certain data points. In general, kNN predictions can be highly unstable close to border regions, and they also tend to be highly sensitive to the local density of data points. The latter can be a problem if we have far more points in one category than in another.\n",
84-
"\n",
85-
"```{figure} ../images/fig_knn_caveats.png\n",
86-
":name: fig_knn_caveats\n",
87-
"\n",
88-
"k-nearest neighbors has a few important caveats. **A** its predictions can change with changing $k$, and generally are very density sensitive. **B** it suffers (as many machine learning models) from overconfidence, which simply means that it will confidently output predictions even for data points that are entirely different from the training data (or even physically impossible).\n",
89-
"```\n",
90-
"\n",
91-
"Finally, another common problem with kNN -but also many other models- is called **over-confidence** ({numref}`fig_knn_caveats`B). The algorithm described here creates its predictions on the $k$ closest neighbors. But for very unusual inputs or even entirely impossible inputs, the algorithm will still find $k$ closest neighbors and make a prediction. So if you ask for the shoe size of a person of 6.20m and 840 kg, your model might confidently answer your question and say: 48 (if nothing bigger occurred in the data). So much for the \"intelligent\" in *artificial intelligence* ..."
126+
"In machine learning, we call such a thing a **hyperparameter** (or: fitting parameter). These are parameters that are not *learned* by the model, but have to be defined by us. We are free to change its value, and it might have a considerable impact on the quality of our predictions, or our \"model performance\". Ideally, we would compare several different models with different parameters and pick the one that performed best. We will later see, that machine learning models often have many hyperparameters. Luckily, not all of them are equally sensitive so they will often already be set to more-or-less ok-ish default values in libraries such as Scikit-Learn."
92127
]
93128
},
94129
{
95130
"cell_type": "markdown",
96131
"id": "f8dc2519-24d0-482b-ad15-71ba3671cb79",
97-
"metadata": {},
132+
"metadata": {
133+
"editable": true,
134+
"slideshow": {
135+
"slide_type": ""
136+
},
137+
"tags": []
138+
},
98139
"source": [
99140
"In summary, k-NN has a number of Pros and Cons:\n",
100141
"\n",

0 commit comments

Comments
 (0)