Predict California House Prices

Build a machine learning model to predict California house prices.

Introduction

30 Second Summary

Every house has a price tag, but what determines it? Location, size, neighborhood income, and dozens of other factors all play a role. Data scientists use these patterns to predict prices before a house even hits the market.

In this project, you will build a machine learning model that predicts California house prices using real census data. You will load and explore the dataset, visualize geographic patterns, train a model, and measure how accurately it predicts.

What You'll Build

A complete machine learning pipeline in Google Colab that predicts median house values across California districts using scikit-learn, pandas, and matplotlib.

By the end of this project, you'll have:

  • A Google Colab notebook containing your full analysis, visualizations, and trained model.
  • Hands-on experience with the end-to-end ML workflow: load data, explore, prepare, train, and evaluate.
  • A trained Linear Regression model with evaluation metrics and a predicted vs. actual values chart.
  • Secret Mission: Train a Random Forest model and compare its performance against Linear Regression to see which predicts better.

Are there any prerequisites?

None. All you need is a Google account to access Google Colab, which runs entirely in your browser with every library pre-installed. No Python installation or setup required on your computer.

Set Up Google Colab

To build a machine learning model, you need somewhere to write Python code, run it, and see results instantly.

Google Colab is a free, browser-based notebook that comes with all the data science libraries you need pre-installed. No downloads, no configuration. Just open it and start coding.

In this step, get ready to:
  • Create a new notebook in Google Colab.
  • Rename the notebook to match your project.
  • Verify that all required libraries are available.
Create a new notebook in Google Colab
  • Open colab.research.google.com in your browser.
  • Sign in with your Google account if you are not already signed in.
  • Click New notebook in the bottom right of the pop-up window.

What is Google Colab?

Google Colab is a hosted Jupyter Notebook service that requires no setup to use. It provides free access to computing resources and is especially well suited to machine learning, data science, and education.

Everything you write and run lives in your browser. Your notebook auto-saves to Google Drive, so you can come back to it anytime.

  • Click on the notebook title at the top left (it says Untitled0.ipynb by default).
  • Replace the title with California House Price Prediction and press Enter.

Don't see the New notebook button?

If the pop-up window did not appear, click File in the top menu bar, then select New notebook.

Still having trouble opening Colab?

Verify the pre-installed libraries

Google Colab comes with popular data science libraries already installed. You need to confirm they are available before building your model.

  • Verify that all required libraries are ready by pasting this code into the first cell and pressing the play button (or Shift+Enter):
# Cell 1: Import Libraries
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn.datasets import fetch_california_housing
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_squared_error, r2_score

What does this code do?

  • pandas loads and manipulates data in table format (DataFrames).
  • numpy handles numerical operations and arrays.
  • matplotlib creates charts and visualizations.
  • sklearn (scikit-learn) provides the dataset, model training tools, and evaluation metrics you will use throughout this project.
  • After the cell finishes running, confirm that no error messages appeared. A small green checkmark appears to the left of the cell when it completes successfully.

Seeing a ModuleNotFoundError?

This is rare in Google Colab since all these libraries come pre-installed. If you do see an import error, try clicking Runtime in the top menu, then Restart runtime. Run the cell again after the restart.

Help me fix my import error in Google Colab.

✔️ Awesome, I've got everything!

Great. Double check that you have saved your notebook by pressing Ctrl+S (or Cmd+S on macOS).

ⓧ I'd like to double check the full code

Your notebook should have one code cell with the following content:

# Cell 1: Import Libraries
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn.datasets import fetch_california_housing
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_squared_error, r2_score

Your notebook is ready and your tools are confirmed. Next up, you will load real California housing data and start exploring what is inside.

Load and Explore the Dataset

Your notebook is set up with all the libraries you need. Now it's time to bring in actual data and understand what you're working with.

The California Housing dataset contains real census data from 1990, recording information about 20,640 districts across the state. Before building any model, you need to explore this data to understand its structure, spot patterns, and check for problems like missing values.

In this step, get ready to:
  • Load the California Housing dataset from scikit-learn into a DataFrame.
  • Explore data types and summary statistics.
  • Check for missing values and identify what each column represents.
Load the California Housing dataset

