Search⌘ K
AI Features

Introduction to scikit-learn

Explore how to fit linear regression models using scikit-learn. Learn the estimator workflow, including data splitting, model fitting, prediction, and using pipelines to avoid information leakage. Understand the importance of maintaining proper data shapes and separating training from test data for reliable rent predictions.

A rent forecast must respond to the apartment being priced. Predicting the average rent for every listing ignores that a 950 square-foot, two-bedroom apartment and a 550 square-foot studio serve different markets. A regression model uses feature columns such as sqft and bedrooms to produce a numeric rent prediction for each apartment.

With the target, prediction time, and error costs already documented, scikit-learn gives us a consistent way to train and use that model. An estimator is an object that learns from data through fit() and produces outputs through predict(). A model is an estimator that turns apartment features into predicted rent. A transformer is an object that learns how to change feature columns through fit() and applies that change through transform().

The basic loop has a clear order. We provide past apartment features and known rents to an estimator. The estimator learns from those examples. We then provide features for apartments whose rents were not used for learning, and the estimator returns predicted rents. Later lessons will explain the line that LinearRegression learns. For now, the important contract is how data enters and leaves the workflow.

Features and targets have different shapes

The estimator contract works because features and targets have separate jobs. X holds the input feature table. Each row represents one apartment, while each column represents one available feature such as sqft or bedrooms. y holds the one rent value to predict for each row.

For regression, X must be two-dimensional even when the model uses only one feature. A common mistake is selecting df["sqft"], which creates a one-dimensional pandas Series. Selecting ...