| Feature | Value | Coefficient | Contribution |
|---|---|---|---|
| Hours studied (\(x_1\)) | 5 | 4 | 20 |
| Practice sets (\(x_2\)) | 6 | 2 | 12 |
| Intercept (\(b\)) | 45 |
The predicted grade is \(20+12+45=77\).
\[\hat{y}=w_1x_1+\cdots+w_dx_d+b=\mathbf{w}^{\mathsf T}\mathbf{x}+b\]
fitFor a linear model, fit learns the coefficients and intercept:
The objective includes the loss and, when used, a regularization penalty.
A residual is the observed target minus the prediction: \(y-\hat{y}\).
| Observed grade \(y\) | Prediction \(\hat{y}\) | Residual | Squared error |
|---|---|---|---|
| 70 | 68 | 2 | 4 |
| 70 | 66 | 4 | 16 |
\[\mathrm{MSE}=\frac{1}{n}\sum_{i=1}^{n}(y_i-\hat{y}_i)^2=\frac{4+16}{2}=10\]
A linear model uses \(\hat{y}=3x+10\).
| Input \(x\) | Observed target \(y\) | Prediction | Residual | Squared error |
|---|---|---|---|---|
| 2 | 18 | ? | ? | ? |
| 5 | 20 | ? | ? | ? |
Ridge: Loss plus regularizationLinearRegression minimizes squared-error loss.Ridge adds regularization: a penalty that discourages large coefficients.In this course, we’ll use Ridge for linear regression.
Ridge minimizes\[\underbrace{\sum_{i=1}^{n}(y_i-\hat{y}_i)^2}_{\text{fit the training targets}} +\underbrace{\alpha\sum_{j=1}^{d}w_j^2}_{\text{discourage large coefficients}}\]
alpha controls the strength of the penalty.alpha is adjusted too.alpha \(\rightarrow\) stronger regularization \(\rightarrow\) smaller coefficients; greater risk of underfitting.alpha \(\rightarrow\) weaker regularization; greater risk of overfitting.alpha using cross-validation on training data; reserve the test set for final evaluation.The pipeline fits the scaler separately within each cross-validation fold.
StandardScaler, one unit corresponds to one training-set standard deviation of the original feature.\[z=\mathbf{w}^{\mathsf T}\mathbf{x}+b \qquad p=\sigma(z)=\frac{1}{1+e^{-z}}\]
decision_function returns the raw score \(z\).predict_proba returns probabilities in the order given by classes_; \(p\) corresponds to classes_[1].fit work for logistic regression?We want to classify an image as cat or dog. The true class is cat.
| Model | \(P(\text{cat})\) | \(P(\text{dog})\) |
|---|---|---|
| A | 0.50 | 0.50 |
| B | 0.03 | 0.97 |
| C | 0.90 | 0.10 |
Which ranking is most appropriate for the penalties these models should receive? Order penalties from smallest to largest.
Let \(p_{\text{true}}\) be the probability assigned to the true class.
\[L=-\log(p_{\text{true}})\]
Here and throughout, \(\log\) is the natural logarithm.
The true class is cat, so \(L=-\log(P(\text{cat}))\).
| Model | \(P(\text{cat})\) | Log loss | Interpretation |
|---|---|---|---|
| A | 0.50 | 0.69 | Uncertain |
| B | 0.03 | 3.51 | Confidently wrong |
| C | 0.90 | 0.11 | Confidently correct |
Increasing the probability assigned to the true class reduces the loss.
Encode cat as \(y=1\) and dog as \(y=0\). Let \(p=P(y=1\mid\mathbf{x})\).
\[L=-\left[y\log(p)+(1-y)\log(1-p)\right]\]
Dataset log loss is the average loss across examples. Logistic regression minimizes training log loss plus a regularization penalty.
For \(K\) classes, let \(\hat p_k\) be the predicted probability of class \(k\). The one-hot target has \(y_k=1\) for the true class and 0 otherwise.
\[L=-\sum_{k=1}^{K} y_k\log(\hat p_k)=-\log(\hat p_{\text{true}})\]
For class order cat, dog, wild:
The same idea applies: reward probability assigned to the true class.
A binary classifier has a mean training log loss of 0.69. Has it learned much?
alpha vs. C| Model | Stronger regularization | Weaker regularization |
|---|---|---|
Ridge |
Increase alpha |
Decrease alpha |
LogisticRegression |
Decrease C |
Increase C |
C controls inverse regularization strength.predict_proba value reflects the model’s estimate, not a guarantee.| Model | Fitting objective | Default .score() |
|---|---|---|
Ridge |
Squared-error loss + penalty | \(R^2\) |
LogisticRegression |
Log loss + penalty | Accuracy |
fit uses its objective to learn coefficients from the training data.score evaluates predictions; it need not report the fitting loss.cross_validate uses the estimator’s .score().Select all of the following statements which are TRUE.
alpha of Ridge is likely to decrease model complexity.Ridge can be used with datasets that have multiple features.Ridge, we learn one coefficient per training example.Select all of the following statements which are TRUE.
C hyperparameter increases model complexity.fit what counts as a better prediction.alpha or smaller C means a stronger penalty.A few big questions remain:
So far, we have worked with various transformers and supervised machine learning models. The goal of this activity is collaboratively complete tables that provide an overview of
(This will serve as a handy reference for your upcoming exam and beyond!)
For strengths and weaknesses, some things to consider are:
Fill in the following table with at least one entry per box.
| Model | Strengths | Weaknesses | Key hyperparameters |
|---|---|---|---|
| decision tree | |||
| \(k\)-NN | |||
| RBF SVM | |||
| linear models |
| Transformation | Purpose | Use cases | Key consideration |
|---|---|---|---|
| Imputation | |||
| Scaling | |||
| One-hot encoding | |||
| Ordinal encoding | |||
| Bag-of-words encoding |
Given an input, the probability that it belongs to class \(j \in \{1, 2, \dots, K\}\) is calculated using the softmax function:
\(P(y = j \mid x_i) = \frac{e^{w_j^\top x_i + b_j}}{\sum_{k=1}^{K} e^{w_k^\top x_i + b_k}}\)
Compute Probabilities:
For each class \(j\), compute the probability \(P(y = j \mid x_i)\) using the softmax function.
Select the Class with the Highest Probability:
The predicted class \(\hat{y}\) is:
\(\hat{y} = \arg \max_{j \in \{1, \dots, K\}} P(y = j \mid x_i)\)
| Aspect | Binary Logistic Regression | Multinomial Logistic Regression |
|---|---|---|
| Target variable | 2 classes (binary) | More than 2 classes (multi-class) |
| Getting probabilities | Sigmoid | Softmax |
| parameters | \(d\) weights, one per feature and the bias term | \(d\) weights and a bias term per class |
| Output | Single probability | Probability distribution over classes |
| Use case | Binary classification (e.g., spam detection) | Multi-class classification (e.g., flower species) |
For this demo, each student should click this link to create a new repo in their accounts, then clone that repo locally to follow along with the demo from today.