The scikit-learn library includes several built-in datasets for practicing machine learning. The California Housing dataset is one of them, which means you can load it directly without downloading any files.

  • Click the + Code button in the toolbar at the top of your notebook to create a new code cell.
  • Load the dataset and preview it by pasting this code into the new cell:
# Cell 2: Load the Dataset
housing = fetch_california_housing(as_frame=True)
df = housing.frame
print(f"Dataset shape: {df.shape}")
print(f"\nFirst 5 rows:")
print(df.head())

What does this code do?

fetch_california_housing(as_frame=True) loads the dataset directly from scikit-learn and returns it as a DataFrame. The as_frame=True parameter means the data comes back as a pandas table instead of a raw array.

housing.frame combines the features and the target column into a single DataFrame called df. The .shape and .head() calls let you quickly confirm how many rows and columns exist and preview the first few entries.

  • Run the cell by pressing Shift+Enter.
  • You should see output showing Dataset shape: (20640, 9) followed by a table of the first 5 rows with columns like MedInc, HouseAge, and MedHouseVal.

Seeing a NameError?

If you see NameError: name 'fetch_california_housing' is not defined, your Cell 1 (import cell) has not been run yet. Go back to Cell 1 and press Shift+Enter to run it first, then run Cell 2 again.

help me fix a NameError in my Colab notebook when loading the California Housing dataset

Explore data types and statistics

You know the dataset has 20,640 rows and 9 columns. The next question is: what types of data do those columns hold, and what do the values look like?

  • Create a new code cell by clicking + Code.
  • Explore the data types and summary statistics by pasting this code:
# Cell 3: Explore the Data
print("\nDataset Info:")
print(df.info())
print("\nSummary Statistics:")
print(df.describe())

What does this code do?

.info() shows the data type of each column (all float64 here) and confirms how many non-null values exist. This helps you spot columns that might have missing data.

.describe() calculates summary statistics for every numeric column: count, mean, standard deviation, min, max, and quartiles. This gives you a quick sense of the range and distribution of each feature.

  • Run the cell by pressing Shift+Enter.
  • You should see a list of all 9 columns with their data types, followed by a statistics table showing values like mean, min, and max for each column.

Output looks cut off?

Colab sometimes truncates long outputs. Click the output area to expand it, or click the three dots (...) at the bottom of the output to see the full result.

help me see the full output of my Colab cell

Check for missing values and identify columns

Missing values can break a machine learning model or silently reduce its accuracy. Before going further, you need to confirm the dataset is complete.

  • Create a new code cell by clicking + Code.
  • Check for missing values by pasting this code:
# Cell 4: Check for Missing Values
print(f"\nMissing values per column:")
print(df.isnull().sum())

What does this code do?

df.isnull().sum() counts how many missing (null) values exist in each column. If every column shows 0, your data is complete and ready for modeling without any cleanup.

  • Run the cell by pressing Shift+Enter.
  • You should see all 9 columns listed with a count of 0 next to each one. This confirms there are no missing values.

What does each column mean?

Each row in the dataset represents a California district (a census block group). Here is what the columns record:

  • MedInc = median income in the block group.
  • HouseAge = median house age in the block group.
  • AveRooms = average number of rooms per household.
  • AveBedrms = average number of bedrooms per household.
  • Population = block group population.
  • AveOccup = average number of household members.
  • Latitude and Longitude = geographic coordinates of the district.
  • MedHouseVal = median house value in $100,000s (this is what you will predict).

✔️ Awesome, I've got everything!

Great. Double check that all three cells (2, 3, and 4) have run successfully and show output below each one.

ⓧ I'd like to double check the full code

Here is everything your notebook should contain so far, from Cell 1 through Cell 4:

# Cell 1: Import Libraries
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn.datasets import fetch_california_housing
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_squared_error, r2_score

# Cell 2: Load the Dataset
housing = fetch_california_housing(as_frame=True)
df = housing.frame
print(f"Dataset shape: {df.shape}")
print(f"\nFirst 5 rows:")
print(df.head())

# Cell 3: Explore the Data
print("\nDataset Info:")
print(df.info())
print("\nSummary Statistics:")
print(df.describe())

# Cell 4: Check for Missing Values
print(f"\nMissing values per column:")
print(df.isnull().sum())

You now know what the data looks like, what each column means, and that there are no missing values to worry about. Next up, you'll create visualizations to see geographic patterns and price distributions hidden in these numbers.

