Machine LearningLesson 20 of 22

Project: House Price Predictor

This is your capstone for tabular data. You will go from raw CSV to a deployed API in one project, using everything from earlier lessons: cleaning, splitting, pipeline, cross-validation, and random forest.

This is your capstone for tabular data. You will go from raw CSV to a deployed API in one project, using everything from earlier lessons: cleaning, splitting, pipeline, cross-validation, and random forest.

You will do exploratory data analysis first: plot price vs sqft, check missing values, and see correlations. Then build a pipeline that imputes, scales, and trains. Finally you will save and serve it.

The goal is not perfect accuracy but a working, explainable system you can demo to employers.

EDA to pipeline

Start with pandas describe and a scatter plot. Build a pipeline so preprocessing does not leak.

PYTHON
import pandas as pd
import matplotlib.pyplot as plt
df = pd.read_csv("housing.csv")
df.plot.scatter(x="sqft", y="price")
plt.show()

Train and save

Train a forest, check cross-validation, save the pipeline.

PYTHON
from sklearn.ensemble import RandomForestRegressor
from sklearn.pipeline import Pipeline
pipe = Pipeline([("model", RandomForestRegressor(n_estimators=200, random_state=42))])
pipe.fit(X_train, y_train)
import joblib; joblib.dump(pipe, "house.joblib")

Syntax

PYTHON
from sklearn.ensemble import RandomForestRegressor
model = RandomForestRegressor(n_estimators=200)
model.fit(X_train, y_train)
print(model.score(X_test, y_test))

Data leakage in EDA

Looking at test data during EDA and then using those insights to choose features leaks test information. Do EDA only on train. This Kaggle post explains why leaderboard drops happen.

PYTHON
# Do EDA on train only: df_train = df.iloc[train_idx]

Good to know

  • EDA first, then pipeline.
  • Use cross-validation, not single split.
  • Save the whole pipeline.

Keep building your ML models

Copy the code, run it locally with pip install scikit-learn pandas, and continue to the next lesson.

→ Next: Project: Image Classifier