A hands-on introduction to linear regression for data science. You will fit simple and multiple regression models with scikit-learn and statsmodels, see where linear regression breaks down, and learn how to handle categorical predictors. The notebooks alternate between worked lessons and short exercises so you can practice each idea as you go.
By the end of this repository, you should be able to:
- Fit and interpret a simple linear regression model with scikit-learn.
- Extend regression to several predictors and read the resulting coefficients.
- Recognize the assumptions and limitations of linear regression.
- Encode and interpret categorical variables in a regression model.
- Fit an OLS model with statsmodels and read its statistical summary.
Work through the notebooks in order. The lessons introduce each concept, and the exercise notebooks let you practice before moving on.
| File / Folder | Description |
|---|---|
| 1 - Simple Linear Regression (scikit-learn) | Fit your first linear regression model with scikit-learn. |
| 2 - Limitations of Linear Regression | Where linear regression breaks down, and the assumptions behind it. |
| 3 - Exercise: Simple Linear Regression | Practice simple linear regression on your own. |
| 4 - Multiple Linear Regression (scikit-learn) | Model a target from several predictors at once. |
| 5 - Exercise: Multiple Linear Regression | Practice multiple linear regression on your own. |
| 6 - Categorical Variables | Encode and interpret categorical predictors in a regression. |
| 7 - Linear Regression with statsmodels | Fit an OLS model and read its statistical summary. |
| File / Folder | Description |
|---|---|
| Data | Datasets used across the notebooks (car prices and car seats). |
| Assets | Figures displayed in the notebooks. |
| Solutions | Worked solutions, added later in the course. |
| 3D Regression Plot | Standalone script that renders a 3D multiple-regression surface. |
| pyproject.toml | Project configuration and dependencies. |
| uv.lock | Dependency lock file. |
Note
Throughout these steps, text in angle brackets like <repo-name> is a placeholder. Replace it, including the < > brackets, with your own value. For example, cd <repo-name> becomes cd ds-linear-regression.
Click Use this template on GitHub.
When creating the repository:
- Set yourself as the Owner
- Choose a repository name
- Disable Include all branches
- Click Create repository
Important
If you are working in pairs or groups, only one person should complete this step.
If working with teammates:
- Open the repository on GitHub
- Go to Settings → Collaborators
- Add your teammates as collaborators
- Share the repository link with your team
Teammates should accept the invitation before continuing.
Copy the SSH URL from the Code button on GitHub, then run:
git clone <copied-ssh-url>The copied SSH URL will look like [email protected]:<your-username>/<repo-name>.git.
This installs all dependencies and creates a virtual environment in .venv/.
cd <repo-name>
uv syncNote
Make sure you open VS Code from the project root so it automatically detects the environment created by uv sync.
Launch VS Code in the project root folder:
code .Then open a notebook and select the Python environment created by uv sync as the kernel.
- Scikit-Learn linear models guide: The official user guide for linear regression and related models.
- Statsmodels regression docs: OLS and other linear models, with full statistical summaries.
- Seaborn regression plots: Visualizing linear relationships between variables.
- An Introduction to Statistical Learning: A free textbook whose chapter on linear regression covers the theory in depth (R and Python editions).
- Ordinary Least Squares example (scikit-learn): A worked example fitting LinearRegression on a real dataset.