Training a Linear Regression Model

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

Generating Synthetic Data

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

Python
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