Lesson Introduction

In this lesson, we'll learn how to train a linear regression model. Linear regression helps predict values based on data. Imagine you have house areas and prices and want to predict the price of a house with an unknown area. That's where linear regression helps.

By the end of this lesson, you will understand linear regression, how to generate and handle synthetic data, and how to use Scikit-Learn to train a linear regression model. You'll also learn to interpret the model's output.

Understanding Linear Regression

Linear regression models the relationship between two variables by fitting a linear equation to the data. One variable is the explanatory variable, often called "feature" and denoted by XX and the other is the dependent variable, often called "target" and denoted by yy.

The linear regression formula is:

y=kX+by = kX + b

Where:

  • b is the y value when X is zero.
  • k (or coefficient) indicates how much y changes for each unit change in X.

Here is an example of some data and two lines trying to fit the data:

We aim to find the "best-fit" line, which minimizes the difference between the actual data points and the predicted values. It means that we need to find the optimal line parameters k and b. This is typically achieved using the least squares method, which finds the line that minimizes the sum of the squared differences between the observed values and the values predicted by the line.

Generating Synthetic Data

To train our model, we need data. We'll generate synthetic (fake) data for learning, like in the previous lesson.

import numpy as np
import pandas as pd

np.random.seed(42)
num_samples = 100
area = np.random.uniform(500, 3500, num_samples)  # House area in square feet

# Assume a linear relationship: price = base_price + (area * price_per_sqft)
base_price = 50000.00
price_per_sqft = 200.00
noise = np.random.normal(0, 25000, num_samples)  # Adding noise
price = base_price + (area * price_per_sqft) + noise

# Output example data
print(f"Area: {area[:5].round(2)}")
# Area: [1623.62 3352.14 2695.98 2295.98  968.06]
print(f"Price: {price[:5].round(2)}")
# Price: [376900.25 712953.4  591490.38 459505.87 238119.39]

# Create DataFrame
df = pd.DataFrame({'Area': area.round(2), 'Price': price.round(2)})

We use NumPy to generate random house areas between 500 and 3500 square feet. The price is calculated based on a base price plus the area multiplied by a fixed rate per square foot, with some random noise.

Again, we use Pandas to organize our data into a DataFrame, a table-like structure for easier data manipulation.

Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal