Predict House Prices with ML
Train two ML models to predict California house prices with Python.
Introduction
30 Second Summary
Every time you see a house listing, you probably wonder whether the price makes sense. Data scientists answer that question by feeding thousands of real records into algorithms that learn the patterns humans miss.
In this project, you will build a Python program that predicts California house prices using real data from 20,640 homes. You will train two different machine learning models, watch the first one struggle, then swap in a more powerful algorithm and see the accuracy jump.
What You'll Build
You run a single Python script and it prints predicted house prices next to actual prices, showing you exactly how close your trained model gets on houses it has never seen before.
By the end of this project, you'll have:
- A working data exploration pipeline that loads 20,640 real housing records, prints summary statistics, and saves a histogram revealing the price distribution.
- Two trained models that predict house prices, with R-squared scores measuring exactly how accurate each one is.
- A side-by-side comparison proving that a Random Forest model dramatically outperforms a simpler Linear Regression when data has complex patterns, plus printed feature importances showing which factors drive prices.
- Secret Mission: Engineer new features from raw data and push your model's accuracy even higher.
Are there any prerequisites?
You need basic Python knowledge (variables, functions, loops) and VS Code with Python 3.14 already installed. Step 1 handles everything else.
Before We Start
Before we dive in, let's lock in what you're about to build and why predicting house prices teaches you the core skills of a data engineer.
Set Up Your ML Environment
To predict house prices, you need a set of data science tools that can load data, build models, and visualize results.
In this step, you will install three Python libraries inside a virtual environment. pandas handles data loading, scikit-learn trains the machine learning models, and matplotlib draws charts.
In this step, get ready to:
- Create and activate a Python virtual environment.
- Install pandas, scikit-learn, and matplotlib.
- Verify the libraries import correctly.
Open VS Code and create a virtual environment
A virtual environment is an isolated space where Python installs libraries for one project only. It keeps your system Python clean and prevents library conflicts between projects.
- Press Cmd+Space (macOS) or the Windows key (Windows) to open your search bar.
- Type Visual Studio Code and press Enter to open it.
- Open the integrated terminal by pressing Ctrl+` (Windows/Linux) or Cmd+` (macOS).
What is the integrated terminal?
VS Code has a built-in terminal panel at the bottom of the window. It works just like your system terminal but stays inside VS Code so you can run commands without switching apps.
- Move to your Desktop by running this command:
cd ~/Desktop
- Create your virtual environment by running this command:
python -m venv ml-env
What does this command do?
python -m venv ml-env tells Python to create a new virtual environment in a folder called ml-env. This folder contains its own copy of Python and a place to install libraries.
- Activate the virtual environment by running the command for your operating system:
macOS
source ml-env/bin/activate
Windows
ml-env\Scripts\activate
You should see (ml-env) appear at the start of your terminal prompt. This confirms the virtual environment is active.
Don't see (ml-env) in your prompt?
On Windows, if you see an error about running scripts being disabled, run Set-ExecutionPolicy -Scope CurrentUser RemoteSigned in PowerShell, then try the activate command again.
On macOS, make sure you typed source before the path. Without it, the shell does not know to run the activation script.
Help me troubleshoot my virtual environment activation
Install the data science libraries
- Install pandas, scikit-learn, and matplotlib by running this command:
pip install pandas scikit-learn matplotlib
You will see download progress for each package. The install finishes when you see a line starting with Successfully installed followed by the package names and versions.
What are these libraries?
- pandas loads and manipulates tabular data (like a spreadsheet in code).
- scikit-learn provides machine learning algorithms and tools for training models, splitting data, and measuring accuracy.
- matplotlib creates charts and visualizations from your data.
Seeing a permission error or timeout?
Make sure your (ml-env) virtual environment is active. If you accidentally closed the terminal, reopen it and run the activate command again before retrying pip install.
If you see a network timeout, check your internet connection and retry the command.
Help me fix my pip install error
Verify the installation
Before you move on, you want to confirm that Python can actually find and load the libraries you just installed.
- Before you run the next command, predict what you expect to see. If the install worked, what should Python print?
- Verify your installation by running this command:
python -c "import pandas; import sklearn; import matplotlib; print('pandas', pandas.__version__); print('sklearn', sklearn.__version__)"
You should see two version numbers printed without any errors. The exact numbers depend on when you install, but you will see something like pandas 3.0.5 and sklearn 1.9.0.
Seeing ModuleNotFoundError?
This means Python cannot find the libraries. Check that your terminal still shows (ml-env) at the prompt. If it does not, reactivate the virtual environment and run pip install pandas scikit-learn matplotlib again.
Also make sure you are using python (not python3) because the virtual environment maps python to the correct version automatically.
Help me fix my ModuleNotFoundError
Your machine learning environment is ready. Next up, you will load real California housing data and explore it to find patterns hiding in 20,000+ records.
Load the Data and Explore It
Your ML environment is set up and ready to go. Now it's time to bring in real data. Before you can predict house prices, you need to see what the data actually looks like.
In this step, you will load 20,640 real California housing records using scikit-learn and explore them with pandas. This gives you your first visible output: summary statistics and a price distribution chart.
In this step, get ready to:
- Load a real housing dataset and print its shape, column names, and statistics.
- Visualize the house price distribution and save it as an image file.
Load the California housing dataset
scikit-learn ships with a built-in California housing dataset containing 20,640 records of real houses. You will load it into a DataFrame and print key statistics to understand what you are working with.
- In VS Code, create a new file called explore.py in your project folder.
- Add the following code to load the dataset and print exploration results:
import pandas as pd
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
from sklearn.datasets import fetch_california_housing
# Load the California housing dataset as a pandas DataFrame
housing = fetch_california_housing(as_frame=True)
df = housing.frame
# Explore the data
print("Dataset shape:", df.shape)
print("\nColumn names:")
print(df.columns.tolist())
print("\nFirst 5 rows:")
print(df.head())
print("\nDescriptive statistics:")
print(df.describe())
# Check for missing values
print("\nMissing values per column:")
print(df.isnull().sum())
What does this code do?
fetch_california_housing(as_frame=True) loads a real dataset of 20,640 California houses directly from scikit-learn. The as_frame=True parameter returns it as a pandas DataFrame so you can use familiar table operations.
housing.frame gives you the full DataFrame including all 8 features plus the target column (MedHouseVal). The print statements reveal the dataset's shape, columns, sample rows, statistics, and whether any values are missing.
matplotlib.use('Agg') sets a non-interactive backend so plots save to files without needing a display window. You will use this for the histogram in the next chunk.
Before you run this, what do you think the shape of the dataset will be? How many columns would a housing dataset need?
- Save explore.py and run it in your terminal:
python explore.py
You should see the dataset shape (20640, 9) printed first, followed by column names including MedInc and MedHouseVal. The descriptive statistics show the mean, min, and max for all 8 features and the target.
Seeing a ModuleNotFoundError?
Make sure your virtual environment is active. You should see (ml-env) at the start of your terminal prompt. If not, activate it again with ml-env\Scripts\activate and rerun the script.
If you see an error about matplotlib or sklearn, run pip install pandas scikit-learn matplotlib again inside the active virtual environment.
help me fix a ModuleNotFoundError when running explore.py
Visualize house price distribution
Numbers alone do not tell the full story. A histogram reveals how house prices are distributed across the dataset and exposes data quality issues you would otherwise miss.
- In explore.py, add the following code at the bottom of the file (below the missing values check):
# Visualize the target variable distribution
plt.figure(figsize=(10, 6))
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.savefig('price_distribution.png')
print("\nHistogram saved as price_distribution.png")
What does this code do?
plt.hist() creates a histogram with 50 bins showing how many houses fall into each price range. The x-axis is median house value in units of $100,000.
plt.savefig('price_distribution.png') writes the chart to a PNG file in your project folder. This lets you inspect the distribution visually without needing a live display window.
- Save explore.py and run it again:
python explore.py
- Confirm the histogram was saved by running:
ls price_distribution.png
You should see price_distribution.png listed in the output, and the terminal prints Histogram saved as price_distribution.png at the end of the script output.
Why are house values capped at 5.0?
Open price_distribution.png in VS Code or your file browser. Notice the tall spike at the right edge (value 5.0). This represents all houses worth $500,000 or more, lumped into a single cap.
This is a real data quality issue. The original census data capped the target at $500,000. Your model will struggle to predict prices above this ceiling because the true values are hidden. Data engineers encounter limits like this in real-world datasets all the time.
Don't see the histogram file?
Make sure matplotlib.use('Agg') appears before import matplotlib.pyplot as plt in your file. If it comes after, the backend is not set correctly and the file may not save.
Also check that you are running ls in the same folder where explore.py lives. The PNG is saved in the current working directory.
help me figure out why price_distribution.png is not being generated
✔️ Awesome, I've got everything!
Great. Make sure you have saved explore.py and that price_distribution.png exists in your project folder.
ⓧ I'd like to double check the full code
Your explore.py should contain both chunks you built above, in this order:
- Chunk 1: Imports, dataset loading, and all the print() statements for shape, columns, head, describe, and missing values.
- Chunk 2: The histogram visualization block starting with plt.figure() and ending with the print confirmation.
Scroll up to each chunk and confirm they match exactly. The file should have 31 lines total (including blank separator lines between sections).
You now have real data loaded and explored. You have seen that the dataset has 20,640 rows across 9 columns, no missing values, and a price cap at $500,000. Next up, you will feed this data into your first machine learning model and see how accurately it can predict house prices.
Train Your First Model (Linear Regression)
You have explored 20,640 housing records and know what the data looks like. Now it is time to feed that data into a machine learning model and get actual price predictions back.
Linear Regression is the simplest ML algorithm available. It draws a straight line through your data to make predictions. You will use it as a baseline to measure everything else against in later steps.
In this step, get ready to:
- Split the data into training and test sets.
- Train a Linear Regression model to predict house prices.
- Measure the model's accuracy using R-squared and Mean Squared Error.
Create train.py and split the data
A machine learning model needs two separate batches of data. One batch is for learning patterns (training data). The other is for testing whether those patterns hold up on data it has never seen (test data).
- Create a new file called train.py in the same folder as explore.py.
- Paste the following code to load the data and split it into training (80%) and test (20%) sets:
import pandas as pd
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
# Load data
housing = fetch_california_housing(as_frame=True)
df = housing.frame
# Separate features and target
X = df.drop('MedHouseVal', axis=1)
y = df['MedHouseVal']
# Split into training (80%) and test (20%) sets
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.2, random_state=42
)
print("Training set size:", X_train.shape[0])
print("Test set size:", X_test.shape[0])
print()
What does this code do?
- X = df.drop('MedHouseVal', axis=1) separates the 8 input features (income, location, house age, etc.) from the target column you want to predict.
- y = df['MedHouseVal'] isolates the target column (median house value) that the model will learn to predict.
- train_test_split randomly shuffles the data and holds back 20% for testing. The random_state=42 makes the shuffle reproducible so you get the same split every time.
- Save train.py.
- Run the file to confirm the split by running:
python train.py
You should see the training set has 16512 samples and the test set has 4128 samples.
Seeing a ModuleNotFoundError?
Make sure your virtual environment is active. You should see (ml-env) at the start of your terminal prompt. If not, activate it again with ml-env\Scripts\activate.
help me fix a ModuleNotFoundError in my train.py file
Train Linear Regression and evaluate its accuracy
Your data is split and ready. Now you will train the model on the training set and measure how well it predicts on data it has never seen.
- In train.py, add the following code at the bottom of the file:
# --- Linear Regression (no scaling) ---
lr_model = LinearRegression()
lr_model.fit(X_train, y_train)
lr_score = lr_model.score(X_test, y_test)
lr_predictions = lr_model.predict(X_test)
lr_mse = mean_squared_error(y_test, lr_predictions)
print("=== Linear Regression (no scaling) ===")
print(f"R-squared: {lr_score:.4f}")
print(f"Mean Squared Error: {lr_mse:.4f}")
print(f"Average prediction error: ${lr_mse**0.5 * 100000:.0f}")
What does this code do?
- lr_model.fit(X_train, y_train) trains the model by finding the best straight-line relationship between features and house prices.
- lr_model.score(X_test, y_test) returns the R-squared value. This measures how much of the price variation your model explains. A score of 1.0 means perfect predictions. A score of 0.0 means the model is no better than guessing the average.
- mean_squared_error gives the average squared difference between actual and predicted prices. Multiplying by 100,000 converts it to dollars (since the target is in units of $100k).
- Save train.py.
Before you run this, what R-squared score do you think your model will get? Remember, 1.0 is perfect and 0.0 means no predictive power at all.
- Run the script to see your model's performance:
python train.py
You should see an R-squared of about 0.60. This means your model explains roughly 60% of price variation. Not bad for a first attempt, but clearly not great.
What does an R-squared of 0.60 mean?
Your model captures the broad trend (higher income areas have higher prices) but misses 40% of the variation. House prices depend on complex interactions between features that a straight line cannot capture.
The average prediction error in dollars shows you how far off a typical prediction is from the real price. You will improve both numbers in the next steps.
Seeing a NameError or unexpected output?
Check that your new code block is below the print() line from the first chunk. If you accidentally pasted it inside the split section, the variables may not be defined yet.
help me fix a NameError in my train.py Linear Regression code
✔️ Awesome, I've got everything!
Great. Make sure you have saved train.py before continuing.
ⓧ I'd like to double check the full code
Your train.py should have these two sections in order. Scroll up and confirm each one is present:
- The imports, data loading, feature/target separation, train/test split, and the three print statements showing set sizes.
- The Linear Regression training block that creates lr_model, fits it, calculates lr_score and lr_mse, and prints all three evaluation lines.
You have a working model that predicts house prices. An R-squared of 0.60 is decent for a first pass, but 40% of the variation is still unexplained. Next up, you will try to improve this score by scaling the features to a common range.
Scale Features and Improve
Your first model explains about 60% of house price variation. That leaves a lot of room for improvement.
One common issue in machine learning is that features live on wildly different scales. In your dataset, median income ranges from 0 to 15, while population ranges from 0 to 35,000. Feature scaling normalizes all features to the same range, which can help algorithms learn patterns more effectively.
In this step, get ready to:
- Scale all features to zero mean and unit variance using StandardScaler.
- Retrain Linear Regression on the scaled data and observe the result.
Add the StandardScaler import
StandardScaler transforms each feature so it has a mean of zero and a standard deviation of one. This puts all features on the same playing field.
- In train.py, find this import line near the top of the file:
from sklearn.linear_model import LinearRegression
- Add this new import line directly below it:
from sklearn.preprocessing import StandardScaler
Scale features and retrain Linear Regression
- Add the following code at the very bottom of train.py, after the last print statement:
print()
# --- Linear Regression (with scaling) ---
scaler = StandardScaler()
X_train_scaled = scaler.fit_transform(X_train)
X_test_scaled = scaler.transform(X_test)
lr_scaled_model = LinearRegression()
lr_scaled_model.fit(X_train_scaled, y_train)
lr_scaled_score = lr_scaled_model.score(X_test_scaled, y_test)
print("=== Linear Regression (with scaling) ===")
print(f"R-squared: {lr_scaled_score:.4f}")
print(f"Scaling barely helped. Linear Regression cannot capture non-linear patterns.")
What does this code do?
- scaler.fit_transform(X_train) calculates the mean and standard deviation from the training data, then scales it. This is called "fitting" the scaler.
- scaler.transform(X_test) applies the same transformation to the test data using the training data's statistics. You never fit on test data, because that would leak information about the test set.
- lr_scaled_model is a fresh Linear Regression trained on the scaled features, so you can compare its score to the original.
- Save train.py.
Before you run this, think about what you expect. The features are now on the same scale. Do you think the R-squared score will improve significantly?
- Run your updated training script:
python train.py
Look at the scaled Linear Regression R-squared score. It is still around 0.60. Scaling barely changed anything.
Why didn't scaling help?
The problem is not the feature scales. The problem is the model itself.
Linear Regression can only learn straight-line relationships between features and the target. House prices depend on complex, non-linear interactions. Income matters more in expensive neighborhoods. Location effects compound with house age. No amount of scaling can teach a straight line to bend.
This is the ceiling for Linear Regression on this dataset. To break past it, you need an algorithm that can model non-linear patterns.
Seeing an import error for StandardScaler?
- Check that the import reads exactly from sklearn.preprocessing import StandardScaler. A typo in preprocessing or StandardScaler will cause an error.
- Make sure your virtual environment ml-env is still active. You should see (ml-env) at the start of your terminal prompt.
Why is my StandardScaler import failing in train.py?
✔️ Awesome, I've got everything!
Double check you have saved train.py and your output shows both the unscaled and scaled R-squared scores.
ⓧ I'd like to double check the full code
Here is the complete train.py at this point:
import pandas as pd
from sklearn.datasets import fetch_california_housing
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
from sklearn.preprocessing import StandardScaler
from sklearn.metrics import mean_squared_error
# Load data
housing = fetch_california_housing(as_frame=True)
df = housing.frame
# Separate features and target
X = df.drop('MedHouseVal', axis=1)
y = df['MedHouseVal']
# Split into training (80%) and test (20%) sets
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.2, random_state=42
)
print("Training set size:", X_train.shape[0])
print("Test set size:", X_test.shape[0])
print()
# --- Linear Regression (no scaling) ---
lr_model = LinearRegression()
lr_model.fit(X_train, y_train)
lr_score = lr_model.score(X_test, y_test)
lr_predictions = lr_model.predict(X_test)
lr_mse = mean_squared_error(y_test, lr_predictions)
print("=== Linear Regression (no scaling) ===")
print(f"R-squared: {lr_score:.4f}")
print(f"Mean Squared Error: {lr_mse:.4f}")
print(f"Average prediction error: ${lr_mse**0.5 * 100000:.0f}")
print()
# --- Linear Regression (with scaling) ---
scaler = StandardScaler()
X_train_scaled = scaler.fit_transform(X_train)
X_test_scaled = scaler.transform(X_test)
lr_scaled_model = LinearRegression()
lr_scaled_model.fit(X_train_scaled, y_train)
lr_scaled_score = lr_scaled_model.score(X_test_scaled, y_test)
print("=== Linear Regression (with scaling) ===")
print(f"R-squared: {lr_scaled_score:.4f}")
print(f"Scaling barely helped. Linear Regression cannot capture non-linear patterns.")
You have proven that Linear Regression has hit its ceiling on this data. No amount of preprocessing can teach it to capture non-linear patterns. In the next step, you will swap in a completely different algorithm that can.
Beat Your First Model with Random Forest
In the last step, you proved that feature scaling alone cannot fix Linear Regression. Your R-squared stayed stuck at ~0.60 no matter what you did to the input data. The algorithm itself is the bottleneck.
Random Forest takes a completely different approach. Instead of fitting one straight line through the data, it builds hundreds of decision trees that each capture different patterns. In this step, you will swap in Random Forest and watch your accuracy jump.
In this step, get ready to:
- Train a Random Forest model and compare its score to Linear Regression.
- Identify which features matter most for predicting house prices.
- Demo a single prediction comparing actual vs predicted price.
Import and train a Random Forest model
Random Forest does not need scaled features because it is tree-based. You will train it on the original unscaled data and compare its score directly against Linear Regression.
- In train.py, find the imports at the top. Locate this line:
from sklearn.preprocessing import StandardScaler
- Add this new import directly below it:
from sklearn.ensemble import RandomForestRegressor
- Now scroll to the very end of train.py. Add the Random Forest training code below the scaled Linear Regression section by copying this code at the bottom of the file:
print()
# --- Random Forest ---
rf_model = RandomForestRegressor(n_estimators=100, random_state=42)
rf_model.fit(X_train, y_train)
rf_score = rf_model.score(X_test, y_test)
rf_predictions = rf_model.predict(X_test)
rf_mse = mean_squared_error(y_test, rf_predictions)
print("=== Random Forest ===")
print(f"R-squared: {rf_score:.4f}")
print(f"Mean Squared Error: {rf_mse:.4f}")
print(f"Average prediction error: ${rf_mse**0.5 * 100000:.0f}")
print()
What does this code do?
- RandomForestRegressor(n_estimators=100, random_state=42) creates a model that builds 100 separate decision trees, each learning slightly different patterns from the data.
- The model trains on the original unscaled X_train because tree-based models handle different feature scales natively.
- .score() returns the R-squared value on the test data, so you can compare directly to Linear Regression.
- Save train.py.
- Run the script to see your new model's score by running:
python train.py
You should see the Random Forest R-squared printed around 0.80 or higher. That is a massive jump from Linear Regression's ~0.60.
Why is Random Forest so much better here?
Linear Regression can only draw a single straight line through the data. House prices depend on complex interactions between features (e.g. income AND location together matter more than either alone).
Random Forest captures these non-linear patterns by building many decision trees that split the data at different thresholds. Each tree votes on a prediction, and the average of all votes is the final answer.
Seeing an import error?
- Make sure you added the from sklearn.ensemble import RandomForestRegressor line in the import section at the top of the file, not at the bottom.
- Check that your virtual environment is still active. You should see (ml-env) at the start of your terminal prompt.
Help me debug my Random Forest import error in train.py.
See which features matter most
One of Random Forest's biggest advantages is that it tells you exactly which features drove its predictions. This is called feature importance.
- Add the feature importances code at the end of train.py by copying this below the Random Forest section:
# Feature importances
feature_importances = pd.Series(
rf_model.feature_importances_,
index=X.columns
).sort_values(ascending=False)
print("=== Top Feature Importances ===")
for feature, importance in feature_importances.head(5).items():
print(f" {feature}: {importance:.4f}")
print()
What does this code do?
- rf_model.feature_importances_ returns an array showing how much each feature contributed to the model's predictions.
- Wrapping it in a pd.Series with X.columns as the index pairs each importance score with its feature name.
- .sort_values(ascending=False).head(5) shows the top 5 most influential features, ranked from most to least important.
- Now add the prediction demo that shows how the model performs on a single house. Copy this code at the very end of the file:
# Prediction demo
print("=== Prediction Demo ===")
sample_idx = 0
sample = X_test.iloc[[sample_idx]]
actual_price = y_test.iloc[sample_idx] * 100000
predicted_price = rf_model.predict(sample)[0] * 100000
print(f"Actual price: ${actual_price:,.0f}")
print(f"Predicted price: ${predicted_price:,.0f}")
print(f"Difference: ${abs(actual_price - predicted_price):,.0f}")
What does this code do?
- X_test.iloc[[sample_idx]] picks one house from the test set (the first sample).
- Multiplying by 100,000 converts the target values back to real dollar amounts (the dataset stores prices in units of $100,000).
- The difference between actual and predicted price shows you exactly how far off the model is on a single prediction.
- Save train.py.
Before you run the script, think about this: which feature do you think matters most for predicting house prices? Income, location, house size, or something else?
- Run the complete script to see all models side by side, the feature importances, and the prediction demo:
python train.py
You should see both model scores printed (Linear Regression ~0.60, Random Forest ~0.80+), the top 5 most important features ranked by score, and a prediction demo showing the actual vs predicted price for one house.
What should I see in the feature importances?
MedInc (median income) will likely rank as the strongest predictor by a wide margin. This makes intuitive sense. Wealthier neighborhoods have more expensive houses.
Location features (Latitude, Longitude) will also rank high, reflecting that California coastal areas command higher prices than inland regions.
Random Forest score seems too low?
- Make sure you are training on X_train (unscaled), not X_train_scaled. Random Forest handles different feature scales on its own.
- Check that n_estimators=100 and random_state=42 are set. A different random state will give different results.
Help me debug my Random Forest model score.
✔️ Awesome, I've got everything!
Great. Double check you have saved train.py with all the changes.
ⓧ I'd like to double check the full code
Here is the complete train.py file. Confirm each section is present in order:
- Imports (including RandomForestRegressor).
- Data loading and train/test split.
- Linear Regression (no scaling) section.
- Linear Regression (with scaling) section.
- Random Forest section.
- Feature importances section.
- Prediction demo section.
Scroll up through the chunks above and confirm each one is in your file, in that order.
You just went from explaining 60% of price variation to over 80% by switching algorithms. You also know exactly which features drive house prices. Next up, the Secret Mission challenges you to push that accuracy even higher with feature engineering.
Secret mission
Engineer New Features to Push Accuracy Higher
Your Random Forest already scores around 0.80. But data engineers don't just pass raw data into models. They create new features that capture domain knowledge. Can you engineer new columns from the existing data and push your model's accuracy even higher?
Clean Up Your Resources
Clean Up Your Resources
This project runs entirely locally, so there are no ongoing costs. Decide whether to keep your resources running, pause them to come back later, or delete them entirely.
Resources you used:
- Virtual environment folder ml-env (contains installed packages: pandas, scikit-learn, matplotlib).
- Python scripts: explore.py and train.py.
- Generated image: price_distribution.png.
Keep everything running
No action needed. Choose this if you are still actively building or want to keep experimenting with your models.
- Your virtual environment stays on disk and you can reactivate it any time.
- Your scripts and plot are ready to run whenever you return.
Pause - I'll come back to this later
Deactivate your virtual environment to free up your terminal session. Your files stay on disk for next time.
- Deactivate the virtual environment by running:
deactivate
- When you come back, reactivate it with ml-env\Scripts\activate (Windows) or source ml-env/bin/activate (macOS/Linux).
Delete - I don't want to use this again
Remove all project files and the virtual environment to start fresh.
- First, deactivate the virtual environment if it is still active:
deactivate
- On macOS/Linux, remove everything by running:
rm -rf ml-env explore.py train.py price_distribution.png
- On Windows, remove everything by running:
rmdir /s /q ml-env
del explore.py train.py price_distribution.png
- Confirm the files are gone by running ls (macOS/Linux) or dir (Windows). You should no longer see ml-env, explore.py, train.py, or price_distribution.png in the output.
Nice Work!
Nice Work!
Nice work! You just built a complete machine learning pipeline that predicts California house prices from real data. You went from raw numbers to trained models, and proved you can measure and improve prediction accuracy.
You've learned how to:
- Load and explore a real dataset with pandas, revealing patterns across 20,640 records using descriptive statistics and histogram visualizations.
- Train a Linear Regression model, measure its accuracy with R-squared and Mean Squared Error, and discover that feature scaling alone cannot fix a model too simple for the data.
- Swap in a Random Forest model that dramatically outperformed Linear Regression, and identify the most important features driving house price predictions.
- Secret Mission: Engineered new features from domain knowledge and pushed your model's accuracy even higher.
Ready to quiz yourself?