Building Your First Insurance Cost Prediction Model with Simple Linear Regression
Introduction & Lesson Overview
Welcome back! In the last lesson, you learned how to identify which factors most strongly influence insurance costs in the PredictHealth dataset. You explored correlations, visualized relationships, and compared group averages to spot the most important predictors of insurance charges. Now, you are ready to take the next step: building your very first cost prediction model.
In this lesson, you will learn how to use simple linear regression to predict insurance charges based on a single feature — age. By the end of this lesson, you will know how to prepare your data for modeling, train a regression model, evaluate its performance, visualize the results, and use the model to make predictions for new customers. This is a key skill in data science and will help you understand how predictive models work in real-world insurance scenarios.
Understanding Simple Linear Regression
Simple linear regression is a basic but powerful tool in data science. It helps you predict a numeric outcome (like insurance charges) using just one input feature (like age). The model fits a straight line to the data, which can be described by the equation:
Here, y is the predicted value (insurance charges), x is the input feature (age), the slope shows how much y changes for each unit increase in x, and the intercept is the value of y when x is zero.
In the context of PredictHealth, you will use age as the predictor. This means you are asking, "If I know a customer's age, how much can I expect their insurance cost to be?" The regression model will find the best-fitting line through the data so you can make these predictions.
Data Preparation For Regression
Before building a prediction model, it is important to select the right variables. In previous lessons, you discovered that age is one of the features most strongly related to insurance charges. For this first regression model, you will use age as the input (feature) and charges as the output (target) you want to predict.
To prepare the data, you need to create two variables: X for the feature and y for the target. In this case, X will be a DataFrame containing the age column, and y will be a Series containing the charges column. Here is how you can do this:
This code selects the age column as the feature and the charges column as the target. Remember, you are using double square brackets for X to keep it as a DataFrame, which is required by scikit-learn. This step sets up your data for the regression model.