Visualize the Data

You now know the shape and statistics of your dataset. Numbers alone can hide important patterns though.

In this step, you will create visualizations using matplotlib to see how house prices are distributed, where expensive houses are located geographically, and which features correlate most strongly with price.

In this step, get ready to:
  • Create a histogram showing the distribution of California house prices.
  • Plot a geographic scatter chart mapping house values across California.
  • Compute correlations to find which features most strongly predict price.
Create a histogram of house prices

Before training a model, it helps to understand how the thing you are predicting is distributed. A histogram shows how frequently different price ranges appear in the data.

  • Create a new code cell in your Colab notebook by clicking the + Code button in the toolbar.
  • Paste the following code into the new cell:
# Cell 5: Visualize Target Distribution
plt.figure(figsize=(8, 5))
plt.hist(df['MedHouseVal'], bins=50, edgecolor='black')
plt.xlabel('Median House Value ($100,000s)')
plt.ylabel('Frequency')
plt.title('Distribution of California House Prices')
plt.show()

What does this code do?

This code creates a histogram with 50 bins, splitting the full range of house prices into 50 equal intervals and counting how many districts fall into each one.

The figsize=(8, 5) sets the chart dimensions in inches. The edgecolor='black' adds outlines to each bar so they are easier to distinguish.

  • Run the cell by pressing Shift+Enter.
  • You should see a histogram chart appear below the cell showing a distribution of house prices. Most values cluster below 3.0 ($300,000), with a noticeable spike at the 5.0 cap.

Don't see a chart?

  • Make sure you ran your Cell 1 (import libraries) first. The plt object comes from that cell.
  • Check that you also ran Cell 2 (load dataset). The df variable must exist before you can plot from it.
  • If you see a NameError, click Runtime in the top menu, then Run all to re-run every cell in order.

Why isn't my matplotlib chart showing in Colab?

Create a geographic scatter plot

The dataset includes latitude and longitude for each district. You can plot these as coordinates and color each point by house value to see geographic price patterns.

  • Create a new code cell by clicking + Code.
  • Paste the following code into the cell:
# Cell 6: Geographic Scatter Plot
plt.figure(figsize=(10, 7))
scatter = plt.scatter(
    df['Longitude'],
    df['Latitude'],
    c=df['MedHouseVal'],
    cmap='viridis',
    alpha=0.4,
    s=10
)
plt.colorbar(scatter, label='Median House Value ($100,000s)')
plt.xlabel('Longitude')
plt.ylabel('Latitude')
plt.title('California House Prices by Location')
plt.show()

What does this code do?

  • The c=df['MedHouseVal'] parameter colors each dot based on its house value. The cmap='viridis' colormap goes from dark purple (low values) to bright yellow (high values).
  • The alpha=0.4 makes dots semi-transparent so overlapping points do not hide each other.
  • The plt.colorbar() adds a legend bar on the side showing what each color means.
  • Run the cell by pressing Shift+Enter.
  • You should see a scatter plot that looks like the outline of California. Bright yellow dots (expensive houses) cluster along the coast, especially near San Francisco and Los Angeles.

Scatter plot looks wrong?

  • If you see a blank or all-one-color plot, double check that you typed c=df['MedHouseVal'] with a capital V in Val.
  • If the shape looks flipped or stretched, make sure Longitude is on the x-axis and Latitude is on the y-axis.

Help me fix my geographic scatter plot in matplotlib.

Analyze feature correlations

Correlation measures how strongly two variables move together. A correlation close to 1 means when one goes up, the other does too. This tells you which features are most useful for predicting house prices.

  • Create a new code cell by clicking + Code.
  • Compute and display the correlation of every feature with house price by pasting this code:
# Cell 7: Correlation Analysis
correlations = df.corr()['MedHouseVal'].sort_values(ascending=False)
print("Correlation with MedHouseVal:")
print(correlations)

What does this code do?

  • The df.corr() method computes the correlation between every pair of columns in the DataFrame.
  • Selecting ['MedHouseVal'] pulls out only the correlations against the target column you want to predict.
  • The .sort_values(ascending=False) orders the results from strongest positive correlation to strongest negative.
  • Run the cell by pressing Shift+Enter.
  • You should see a sorted list of correlations. MedInc (median income) has the strongest positive correlation with house value, meaning higher income districts tend to have more expensive houses.

