⚡ Linear Regression with sklearn
These are Noah's notes on how to implement a linear regression model using the scikit-learn library. This demonstration will help you understand how scikit-learn simplifies the process of training, evaluating, and using linear regression models.
The interactive notebook includes:
- Synthetic data generated from a line we plant ourselves ($y = 4 + 3x$), so the result can be checked.
- Splitting the data into training and test sets with
train_test_split. - Training a
LinearRegressionmodel with a single call tofit(), and comparing what it learns with the planted line. - Evaluation on unseen data with MSE and R², and a comparison with the training error.
- Visualization of the training data, the test data and the regression line.
- Predictions for new data, and the difference between interpolation and extrapolation.
Want to modify the code and experiment on your own?
Open the notebook in Google Colab
Bibliography
- scikit-learn API Reference –
LinearRegression: Estimator parameters, attributes, and practical notes. https://scikit-learn.org/stable/modules/generated/sklearn.linear_model.LinearRegression.html - scikit-learn User Guide – Train/Test Split: Best practices for
train_test_splitand validation strategies. https://scikit-learn.org/stable/modules/cross_validation.html#cross-validation - scikit-learn Metrics – Regression: Definitions of the MSE and R² metrics used in the example. https://scikit-learn.org/stable/modules/model_evaluation.html#regression-metrics
- NumPy – Random sampling:
np.random.rand,np.random.randnandnp.random.seed, used to generate the synthetic data. https://numpy.org/doc/stable/reference/random/index.html