Skip to content

Commit ff0694f

Browse files
committed
update
1 parent 1c12e62 commit ff0694f

4 files changed

Lines changed: 486 additions & 35 deletions

File tree

‎notebooks/Notebook_2/Notebook_2a_Building_a_Model.ipynb‎

Lines changed: 95 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -79,15 +79,20 @@
7979
"source": [
8080
"## What is a Machine Learning Model?\n",
8181
"\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",
8383
"\n",
84+
"::: {.callout-note title=\"Definition: a model\"}\n",
8485
"$$\n",
8586
"\\hat{y} = f(\\mathbf{x})\n",
8687
"$$\n",
8788
"\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",
9196
"\n",
9297
"In practice, we will write:\n",
9398
"\n",
@@ -103,9 +108,13 @@
103108
"- $\\boldsymbol{\\eta}$: **hyperparameters** (we choose these *before* training)\n",
104109
" - e.g. number of neighbors `k`, depth of a tree, regularization strength\n",
105110
"\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",
109118
":::\n",
110119
"\n",
111120
"Later we will tune hyperparameters more carefully. For now, we’ll use simple defaults and explore how they affect behavior.\n"
@@ -142,6 +151,13 @@
142151
"\n",
143152
"> “Let the data speak; find structure in $\\mathbf{x}$ itself.”\n",
144153
"\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",
145161
"In this module, we focus on **supervised learning and classification** to get familiar with modeling in `scikit-learn`.\n"
146162
]
147163
},
@@ -306,7 +322,19 @@
306322
"2. _fit_: train data X, labels y (labels needed for supervised models only)\n",
307323
" - `model.fit(X, y)`\n",
308324
"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+
":::"
310338
]
311339
},
312340
{
@@ -334,7 +362,19 @@
334362
"2. Find the $k$ closest training points.\n",
335363
"3. Let those neighbors vote.\n",
336364
"\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"
338378
]
339379
},
340380
{
@@ -2038,6 +2078,18 @@
20382078
"- `p=1` gives Manhattan distance,\n",
20392079
"- `p=2` gives Euclidean distance.\n",
20402080
"\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",
20412093
"Run the next cell, then move the sliders.\n"
20422094
]
20432095
},
@@ -2192,6 +2244,40 @@
21922244
"4. Compare its boundary to kNN.\n",
21932245
"5. Which model seems more flexible? Which seems easier to explain?"
21942246
]
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+
]
21952281
}
21962282
],
21972283
"metadata": {

‎notebooks/Notebook_2/Notebook_2a_Building_a_Model.py‎

Lines changed: 86 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -54,15 +54,20 @@
5454
# %% [markdown] id="39usOoiAe5DI"
5555
# ## What is a Machine Learning Model?
5656
#
57-
# At a high level, a **model** is a rule that maps inputs to outputs:
57+
# At a high level, a **model** is a rule that maps inputs to outputs.
5858
#
59+
# ::: {.callout-note title="Definition: a model"}
5960
# $$
6061
# \hat{y} = f(\mathbf{x})
6162
# $$
6263
#
63-
# - $\mathbf{x}$: input (features)
64-
# - $\hat{y}$: model output (prediction)
65-
# - $f$: model (the rule we learn from data)
64+
# - $\mathbf{x}$: input (features), one example as a point in $\mathbb{R}^d$,
65+
# - $\hat{y}$: model output (prediction),
66+
# - $f$: the model, i.e. the rule we learn from data.
67+
#
68+
# A model is a *function*. Building one means choosing its form, then choosing the
69+
# numbers inside it.
70+
# :::
6671
#
6772
# In practice, we will write:
6873
#
@@ -78,9 +83,13 @@
7883
# - $\boldsymbol{\eta}$: **hyperparameters** (we choose these *before* training)
7984
# - e.g. number of neighbors `k`, depth of a tree, regularization strength
8085
#
81-
# ::: {.callout-note title="Key idea"}
82-
# - Parameters are *learned* from data by the training algorithm.
83-
# - Hyperparameters are *chosen* by us to control model complexity and behavior.
86+
# ::: {.callout-tip title="Key concept: parameters are learned, hyperparameters are chosen"}
87+
# - **Parameters** $\mathbf{w}$ are *learned* from data by the training algorithm.
88+
# - **Hyperparameters** $\boldsymbol{\eta}$ are *chosen* by us, before training, and
89+
# control the model's complexity and behavior.
90+
#
91+
# The notation $\mathbf{w}(\boldsymbol{\eta})$ says this out loud: which parameters we
92+
# end up learning depends on the hyperparameters we picked first.
8493
# :::
8594
#
8695
# Later we will tune hyperparameters more carefully. For now, we’ll use simple defaults and explore how they affect behavior.
@@ -111,6 +120,13 @@
111120
#
112121
# > “Let the data speak; find structure in $\mathbf{x}$ itself.”
113122
#
123+
# ::: {.callout-tip title="Key concept: the task comes from the question, not the data"}
124+
# The same dataset matrix $X$ can serve any of these tasks. What changes is what we ask
125+
# for: a number (regression), a label (classification), or structure in $X$ itself
126+
# (unsupervised). That single choice fixes the target, the family of models we can use,
127+
# *and* the metric we are allowed to judge the result by.
128+
# :::
129+
#
114130
# In this module, we focus on **supervised learning and classification** to get familiar with modeling in `scikit-learn`.
115131
#
116132

@@ -213,6 +229,18 @@
213229
# - `model.fit(X, y)`
214230
# 3. _predict_: generates output
215231
# - `model.predict(X_test)`
232+
#
233+
# ::: {.callout-note title="Key concept: every sklearn model is the same three calls"}
234+
# ```python
235+
# model = Estimator(hyperparameters) # 1. pick the model family and its knobs
236+
# model.fit(X, y) # 2. learn the parameters from data
237+
# y_pred = model.predict(X_new) # 3. predict on new inputs
238+
# ```
239+
#
240+
# The estimator changes; the three lines do not. Everything passed to the constructor is
241+
# a hyperparameter $\boldsymbol{\eta}$, and `fit` is where the parameters $\mathbf{w}$
242+
# come from.
243+
# :::
216244

217245
# %% [markdown] id="sUGXrwTJu0DG"
218246
# A list of sklearn supervised learning methods can be found [here](https://scikit-learn.org/stable/supervised_learning.html).
@@ -231,6 +259,18 @@
231259
#
232260
# There is almost no training phase. The model mostly stores the training data and uses distance at prediction time.
233261
#
262+
# ::: {.callout-note title="Definition: the $k$-nearest-neighbors rule"}
263+
# To predict the label of a new point $\mathbf{x}$:
264+
#
265+
# 1. compute the distance $d(\mathbf{x}, \mathbf{x}_i)$ to every training point,
266+
# 2. keep the $k$ smallest, giving the neighbor set $N_k(\mathbf{x})$,
267+
# 3. predict the majority label among those $k$ neighbors.
268+
#
269+
# There is no fitted $\mathbf{w}$ here: kNN stores the training set and does all its work
270+
# at prediction time. Its behavior is set entirely by hyperparameters, $k$ and the
271+
# distance $d$ (the $L^p$ distance from Notebook 1a).
272+
# :::
273+
#
234274

235275
# %%
236276
# The kNN computation: distances to every training point, the k closest, and
@@ -454,6 +494,18 @@ def plot_decision_boundary(clf, X, y):
454494
# - `p=1` gives Manhattan distance,
455495
# - `p=2` gives Euclidean distance.
456496
#
497+
# ::: {.callout-important title="Key concept: $k$ controls model complexity"}
498+
# $k$ sets how much the model is allowed to trust a single point.
499+
#
500+
# - $k = 1$: every training point rules its own neighborhood. Training accuracy is
501+
# perfect and the boundary chases noise, i.e. **overfitting**.
502+
# - $k$ close to $n$: every prediction is the majority label of nearly the whole dataset.
503+
# The boundary is so smooth it ignores real structure, i.e. **underfitting**.
504+
#
505+
# Nothing in the data tells us the right $k$: we have to *choose* it. Notebook 2b is
506+
# about how to make that choice honestly.
507+
# :::
508+
#
457509
# Run the next cell, then move the sliders.
458510
#
459511

@@ -568,3 +620,30 @@ def plot_knn_model(n_neighbors=5, p=2, weights="uniform"):
568620
# 3. Report train accuracy and test accuracy.
569621
# 4. Compare its boundary to kNN.
570622
# 5. Which model seems more flexible? Which seems easier to explain?
623+
624+
# %% [markdown] id="nb2a-takeaways"
625+
# ## Takeaways
626+
#
627+
# ::: {.callout-tip title="Takeaways from this lecture"}
628+
# 1. **A model is a function**, $\hat{y} = f(\mathbf{x}, \mathbf{w}(\boldsymbol{\eta}))$,
629+
# from features to a prediction, not a black box.
630+
# 2. **Parameters vs. hyperparameters:** $\mathbf{w}$ is *learned* by fitting;
631+
# $\boldsymbol{\eta}$ is *chosen* by us before training.
632+
# 3. **The task comes from the question.** The same $X$ supports regression (predict a
633+
# number), classification (predict a label), and unsupervised learning (find structure
634+
# in $X$ alone).
635+
# 4. **Every sklearn model is the same three calls:** construct with hyperparameters,
636+
# `fit` to learn parameters, `predict` on new inputs.
637+
# 5. **kNN predicts by neighborhood vote:** distances to all training points, keep the $k$
638+
# closest, take the majority label. It stores data instead of fitting parameters.
639+
# 6. **The distance is a hyperparameter too:** Minkowski `p=1` is Manhattan and `p=2` is
640+
# Euclidean, the $L^p$ distances from Notebook 1a, and they give different boundaries
641+
# on the same data.
642+
# 7. **$k$ controls complexity:** small $k$ gives a jagged boundary that overfits, large
643+
# $k$ gives a smooth one that underfits. Notebook 2b turns this into a principled
644+
# choice.
645+
# 8. **A decision boundary makes a model's assumptions visible.** Plot it whenever you
646+
# want to see what a classifier actually believes.
647+
# 9. **A model encodes assumptions about which patterns matter.** kNN assumes nearby
648+
# points share labels; that is a claim about the data, not a fact.
649+
# :::

0 commit comments

Comments
 (0)