Seeing a warning or error?

  • If you see a FutureWarning about numeric_only, this is safe to ignore. The correlation values still print correctly.
  • If you see a KeyError, check that you typed MedHouseVal with that exact capitalization.

Help me debug my correlation analysis in pandas.

✔️ Awesome, I've got everything!

Great. Double check you have three visualization outputs: a histogram, a geographic scatter plot, and a printed list of correlations.

ⓧ I'd like to double check the full code

Here is everything your notebook should contain so far:

# Cell 1: Import Libraries
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn.datasets import fetch_california_housing
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_squared_error, r2_score

# Cell 2: Load the Dataset
housing = fetch_california_housing(as_frame=True)
df = housing.frame
print(f"Dataset shape: {df.shape}")
print(f"\nFirst 5 rows:")
print(df.head())

# Cell 3: Explore the Data
print("\nDataset Info:")
print(df.info())
print("\nSummary Statistics:")
print(df.describe())

# Cell 4: Check for Missing Values
print(f"\nMissing values per column:")
print(df.isnull().sum())

# Cell 5: Visualize Target Distribution
plt.figure(figsize=(8, 5))
plt.hist(df['MedHouseVal'], bins=50, edgecolor='black')
plt.xlabel('Median House Value ($100,000s)')
plt.ylabel('Frequency')
plt.title('Distribution of California House Prices')
plt.show()

# Cell 6: Geographic Scatter Plot
plt.figure(figsize=(10, 7))
scatter = plt.scatter(
    df['Longitude'],
    df['Latitude'],
    c=df['MedHouseVal'],
    cmap='viridis',
    alpha=0.4,
    s=10
)
plt.colorbar(scatter, label='Median House Value ($100,000s)')
plt.xlabel('Longitude')
plt.ylabel('Latitude')
plt.title('California House Prices by Location')
plt.show()

# Cell 7: Correlation Analysis
correlations = df.corr()['MedHouseVal'].sort_values(ascending=False)
print("Correlation with MedHouseVal:")
print(correlations)

Your visualizations revealed key patterns: income is the strongest predictor of house value, and expensive homes cluster along the California coast. Next up, you will prepare this data for machine learning by splitting it into training and testing sets.

Prepare the Data for Machine Learning

You've explored the California Housing dataset and uncovered patterns through visualizations. Now it's time to prepare the data so a machine learning model can learn from it.

Machine learning models need clearly separated inputs and outputs. You also need to hold back some data for testing, so you can measure how well the model performs on data it has never seen before.

In this step, get ready to:
  • Separate features from the target variable.
  • Split data into training and testing sets.
  • Confirm the split by printing dataset shapes.
Separate features from the target variable

A machine learning model needs two things: the input data it uses to make predictions (called features), and the correct answer it's trying to predict (called the target). You need to split your DataFrame into these two pieces.

  • Create a new code cell in your notebook and add the following code to separate features from the target:
# Cell 8: Prepare Features and Target
X = df.drop('MedHouseVal', axis=1)
y = df['MedHouseVal']
print(f"Features shape: {X.shape}")
print(f"Target shape: {y.shape}")

What does this code do?

The variable X holds your features. It's the full DataFrame with the MedHouseVal column removed. These 8 remaining columns (income, location, house age, etc.) are the inputs your model will learn from.

The variable y holds the target. It's the single column of house prices your model will try to predict.

  • Run the cell by pressing **Shift+Enter**.
  • You should see Features shape: (20640, 8) and Target shape: (20640,) printed in the output.

Seeing a NameError?

If you see NameError: name 'df' is not defined, it means the earlier cells haven't been run yet. Go back to Cell 2 and run each cell in order from the top of your notebook.

Make sure the column name MedHouseVal is spelled exactly as shown, including capitalization.

help me fix a NameError in my Colab notebook

Split the data into training and testing sets

If you train and test a model on the same data, you have no way of knowing if it actually learned patterns or just memorized the answers. A train/test split solves this by reserving a portion of data the model never sees during training.

  • Create a new code cell and add the following code to split your data:
# Cell 9: Train/Test Split
X_train, X_test, y_train, y_test = train_test_split(
    X, y, test_size=0.2, random_state=42
)
print(f"Training set: {X_train.shape[0]} samples")
print(f"Test set: {X_test.shape[0]} samples")

