|
79 | 79 | "source": [ |
80 | 80 | "## What is a Machine Learning Model?\n", |
81 | 81 | "\n", |
82 | | - "At a high level, a **model** is a rule that maps inputs to outputs:\n", |
| 82 | + "At a high level, a **model** is a rule that maps inputs to outputs.\n", |
83 | 83 | "\n", |
| 84 | + "::: {.callout-note title=\"Definition: a model\"}\n", |
84 | 85 | "$$\n", |
85 | 86 | "\\hat{y} = f(\\mathbf{x})\n", |
86 | 87 | "$$\n", |
87 | 88 | "\n", |
88 | | - "- $\\mathbf{x}$: input (features)\n", |
89 | | - "- $\\hat{y}$: model output (prediction)\n", |
90 | | - "- $f$: model (the rule we learn from data)\n", |
| 89 | + "- $\\mathbf{x}$: input (features), one example as a point in $\\mathbb{R}^d$,\n", |
| 90 | + "- $\\hat{y}$: model output (prediction),\n", |
| 91 | + "- $f$: the model, i.e. the rule we learn from data.\n", |
| 92 | + "\n", |
| 93 | + "A model is a *function*. Building one means choosing its form, then choosing the\n", |
| 94 | + "numbers inside it.\n", |
| 95 | + ":::\n", |
91 | 96 | "\n", |
92 | 97 | "In practice, we will write:\n", |
93 | 98 | "\n", |
|
103 | 108 | "- $\\boldsymbol{\\eta}$: **hyperparameters** (we choose these *before* training)\n", |
104 | 109 | " - e.g. number of neighbors `k`, depth of a tree, regularization strength\n", |
105 | 110 | "\n", |
106 | | - "::: {.callout-note title=\"Key idea\"}\n", |
107 | | - "- Parameters are *learned* from data by the training algorithm.\n", |
108 | | - "- Hyperparameters are *chosen* by us to control model complexity and behavior.\n", |
| 111 | + "::: {.callout-tip title=\"Key concept: parameters are learned, hyperparameters are chosen\"}\n", |
| 112 | + "- **Parameters** $\\mathbf{w}$ are *learned* from data by the training algorithm.\n", |
| 113 | + "- **Hyperparameters** $\\boldsymbol{\\eta}$ are *chosen* by us, before training, and\n", |
| 114 | + " control the model's complexity and behavior.\n", |
| 115 | + "\n", |
| 116 | + "The notation $\\mathbf{w}(\\boldsymbol{\\eta})$ says this out loud: which parameters we\n", |
| 117 | + "end up learning depends on the hyperparameters we picked first.\n", |
109 | 118 | ":::\n", |
110 | 119 | "\n", |
111 | 120 | "Later we will tune hyperparameters more carefully. For now, we’ll use simple defaults and explore how they affect behavior.\n" |
|
142 | 151 | "\n", |
143 | 152 | "> “Let the data speak; find structure in $\\mathbf{x}$ itself.”\n", |
144 | 153 | "\n", |
| 154 | + "::: {.callout-tip title=\"Key concept: the task comes from the question, not the data\"}\n", |
| 155 | + "The same dataset matrix $X$ can serve any of these tasks. What changes is what we ask\n", |
| 156 | + "for: a number (regression), a label (classification), or structure in $X$ itself\n", |
| 157 | + "(unsupervised). That single choice fixes the target, the family of models we can use,\n", |
| 158 | + "*and* the metric we are allowed to judge the result by.\n", |
| 159 | + ":::\n", |
| 160 | + "\n", |
145 | 161 | "In this module, we focus on **supervised learning and classification** to get familiar with modeling in `scikit-learn`.\n" |
146 | 162 | ] |
147 | 163 | }, |
|
306 | 322 | "2. _fit_: train data X, labels y (labels needed for supervised models only)\n", |
307 | 323 | " - `model.fit(X, y)`\n", |
308 | 324 | "3. _predict_: generates output\n", |
309 | | - " - `model.predict(X_test)`" |
| 325 | + " - `model.predict(X_test)`\n", |
| 326 | + "\n", |
| 327 | + "::: {.callout-note title=\"Key concept: every sklearn model is the same three calls\"}\n", |
| 328 | + "```python\n", |
| 329 | + "model = Estimator(hyperparameters) # 1. pick the model family and its knobs\n", |
| 330 | + "model.fit(X, y) # 2. learn the parameters from data\n", |
| 331 | + "y_pred = model.predict(X_new) # 3. predict on new inputs\n", |
| 332 | + "```\n", |
| 333 | + "\n", |
| 334 | + "The estimator changes; the three lines do not. Everything passed to the constructor is\n", |
| 335 | + "a hyperparameter $\\boldsymbol{\\eta}$, and `fit` is where the parameters $\\mathbf{w}$\n", |
| 336 | + "come from.\n", |
| 337 | + ":::" |
310 | 338 | ] |
311 | 339 | }, |
312 | 340 | { |
|
334 | 362 | "2. Find the $k$ closest training points.\n", |
335 | 363 | "3. Let those neighbors vote.\n", |
336 | 364 | "\n", |
337 | | - "There is almost no training phase. The model mostly stores the training data and uses distance at prediction time.\n" |
| 365 | + "There is almost no training phase. The model mostly stores the training data and uses distance at prediction time.\n", |
| 366 | + "\n", |
| 367 | + "::: {.callout-note title=\"Definition: the $k$-nearest-neighbors rule\"}\n", |
| 368 | + "To predict the label of a new point $\\mathbf{x}$:\n", |
| 369 | + "\n", |
| 370 | + "1. compute the distance $d(\\mathbf{x}, \\mathbf{x}_i)$ to every training point,\n", |
| 371 | + "2. keep the $k$ smallest, giving the neighbor set $N_k(\\mathbf{x})$,\n", |
| 372 | + "3. predict the majority label among those $k$ neighbors.\n", |
| 373 | + "\n", |
| 374 | + "There is no fitted $\\mathbf{w}$ here: kNN stores the training set and does all its work\n", |
| 375 | + "at prediction time. Its behavior is set entirely by hyperparameters, $k$ and the\n", |
| 376 | + "distance $d$ (the $L^p$ distance from Notebook 1a).\n", |
| 377 | + ":::\n" |
338 | 378 | ] |
339 | 379 | }, |
340 | 380 | { |
|
2038 | 2078 | "- `p=1` gives Manhattan distance,\n", |
2039 | 2079 | "- `p=2` gives Euclidean distance.\n", |
2040 | 2080 | "\n", |
| 2081 | + "::: {.callout-important title=\"Key concept: $k$ controls model complexity\"}\n", |
| 2082 | + "$k$ sets how much the model is allowed to trust a single point.\n", |
| 2083 | + "\n", |
| 2084 | + "- $k = 1$: every training point rules its own neighborhood. Training accuracy is\n", |
| 2085 | + " perfect and the boundary chases noise, i.e. **overfitting**.\n", |
| 2086 | + "- $k$ close to $n$: every prediction is the majority label of nearly the whole dataset.\n", |
| 2087 | + " The boundary is so smooth it ignores real structure, i.e. **underfitting**.\n", |
| 2088 | + "\n", |
| 2089 | + "Nothing in the data tells us the right $k$: we have to *choose* it. Notebook 2b is\n", |
| 2090 | + "about how to make that choice honestly.\n", |
| 2091 | + ":::\n", |
| 2092 | + "\n", |
2041 | 2093 | "Run the next cell, then move the sliders.\n" |
2042 | 2094 | ] |
2043 | 2095 | }, |
|
2192 | 2244 | "4. Compare its boundary to kNN.\n", |
2193 | 2245 | "5. Which model seems more flexible? Which seems easier to explain?" |
2194 | 2246 | ] |
| 2247 | + }, |
| 2248 | + { |
| 2249 | + "cell_type": "markdown", |
| 2250 | + "id": "8ea28411", |
| 2251 | + "metadata": { |
| 2252 | + "id": "nb2a-takeaways" |
| 2253 | + }, |
| 2254 | + "source": [ |
| 2255 | + "## Takeaways\n", |
| 2256 | + "\n", |
| 2257 | + "::: {.callout-tip title=\"Takeaways from this lecture\"}\n", |
| 2258 | + "1. **A model is a function**, $\\hat{y} = f(\\mathbf{x}, \\mathbf{w}(\\boldsymbol{\\eta}))$,\n", |
| 2259 | + " from features to a prediction, not a black box.\n", |
| 2260 | + "2. **Parameters vs. hyperparameters:** $\\mathbf{w}$ is *learned* by fitting;\n", |
| 2261 | + " $\\boldsymbol{\\eta}$ is *chosen* by us before training.\n", |
| 2262 | + "3. **The task comes from the question.** The same $X$ supports regression (predict a\n", |
| 2263 | + " number), classification (predict a label), and unsupervised learning (find structure\n", |
| 2264 | + " in $X$ alone).\n", |
| 2265 | + "4. **Every sklearn model is the same three calls:** construct with hyperparameters,\n", |
| 2266 | + " `fit` to learn parameters, `predict` on new inputs.\n", |
| 2267 | + "5. **kNN predicts by neighborhood vote:** distances to all training points, keep the $k$\n", |
| 2268 | + " closest, take the majority label. It stores data instead of fitting parameters.\n", |
| 2269 | + "6. **The distance is a hyperparameter too:** Minkowski `p=1` is Manhattan and `p=2` is\n", |
| 2270 | + " Euclidean, the $L^p$ distances from Notebook 1a, and they give different boundaries\n", |
| 2271 | + " on the same data.\n", |
| 2272 | + "7. **$k$ controls complexity:** small $k$ gives a jagged boundary that overfits, large\n", |
| 2273 | + " $k$ gives a smooth one that underfits. Notebook 2b turns this into a principled\n", |
| 2274 | + " choice.\n", |
| 2275 | + "8. **A decision boundary makes a model's assumptions visible.** Plot it whenever you\n", |
| 2276 | + " want to see what a classifier actually believes.\n", |
| 2277 | + "9. **A model encodes assumptions about which patterns matter.** kNN assumes nearby\n", |
| 2278 | + " points share labels; that is a claim about the data, not a fact.\n", |
| 2279 | + ":::" |
| 2280 | + ] |
2195 | 2281 | } |
2196 | 2282 | ], |
2197 | 2283 | "metadata": { |
|
0 commit comments