Practice Lab
Runnable lab version for web reading.
Linear Models: Exercises
Ten exercises cover affine prediction, least squares, gradients, ridge regularization, logistic and softmax probabilities, standardization, and diagnostics.
Code cell 2
Python snippet
Run the code to see stdout, return values, and errors here.
Runs in your browser sandbox. DeepML does not send this code to the server.
Exercise 1: Affine prediction
Compute one affine prediction.
Code cell 4
Python snippet
Run the code to see stdout, return values, and errors here.
Runs in your browser sandbox. DeepML does not send this code to the server.
Code cell 5
Python snippet
Run the code to see stdout, return values, and errors here.
Runs in your browser sandbox. DeepML does not send this code to the server.
Exercise 2: Least squares
Solve a tiny least-squares problem.
Code cell 7
Python snippet
Run the code to see stdout, return values, and errors here.
Runs in your browser sandbox. DeepML does not send this code to the server.
Code cell 8
Python snippet
Run the code to see stdout, return values, and errors here.
Runs in your browser sandbox. DeepML does not send this code to the server.
Exercise 3: Squared-loss gradient
Compute gradient X.T@(Xw-y).
Code cell 10
Python snippet
Run the code to see stdout, return values, and errors here.
Runs in your browser sandbox. DeepML does not send this code to the server.
Code cell 11
Python snippet
Run the code to see stdout, return values, and errors here.
Runs in your browser sandbox. DeepML does not send this code to the server.
Exercise 4: Gradient step
Take one gradient descent step.
Code cell 13
Python snippet
Run the code to see stdout, return values, and errors here.
Runs in your browser sandbox. DeepML does not send this code to the server.
Code cell 14
Python snippet
Run the code to see stdout, return values, and errors here.
Runs in your browser sandbox. DeepML does not send this code to the server.
Exercise 5: Ridge solution
Compute ridge solution for identity X.
Code cell 16
Python snippet
Run the code to see stdout, return values, and errors here.
Runs in your browser sandbox. DeepML does not send this code to the server.
Code cell 17
Python snippet
Run the code to see stdout, return values, and errors here.
Runs in your browser sandbox. DeepML does not send this code to the server.
Exercise 6: Shrinkage path
Compare ridge coefficients for two lambdas.
Code cell 19
Python snippet
Run the code to see stdout, return values, and errors here.
Runs in your browser sandbox. DeepML does not send this code to the server.
Code cell 20
Python snippet
Run the code to see stdout, return values, and errors here.
Runs in your browser sandbox. DeepML does not send this code to the server.
Exercise 7: Logistic probability
Compute sigmoid and BCE.
Code cell 22
Python snippet
Run the code to see stdout, return values, and errors here.
Runs in your browser sandbox. DeepML does not send this code to the server.
Code cell 23
Python snippet
Run the code to see stdout, return values, and errors here.
Runs in your browser sandbox. DeepML does not send this code to the server.
Exercise 8: Softmax
Compute softmax probabilities.
Code cell 25
Python snippet
Run the code to see stdout, return values, and errors here.
Runs in your browser sandbox. DeepML does not send this code to the server.
Code cell 26
Python snippet
Run the code to see stdout, return values, and errors here.
Runs in your browser sandbox. DeepML does not send this code to the server.
Exercise 9: Standardization
Standardize using train mean and std.
Code cell 28
Python snippet
Run the code to see stdout, return values, and errors here.
Runs in your browser sandbox. DeepML does not send this code to the server.
Code cell 29
Python snippet
Run the code to see stdout, return values, and errors here.
Runs in your browser sandbox. DeepML does not send this code to the server.
Exercise 10: Checklist
Write four linear-model diagnostics.
Code cell 31
Python snippet
Run the code to see stdout, return values, and errors here.
Runs in your browser sandbox. DeepML does not send this code to the server.
Code cell 32
Python snippet
Run the code to see stdout, return values, and errors here.
Runs in your browser sandbox. DeepML does not send this code to the server.
Closing Reflection
Linear models are the best place to learn the full supervised-learning loop because every shape, gradient, regularizer, and diagnostic is visible.