What does this code do?

  • train_test_split randomly shuffles your data and divides it into two groups: one for training and one for testing.
  • test_size=0.2 means 20% of the data is reserved for testing (4,128 samples) and 80% is used for training (16,512 samples).
  • random_state=42 locks the random shuffle so you get the same split every time you run the code. This makes your results reproducible.
  • Run the cell by pressing **Shift+Enter**.
  • You should see Training set: 16512 samples and Test set: 4128 samples printed in the output.

Seeing unexpected sample counts?

If your training set shows a different number than 16512, double check that test_size=0.2 is set correctly. A value of 0.2 means 20% goes to testing.

If you see a NameError for X or y, make sure you ran Cell 8 first.

help me debug my train_test_split output

✔️ Awesome, I've got everything!

Great. Double check you've run both Cell 8 and Cell 9 successfully before moving on.

ⓧ I'd like to double check the full code

Here is the complete notebook code up to this point:

# Cell 1: Import Libraries
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn.datasets import fetch_california_housing
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_squared_error, r2_score

# Cell 2: Load the Dataset
housing = fetch_california_housing(as_frame=True)
df = housing.frame
print(f"Dataset shape: {df.shape}")
print(f"\nFirst 5 rows:")
print(df.head())

# Cell 3: Explore the Data
print("\nDataset Info:")
print(df.info())
print("\nSummary Statistics:")
print(df.describe())

# Cell 4: Check for Missing Values
print(f"\nMissing values per column:")
print(df.isnull().sum())

# Cell 5: Visualize Target Distribution
plt.figure(figsize=(8, 5))
plt.hist(df['MedHouseVal'], bins=50, edgecolor='black')
plt.xlabel('Median House Value ($100,000s)')
plt.ylabel('Frequency')
plt.title('Distribution of California House Prices')
plt.show()

# Cell 6: Geographic Scatter Plot
plt.figure(figsize=(10, 7))
scatter = plt.scatter(
    df['Longitude'],
    df['Latitude'],
    c=df['MedHouseVal'],
    cmap='viridis',
    alpha=0.4,
    s=10
)
plt.colorbar(scatter, label='Median House Value ($100,000s)')
plt.xlabel('Longitude')
plt.ylabel('Latitude')
plt.title('California House Prices by Location')
plt.show()

# Cell 7: Correlation Analysis
correlations = df.corr()['MedHouseVal'].sort_values(ascending=False)
print("Correlation with MedHouseVal:")
print(correlations)

# Cell 8: Prepare Features and Target
X = df.drop('MedHouseVal', axis=1)
y = df['MedHouseVal']
print(f"Features shape: {X.shape}")
print(f"Target shape: {y.shape}")

# Cell 9: Train/Test Split
X_train, X_test, y_train, y_test = train_test_split(
    X, y, test_size=0.2, random_state=42
)
print(f"Training set: {X_train.shape[0]} samples")
print(f"Test set: {X_test.shape[0]} samples")

Your data is prepped and split into training and testing sets. Next up, you'll train a Linear Regression model on the training data and see how accurately it predicts house prices.

Train and Evaluate the Model

Your data is split into training and testing sets. The training set teaches the model patterns, and the testing set checks whether those patterns generalize to data the model has never seen.

Now it's time for the exciting part: training a Linear Regression model to predict house prices, then measuring how accurately it performs. You'll use two metrics to judge the model and create a visualization that makes its accuracy instantly obvious.

In this step, get ready to:
  • Train a Linear Regression model on the training data.
  • Evaluate predictions using MSE and R-squared metrics.
  • Visualize predicted vs. actual house prices on a scatter plot.
Train the Linear Regression model

Linear Regression finds the best straight-line relationship between your features and the target variable. scikit-learn makes this a two-line process: create the model, then fit it to your training data.

  • Create a new code cell in your notebook and add the following code to train the model:
# Cell 10: Train Linear Regression Model
model = LinearRegression()
model.fit(X_train, y_train)
print("Model trained successfully!")

What does this code do?

`LinearRegression()` creates a blank model that hasn't learned anything yet.

