Lesson Introduction

Hi there! Today, we're going to learn how to apply Linear Regression to a real dataset. Working with real data shows us how machine learning solves real problems. We'll use the California Housing Dataset. By the end of this lesson, you'll know how to use Linear Regression on a real dataset and understand the results.

Understanding the California Housing Dataset

Before diving into the code, let's understand the dataset we'll be working with. The California Housing Dataset is based on data from the 1990 California census. It contains information about various factors affecting housing prices in different districts of California.

Here's a quick overview of the columns in the dataset:

  • MedInc: Median income in block group
  • HouseAge: Median house age in block group
  • AveRooms: Average number of rooms per household
  • AveBedrms: Average number of bedrooms per household
  • Population: Block group population
  • AveOccup: Average household size
  • Latitude: Block group latitude
  • Longitude: Block group longitude
  • MedHouseVal: Median house value for California districts (This is our target variable)
Loading and Preparing the Data: Part 1

First, let's load our data. Think of this step as getting all the ingredients ready before cooking. Here's the code to load the dataset:

Python
import numpy as np
import pandas as pd
from sklearn.datasets import fetch_california_housing

# Load the California Housing dataset
california = fetch_california_housing(as_frame=True)
df = california.frame

print(df.head())  # Display the first five rows
# Output:
#    MedInc  HouseAge  AveRooms  AveBedrms  Population  AveOccup  Latitude  Longitude  MedHouseVal
# 0  8.3252      41.0  6.984127   1.023810      322.0  2.555556     37.88    -122.23        4.526
# 1  8.3014      21.0  6.238137   0.971880     2401.0  2.109842     37.86    -122.22        3.585
# 2  7.2574      52.0  8.288136   1.073446      496.0  2.802260     37.85    -122.24        3.521
# 3  5.6431      52.0  5.817352   1.073059      558.0  2.547945     37.85    -122.25        3.413
# 4  3.8462      52.0  6.281853   1.081081      565.0  2.181467     37.85    -122.25        3.422

We used the fetch_california_housing function to load the dataset and convert it to a Pandas DataFrame for easier handling.

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