# Your First Model: Linear Regression

Fit a straight line through examples and use it to guess: first by hand, so the idea is clear, then with scikit-learn's LinearRegression and its fit and predict methods. You'll split the data so the model is tested on homes it never saw, and add more features with care about what their numbers do and don't mean. The running example is a town's home sales.

Updated: November 19, 2026

## [A Line Through the Points](https://www.ask-codey.com/courses/machine-learning-python/topics/ml-a-line-through-points)

1. [Fitting a Line](https://www.ask-codey.com/lessons/ml-fitting-a-line) (Apprentice): A straight line through a scatter plot of home sales turns a home's size into a price guess, the intercept plus the slope times the size, and least squares picks the best line, the one whose squared errors add up to the least, which Python's statistics.linear_regression finds for you.

## [Linear Regression in scikit-learn](https://www.ask-codey.com/courses/machine-learning-python/topics/ml-linear-regression-in-scikit-learn)

1. [Your First scikit-learn Model: fit and predict](https://www.ask-codey.com/lessons/ml-fit-and-predict) (Journeyman): Every scikit-learn model follows the same three steps, make it, fit(X, y) to learn from examples, and predict(X) to guess for new ones, with the features X as a table even when there's only one, and a linear regression's learned slope and intercept kept in coef\_ and intercept\_.
2. [Splitting Data with train_test_split](https://www.ask-codey.com/lessons/ml-train-test-split) (Journeyman): train_test_split shuffles the examples and holds a share back as test data (test_size=0.2 holds back a fifth, and 25% when no size is given), random_state fixes the shuffle so every run splits the same way, and the split comes before anything learns, so the test homes stay unseen.
3. [More Than One Feature](https://www.ask-codey.com/lessons/ml-several-features) (Journeyman): A linear regression with several features learns one coefficient for each, read as the change in the guess for one more unit of that feature with the others held the same; a coefficient's size isn't its importance when the units differ, and features that move together, such as bedrooms and size, share the credit.

## [Exercise: Home Price Guesser](https://www.ask-codey.com/courses/machine-learning-python/topics/exercise-ml-home-price-guesser)

1. [Exercise: Home Price Guesser](https://www.ask-codey.com/lessons/exercise-ml-home-price-guesser) (Journeyman): Write three functions that split Maple Grove's 400 home sales into training and test homes with no home on both sides, train a LinearRegression on the training homes, and guess prices for new homes, and let pytest check the split, the model and the guesses.

Source: https://www.ask-codey.com/courses/machine-learning-python/chapters/ml-linear-regression