`.fit(X_train, y_train)` is where the learning happens. The model analyzes all 16,512 training samples to find the relationship between the 8 features and house prices.

  • Run the cell by pressing the play button or Shift+Enter.
  • You should see Model trained successfully! printed below the cell.

Seeing a NameError?

If you see an error mentioning that `X_train` or `LinearRegression` is not defined, make sure you ran all previous cells in order. Click **Runtime** in the top menu, then **Run all** to re-run everything from the start.

Why is my model training failing?

Make predictions and evaluate accuracy

A trained model is only useful if it makes accurate predictions on data it hasn't seen before. You'll now use the test set to generate predictions and measure performance with two key metrics.

  • Create a new code cell and add this code to generate predictions and calculate evaluation metrics:
# Cell 11: Make Predictions and Evaluate
y_pred = model.predict(X_test)
mse = mean_squared_error(y_test, y_pred)
r2 = r2_score(y_test, y_pred)
print(f"Mean Squared Error (MSE): {mse:.4f}")
print(f"R-squared (R2) Score: {r2:.4f}")

What do these metrics mean?

`model.predict(X_test)` feeds the 4,128 test samples through the trained model to get predicted house values.

**Mean Squared Error (MSE)** measures how far off predictions are on average. Lower is better. It squares the errors, so big misses are penalized more heavily than small ones.

**R-squared (R2)** tells you what percentage of the variation in house prices your model explains. A score of 1.0 means perfect predictions. A score of 0.0 means the model is no better than just guessing the average price every time.

  • Run the cell.
  • You should see two lines printed: an MSE value and an R2 score around 0.60. This means your model explains roughly 60% of the variation in house prices.

R2 score looks very low or negative?

Make sure you passed the arguments in the correct order: `mean_squared_error(y_test, y_pred)` with actual values first. Also confirm that your Cell 9 used `random_state=42` in `train_test_split`.

Why is my R2 score unexpectedly low?

Visualize predicted vs. actual values

Numbers are helpful, but a chart makes model performance instantly intuitive. A scatter plot of predicted vs. actual values shows you exactly where the model gets it right and where it struggles.

  • Create a new code cell and add this code to plot predicted values against actual values:
# Cell 12: Predicted vs Actual Plot
plt.figure(figsize=(8, 6))
plt.scatter(y_test, y_pred, alpha=0.3, s=10)
plt.plot([0, 5], [0, 5], 'r--', label='Perfect Prediction')
plt.xlabel('Actual House Value ($100,000s)')
plt.ylabel('Predicted House Value ($100,000s)')
plt.title(f'Predicted vs Actual House Prices (R2 = {r2:.4f})')
plt.legend()
plt.show()

How do I read this chart?

Each dot represents one district from the test set. Its position on the x-axis is the actual house value, and its position on the y-axis is what the model predicted.

The red dashed line shows where perfect predictions would fall. Points close to this line are accurate predictions. Points far from the line are misses. You'll likely see the model struggles most with expensive houses (above $4-5 on the scale).

  • Run the cell.
  • You should see a scatter plot with a cloud of blue dots roughly following the red dashed diagonal line. The title includes your R2 score.

Plot not showing up?

Make sure you ran Cell 11 first so that `y_pred` and `r2` are defined. If you see a blank output, check that `plt.show()` is the last line in the cell.

Why isn't my matplotlib plot displaying in Colab?

✔️ Awesome, I've got everything!

Great. Double check you've run all cells in order from Cell 1 through Cell 12.

ⓧ I'd like to double check the full code

Here is the complete notebook code from Cell 1 through Cell 12. Compare each cell against yours to find any differences.

# Cell 1: Import Libraries
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn.datasets import fetch_california_housing
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_squared_error, r2_score

# Cell 2: Load the Dataset
housing = fetch_california_housing(as_frame=True)
df = housing.frame
print(f"Dataset shape: {df.shape}")
print(f"\nFirst 5 rows:")
print(df.head())

# Cell 3: Explore the Data
print("\nDataset Info:")
print(df.info())
print("\nSummary Statistics:")
print(df.describe())

# Cell 4: Check for Missing Values
print(f"\nMissing values per column:")
print(df.isnull().sum())

# Cell 5: Visualize Target Distribution
plt.figure(figsize=(8, 5))
plt.hist(df['MedHouseVal'], bins=50, edgecolor='black')
plt.xlabel('Median House Value ($100,000s)')
plt.ylabel('Frequency')
plt.title('Distribution of California House Prices')
plt.show()

