Skip to content

Repository files navigation

ml-04-regression

Workflow Guide Python 3.14 MIT

Professional Python project: building and evaluating regression models.

Project Description

This project builds and evaluates regression models. Phase 4 extends a course example, and Phase 5 applies the same workflow to a custom problem, which is the main work in this repository.

The example predicts body_mass_g on the Seaborn penguins dataset from a single body measurement. The Phase 4 modification extends it to a multiple regression on two features and adds a case comparison to test whether the second feature earns its place.

The Phase 5 custom project predicts individual medical insurance charges from age, sex, bmi, number of children, smoking status, and region, using a public dataset of 1,338 records with no missing values. Charges are right-skewed, and one predictor carries most of the signal, so the project compares a plain linear fit against a log-transformed target, polynomial expansion, and regularized models. The dataset is widely circulated but its provenance is not fully documented, so the results are an exercise in model comparison rather than a claim about actual pricing. The full write-up is in docs/index.md.

The work covers:

  • fitting and evaluating regression models
  • using a train/test split to evaluate on unseen data
  • reading regression metrics: R-squared, RMSE, and residual plots
  • comparing feature cases and polynomial degree to choose a model
  • transforming a skewed target and tuning Ridge and ElasticNet regularization

Notebooks

Links:

Command Reference

Show command reference

In a machine terminal (open in your Repos folder)

After you get a copy of this repo in your own GitHub account, open a machine terminal in your Repos folder:

# Replace username with YOUR GitHub username.
git clone https://github.com/gracecode42/ml-04-regression

cd ml-04-regression
code .

In a VS Code terminal

These are listed for convenience. For best results, follow the detailed instructions in pro-analytics-02 guide.

uv self update
uv python pin 3.14
uv lock --upgrade
uv sync --extra dev --extra docs --upgrade

uvx pre-commit install
uvx pre-commit autoupdate

git add -A
uvx pre-commit run --all-files
# repeat if changes were made
uvx pre-commit run --all-files

# run the example module to verify the environment (.venv/)
uv run python -m mlstudio.app_case

# run common chores
uv run ruff format .
uv run ruff check . --fix
uv run python -m pytest --cov=src --cov-report=term-missing
uv run python -m pytest
uv run python -m zensical build

# save progress
git add -A
git commit -m "update"
git push -u origin main

Findings and Visuals

On the insurance data, a plain linear model reaches a test R-squared of 0.7836 with an RMSE of 5,796 dollars. The residual plot is not random. Errors fan out as predicted charges rise, and two separate bands appear at the high end, which points to structure the linear form is not capturing rather than simple imprecision.

Residuals versus predicted charges on the test set

Log-transforming the target reduced the fanning in the lower range but raised RMSE to 7,814 dollars, so it did not help in the units that matter. Polynomial features on the dollar target did. Degree 2 lowered RMSE from 5,796 to 4,551 and raised R-squared to 0.8666, since the product terms let smoking status and bmi interact instead of contributing separately. Degrees 3 and 4 overfit: training RMSE kept falling while test RMSE turned up. Ridge and ElasticNet with cross-validated alpha recovered part of that loss at degree 3, reaching about 4,645, but neither beat 4,551. The final model is the degree 2 polynomial on the dollar target.

Train and test RMSE as polynomial degree increases

Final Model

The chosen model is a degree 2 polynomial regression on the dollar target. It reaches a test R-squared of 0.8666 and an RMSE of 4,551 dollars.

Project Documentation

Additional project instructions, terms, and notes:

docs/index.md

Citation

CITATION.cff

License

MIT

Releases

Contributors

Languages