Spaces:
Running
Running
File size: 18,925 Bytes
2d4a671 37f75d3 2d4a671 9dc8320 2d4a671 37f75d3 2d4a671 37f75d3 2d4a671 37f75d3 2d4a671 37f75d3 2d4a671 37f75d3 2d4a671 37f75d3 2d4a671 37f75d3 2d4a671 37f75d3 2d4a671 37f75d3 2d4a671 7497033 2d4a671 37f75d3 2d4a671 37f75d3 2d4a671 37f75d3 2d4a671 37f75d3 2d4a671 37f75d3 2d4a671 37f75d3 2d4a671 37f75d3 2d4a671 37f75d3 2d4a671 37f75d3 2d4a671 37f75d3 2d4a671 1a64053 2d4a671 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 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 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 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 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 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 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 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 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 |
# /// script
# requires-python = ">=3.10"
# dependencies = [
# "marimo",
# "matplotlib==3.10.1",
# "numpy==2.2.4",
# "drawdata==0.3.7",
# "scikit-learn==1.6.1",
# "polars==1.26.0",
# ]
# ///
import marimo
__generated_with = "0.12.5"
app = marimo.App(width="medium", app_title="Logistic Regression")
@app.cell(hide_code=True)
def _(mo):
mo.md(
r"""
# Logistic Regression
_This notebook is a computational companion to ["Probability for Computer Scientists"](https://chrispiech.github.io/probabilityForComputerScientists/en/part5/log_regression/), by Stanford professor Chris Piech._
Logistic regression learns a function approximating $P(y|x)$, and can be used to make a classifier. It makes the central assumption that $P(y|x)$ can be approximated as a sigmoid function applied to a linear combination of input features. It is particularly important to learn because logistic regression is the basic building block of artificial neural networks.
"""
)
return
@app.cell(hide_code=True)
def _(mo):
mo.md(
r"""
## The Binary Classification Problem
Imagine situations where we would like to know:
- The eligibility of getting a bank loan given the value of credit score ($x_{credit\_score}$) and monthly income ($x_{income}$)
- Identifying a tumor as benign or malignant given its size ($x_{tumor\_size}$)
- Classifying an email as promotional given the number of occurrences for some keywords like {'win', 'gift', 'discount'} ($x_{n\_win}$, $x_{n\_gift}$, $x_{n\_discount}$)
- Finding a monetary transaction as fraudulent given the time of occurrence ($x_{time\_stamp}$) and amount ($x_{amount}$)
These problems occur frequently in real life & can be dealt with machine learning. All such problems come under the umbrella of what is known as Classification. In each scenario, only one of the two possible outcomes can occur, hence these are specifically known as Binary Classification problems.
### How Does A Machine Perform Classification?
During the inference, the goal is to have the ML model predict the class label for a given set of feature values.
Specifically, a binary classification model estimates two probabilities $p_0$ & $p_1$ for 'class-0' and 'class-1' respectively where $p_0 + p_1 = 1$.
The predicted label depends on $\max(p_0, p_1)$ i.e., it's the one which is most probable based on the given features.
In logistic regression, $p_1$ (i.e., success probability) is compared with a predefined threshold $p$ to predict the class label like below:
$$\text{predicted class} =
\begin{cases}
1, & \text{if } p_1 \geq p \\
0, & \text{otherwise}
\end{cases}$$
To keep the notation simple and consistent, we will denote the success probability as $p$, and failure probability as $(1-p)$ instead of $p_1$ and $p_0$ respectively.
"""
)
return
@app.cell(hide_code=True)
def _(mo):
mo.md(
r"""
## Why NOT Linear Regression?
Can't we really use linear regression to address classification? The answer is NO! The key issue is that probabilities must be between 0 and 1 and linear regression can output any real number.
If we tried using linear regression directly:
$$p = \beta_0 + \beta_1 \cdot x_{feature}$$
This creates a problem: the right side can produce any value in $\mathbb{R}$ (all real numbers), but a probability $p$ must be confined to the range $(0,1)$.
Can we convert $(\beta_0 + \beta_1 \cdot x_{tumor\_size})$ to something belonging to $(0,1)$? That may work as an estimate of a probability! The answer is YES!
We need a converter (a function), say, $g()$ that will connect $p \in (0,1)$ to $(\beta_0 + \beta_1 \cdot x_{tumor\_size}) \in \mathbb{R}$.
The solution is to use a "link function" that maps from any real number to a valid probability range. This is where the sigmoid function comes in.
"""
)
return
@app.cell(hide_code=True)
def _(mo, np, plt):
# plot sigmoid to evidentiate above statements
_fig, ax = plt.subplots(figsize=(10, 6))
# x values
x = np.linspace(-10, 10, 1000)
# sigmoid formula
def sigmoid(z):
return 1 / (1 + np.exp(-z))
y = sigmoid(x)
# plot
ax.plot(x, y, 'b-', linewidth=2)
ax.axhline(y=0, color='k', linestyle='-', alpha=0.3)
ax.axhline(y=1, color='k', linestyle='-', alpha=0.3)
ax.axhline(y=0.5, color='r', linestyle='--', alpha=0.5)
# vertical line at x=0
ax.axvline(x=0, color='k', linestyle='-', alpha=0.3)
# annotations
ax.text(1, 0.85, r'$\sigma(z) = \frac{1}{1 + e^{-z}}$', fontsize=14)
ax.text(-9, 0.1, 'As z β -β, Ο(z) β 0', fontsize=12)
ax.text(3, 0.9, 'As z β β, Ο(z) β 1', fontsize=12)
ax.text(0.5, 0.4, 'Ο(0) = 0.5', fontsize=12)
# labels and title
ax.set_xlabel('z', fontsize=14)
ax.set_ylabel('Ο(z)', fontsize=14)
ax.set_title('Sigmoid Function', fontsize=16)
# axis limits set
ax.set_xlim(-10, 10)
ax.set_ylim(-0.1, 1.1)
# grid
ax.grid(True, alpha=0.3)
mo.mpl.interactive(_fig)
return ax, sigmoid, x, y
@app.cell(hide_code=True)
def _(mo):
mo.md(
r"""
**Figure**: The sigmoid function maps any real number to a value between 0 and 1, making it perfect for representing probabilities.
/// note
For more information about the sigmoid function, head over to [this detailed notebook](http://marimo.app/https://github.com/marimo-team/deepml-notebooks/blob/main/problems/problem-22/notebook.py) for more insights.
///
"""
)
return
@app.cell(hide_code=True)
def _(mo):
mo.md(
r"""
## The Core Concept (math)
Logistic regression models the probability of class 1 using the sigmoid function:
$$P(Y=1|X=x) = \sigma(z) \text{ where } z = \theta_0 + \sum_{i=1}^m \theta_i x_i$$
The sigmoid function $\sigma(z)$ transforms any real number into a probability between 0 and 1:
$$\sigma(z) = \frac{1}{1+ e^{-z}}$$
This can be written more compactly using vector notation:
$$P(Y=1|\mathbf{X}=\mathbf{x}) =\sigma(\mathbf{\theta}^T\mathbf{x}) \quad \text{ where we always set $x_0$ to be 1}$$
$$P(Y=0|\mathbf{X}=\mathbf{x}) =1-\sigma(\mathbf{\theta}^T\mathbf{x}) \quad \text{ by total law of probability}$$
Where $\theta$ represents the model parameters that need to be learned from data, and $x$ is the feature vector (with $x_0=1$ to account for the intercept term).
> **Note:** For the detailed mathematical derivation of how these parameters are learned through Maximum Likelihood Estimation (MLE) and Gradient Descent (GD), please refer to [Chris Piech's original material](https://chrispiech.github.io/probabilityForComputerScientists/en/part5/log_regression/). The mathematical details are elegant but beyond the scope of this notebook topic (which is confined to Logistic Regression).
"""
)
return
@app.cell(hide_code=True)
def _(mo):
mo.md(
r"""
### Linear Decision Boundary
A key characteristic of logistic regression is that it creates a linear decision boundary. When the model predicts, it's effectively dividing the feature space with a straight line (in 2D) or hyperplane (in higher dimensions). It is actually a straight line (of the form $y = mx + c$).
Recall the prediction rule:
$$\text{predicted class} =
\begin{cases}
1, & \text{if } p \geq \theta_0 + \theta_1 \cdot x_{tumor\_size} \Rightarrow \log\frac{p}{1-p} \\
0, & \text{otherwise}
\end{cases}$$
For a two-feature model, the decision boundary where $P(Y=1|X=x) = 0.5$ occurs at:
$$\theta_0 + \theta_1 x_1 + \theta_2 x_2 = 0$$
A simple logistic regression predicts the class label by identifying the regions on either side of a straight line (or hyperplane in general), hence it's a _linear_ classifier.
This linear nature makes logistic regression effective for linearly separable classes but limited when dealing with more complex patterns.
"""
)
return
@app.cell(hide_code=True)
def _(mo):
mo.md("""### Visual: Linear Separability and Classification""")
return
@app.cell(hide_code=True)
def _(mo, np, plt):
# show relevant comparison to the above concepts/statements
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(12, 5))
# Linear separable data
np.random.seed(42)
X1 = np.random.randn(100, 2) - 2
X2 = np.random.randn(100, 2) + 2
ax1.scatter(X1[:, 0], X1[:, 1], color='blue', alpha=0.5)
ax1.scatter(X2[:, 0], X2[:, 1], color='red', alpha=0.5)
# Decision boundary (line)
ax1.plot([-5, 5], [5, -5], 'k--', linewidth=2)
ax1.set_xlim(-5, 5)
ax1.set_ylim(-5, 5)
ax1.set_title('Linearly Separable Classes')
# non-linear separable data
radius = 2
theta = np.linspace(0, 2*np.pi, 100)
# Outer circle points (class 1)
outer_x = 3 * np.cos(theta)
outer_y = 3 * np.sin(theta)
# Inner circle points (class 2)
inner_x = 1.5 * np.cos(theta) + np.random.randn(100) * 0.2
inner_y = 1.5 * np.sin(theta) + np.random.randn(100) * 0.2
ax2.scatter(outer_x, outer_y, color='blue', alpha=0.5)
ax2.scatter(inner_x, inner_y, color='red', alpha=0.5)
# Attempt to draw a linear boundary (which won't work well) proving the point
ax2.plot([-5, 5], [2, 2], 'k--', linewidth=2)
ax2.set_xlim(-5, 5)
ax2.set_ylim(-5, 5)
ax2.set_title('Non-Linearly Separable Classes')
fig.tight_layout()
mo.mpl.interactive(fig)
return (
X1,
X2,
ax1,
ax2,
fig,
inner_x,
inner_y,
outer_x,
outer_y,
radius,
theta,
)
@app.cell(hide_code=True)
def _(mo):
mo.md(r"""**Figure**: On the left, the classes are linearly separable as the boundary is a straight line. However, they are not linearly separable on the right, where no straight line can properly separate the two classes.""")
return
@app.cell(hide_code=True)
def _(mo):
mo.md(
r"""
Logistic regression is typically trained using MLE - finding the parameters $\theta$ that make our observed data most probable.
The optimization process generally uses GD (or its variants) to iteratively improve the parameters. The gradient has a surprisingly elegant form:
$$\frac{\partial LL(\theta)}{\partial \theta_j} = \sum_{i=1}^n \left[
y^{(i)} - \sigma(\theta^T x^{(i)})
\right] x_j^{(i)}$$
This shows that the update to each parameter depends on the prediction error (actual - predicted) multiplied by the feature value.
For those interested in the complete mathematical derivation, including log likelihood calculation and the detailed steps of GD (and relevant pseudocode followed for training), please see the [original lecture notes](https://chrispiech.github.io/probabilityForComputerScientists/en/part5/log_regression/).
"""
)
return
@app.cell(hide_code=True)
def _(controls, mo, widget):
# create the layout
mo.vstack([
mo.md("## Interactive drawing demo\nDraw points of two different classes and see how logistic regression separates them. _The interactive demo was adapted and improvised from [Vincent Warmerdam's](https://github.com/koaning) code [here](https://github.com/probabl-ai/youtube-appendix/blob/main/04-drawing-data/notebook.ipynb)_."),
controls,
widget
])
return
@app.cell(hide_code=True)
def _(LogisticRegression, mo, np, plt, run_button, widget):
warning_msg = mo.md(""" /// warning
Need more data, please draw points of at least two different colors in the scatter widget
""")
# mo.stop if button isn't clicked yet
mo.stop(
not run_button.value,
mo.md(""" /// tip
click 'Run Logistic Regression' to see the model
""")
)
# get data from widget (can also use as_pandas)
df = widget.data_as_polars
# display appropriate warning
mo.stop(
df.is_empty() or df['color'].n_unique() < 2,
warning_msg
)
# extract features and labels
X = df[['x', 'y']].to_numpy()
y_colors = df['color'].to_numpy()
# fit logistic regression model
model = LogisticRegression()
model.fit(X, y_colors)
# create grid for the viz
x_min, x_max = X[:, 0].min() - 0.5, X[:, 0].max() + 0.5
y_min, y_max = X[:, 1].min() - 0.5, X[:, 1].max() + 0.5
xx, yy = np.meshgrid(
np.linspace(x_min, x_max, 100),
np.linspace(y_min, y_max, 100)
)
# get probability predictions
Z = model.predict_proba(np.c_[xx.ravel(), yy.ravel()])[:, 1]
Z = Z.reshape(xx.shape)
# create figure
_fig, ax_fig = plt.subplots(figsize=(12, 8))
# plot decision boundary (probability contours)
contour = ax_fig.contourf(
xx, yy, Z,
levels=np.linspace(0, 1, 11),
alpha=0.7,
cmap="RdBu_r"
)
# plot decision boundary line (probability = 0.5)
ax_fig.contour(
xx, yy, Z,
levels=[0.5],
colors='k',
linewidths=2
)
# plot the data points (use same colors as in the widget)
ax_fig.scatter(X[:, 0], X[:, 1], c=y_colors, edgecolor='k', s=80)
# colorbar
plt.colorbar(contour, ax=ax_fig)
# labels and title
ax_fig.set_xlabel('x')
ax_fig.set_ylabel('y')
ax_fig.set_title('Logistic Regression')
# model params
coef = model.coef_[0]
intercept = model.intercept_[0]
equation = f"log(p/(1-p)) = {intercept:.2f} + {coef[0]:.3f}xβ + {coef[1]:.3f}xβ"
# relevant info in regards to regression
model_info = mo.md(f"""
### Logistic regression model
**Equation**: {equation}
**Decision boundary**: probability = 0.5
**Accuracy**: {model.score(X, y_colors):.2f}
""")
# show results vertically stacked
mo.vstack([
mo.mpl.interactive(_fig),
model_info
])
return (
X,
Z,
ax_fig,
coef,
contour,
df,
equation,
intercept,
model,
model_info,
warning_msg,
x_max,
x_min,
xx,
y_colors,
y_max,
y_min,
yy,
)
@app.cell(hide_code=True)
def _(mo):
mo.md(
r"""
## π€ Key Takeaways
Click on the statements below that you think are correct to verify your understanding:
/// details | Logistic regression tries to find parameters (ΞΈ) that minimize the error between predicted and actual values using ordinary least squares.
β **Incorrect.** Logistic regression uses maximum likelihood estimation (MLE), not ordinary least squares. It finds parameters that maximize the probability of observing the training data, which is different from minimizing squared errors as in linear regression.
///
/// details | The sigmoid function maps any real number to a value between 0 and 1, which allows logistic regression to output probabilities.
β
**Correct!** The sigmoid function Ο(z) = 1/(1+e^(-z)) takes any real number as input and outputs a value between 0 and 1. This is perfect for representing probabilities and is a key component of logistic regression.
///
/// details | The decision boundary in logistic regression is always a straight line, regardless of the data's complexity.
β
**Correct!** Standard logistic regression produces a linear decision boundary (a straight line in 2D or a hyperplane in higher dimensions). This is why it works well for linearly separable data but struggles with more complex patterns, like concentric circles (as you might've noticed from the interactive demo).
///
/// details | The logistic regression model params are typically initialized to random values and refined through gradient descent.
β
**Correct!** Parameters are often initialized to zeros or small random values, then updated iteratively using gradient descent (or ascent for maximizing likelihood) until convergence.
///
/// details | Logistic regression can naturally handle multi-class classification problems without any modifications.
β **Incorrect.** Standard logistic regression is inherently a binary classifier. To handle multi-class classification, techniques like one-vs-rest or softmax regression are typically used.
///
"""
)
return
@app.cell(hide_code=True)
def _(mo):
mo.md(
r"""
## Summary
So we've just explored logistic regression. Despite its name (seriously though, why not call it "logistic classification"?), it's actually quite elegant in how it transforms a simple linear model into a powerful decision _boundary_ maker.
The training process boils down to finding the values of ΞΈ that maximize the likelihood of seeing our training data. What's super cool is that even though the math looks _scary_ at first, the gradient has this surprisingly simple form: just the error (y - predicted) multiplied by the feature values.
Two key insights to remember:
- Logistic regression creates a _linear_ decision boundary, so it works great for linearly separable classes but struggles with more _complex_ patterns
- It directly gives you probabilities, not just classifications, which is incredibly useful when you need confidence measures
"""
)
return
@app.cell(hide_code=True)
def _(mo):
mo.md(
r"""
Additional resources referred to:
- [Logistic Regression Tutorial by _Koushik Khan_](https://koushikkhan.github.io/resources/pdf/tutorials/logistic_regression_tutorial.pdf)
"""
)
return
@app.cell(hide_code=True)
def _(mo):
mo.md(r"""Appendix (helper code)""")
return
@app.cell
def _():
import marimo as mo
return (mo,)
@app.cell
def init_imports():
# imports for our notebook
import numpy as np
import matplotlib.pyplot as plt
from drawdata import ScatterWidget
from sklearn.linear_model import LogisticRegression
# for consistent results
np.random.seed(42)
# nicer plots
plt.style.use('seaborn-v0_8-darkgrid')
return LogisticRegression, ScatterWidget, np, plt
@app.cell(hide_code=True)
def _(ScatterWidget, mo):
# drawing widget
widget = mo.ui.anywidget(ScatterWidget())
# run_button to run model
run_button = mo.ui.run_button(label="Run Logistic Regression", kind="success")
# stack controls
controls = mo.hstack([run_button])
return controls, run_button, widget
if __name__ == "__main__":
app.run()
|