# Cell 6: Geographic Scatter Plot
plt.figure(figsize=(10, 7))
scatter = plt.scatter(
    df['Longitude'],
    df['Latitude'],
    c=df['MedHouseVal'],
    cmap='viridis',
    alpha=0.4,
    s=10
)
plt.colorbar(scatter, label='Median House Value ($100,000s)')
plt.xlabel('Longitude')
plt.ylabel('Latitude')
plt.title('California House Prices by Location')
plt.show()

# Cell 7: Correlation Analysis
correlations = df.corr()['MedHouseVal'].sort_values(ascending=False)
print("Correlation with MedHouseVal:")
print(correlations)

# Cell 8: Prepare Features and Target
X = df.drop('MedHouseVal', axis=1)
y = df['MedHouseVal']
print(f"Features shape: {X.shape}")
print(f"Target shape: {y.shape}")

# Cell 9: Train/Test Split
X_train, X_test, y_train, y_test = train_test_split(
    X, y, test_size=0.2, random_state=42
)
print(f"Training set: {X_train.shape[0]} samples")
print(f"Test set: {X_test.shape[0]} samples")

# Cell 10: Train Linear Regression Model
model = LinearRegression()
model.fit(X_train, y_train)
print("Model trained successfully!")

# Cell 11: Make Predictions and Evaluate
y_pred = model.predict(X_test)
mse = mean_squared_error(y_test, y_pred)
r2 = r2_score(y_test, y_pred)
print(f"Mean Squared Error (MSE): {mse:.4f}")
print(f"R-squared (R2) Score: {r2:.4f}")

# Cell 12: Predicted vs Actual Plot
plt.figure(figsize=(8, 6))
plt.scatter(y_test, y_pred, alpha=0.3, s=10)
plt.plot([0, 5], [0, 5], 'r--', label='Perfect Prediction')
plt.xlabel('Actual House Value ($100,000s)')
plt.ylabel('Predicted House Value ($100,000s)')
plt.title(f'Predicted vs Actual House Prices (R2 = {r2:.4f})')
plt.legend()
plt.show()

Secret mission

Compare with Random Forest

Your Linear Regression model works, but can you do better? Try a more powerful algorithm and discover which features matter most for predicting house prices.

Clean Up Your Resources

Clean Up Your Resources

Decide whether to keep your resources running, pause them to come back later, or delete them entirely.

This project runs entirely in Google Colab, so there are no ongoing costs. Your notebook is stored in your Google Drive for free.

Resources you used:

  • Google Colab notebook ("California House Price Prediction") stored in your Google Drive.

Keep everything running

No action needed. Choose this if you plan to revisit your notebook, experiment with different models, or continue building on your analysis.

  • Your notebook stays in your Google Drive and is accessible anytime at colab.research.google.com.
  • The Colab runtime disconnects automatically after inactivity, so no compute resources stay running.

Pause - I'll come back to this later

Disconnect the runtime to free up any active compute session. Your notebook file remains saved in Google Drive.

  • In your Colab notebook, click **Runtime** in the top menu bar.
  • Click **Disconnect and delete runtime**.
  • Your code and outputs remain saved in the notebook file. Next time you open it, click the play button on any cell to reconnect.

Delete - I don't want to use this again

Remove the notebook from your Google Drive entirely.

  • Open Google Drive at drive.google.com.
  • Find the folder called **Colab Notebooks** in your Drive.
  • Right-click on the "California House Price Prediction" notebook file.
  • Click **Move to trash**.
  • Empty your trash to permanently delete the file. Click **Trash** in the left sidebar, then click **Empty trash** at the top right.

That's a Wrap!

That's a Wrap!

Nice work! You just built a complete machine learning pipeline that predicts California house prices from real census data.

You've learned how to:

  • Loaded and explored a real-world dataset with 20,640 samples using pandas, including summary statistics and missing value checks.
  • Created data visualizations including geographic scatter plots and distribution histograms to understand patterns in the data.
  • Trained a Linear Regression model and evaluated its performance using MSE and R-squared metrics.
  • Trained a Random Forest model and compared its performance against Linear Regression, discovering which features matter most for predicting house prices.

Ready to quiz yourself?