Diamond Price Regression Pipeline

Diamond Price Regression Pipeline

Diamond Price Regression Pipeline

A step-by-step guide to building a machine learning model to predict a diamond's price using a RandomForestRegressor.

Libraries Used

This pipeline uses a few key Python libraries. Here is a quick overview of their roles:

  • pandas: Used for data manipulation and analysis, primarily for working with DataFrames.
  • seaborn: A data visualization library used here to conveniently load the "diamonds" dataset.
  • scikit-learn (sklearn): The core machine learning library. It provides all the necessary tools for splitting data, preprocessing, building the model, and evaluating performance.

1. Load the Dataset

  • What the code does: It calls sns.load_dataset() to fetch the "diamonds" data and stores it in a pandas DataFrame named df.
  • Behind the scenes: This dataset is a regression problem. The goal is to predict the continuous `price` variable based on features like `carat`, `cut`, `color`, and `clarity`.
  • Abstraction: The function call abstracts the process of locating, downloading, and reading a dataset into a single, simple line of code.
import seaborn as sns
import pandas as pd

try:
    df = sns.load_dataset("diamonds")
except Exception as e:
    print(f"Error loading dataset: {e}")
    exit()

2. Split Target and Features

  • What the code does: It assigns the "price" column to the target variable `y` and the rest of the DataFrame to the feature variable `X`.
  • Behind the scenes: The `y` variable is the continuous numerical value we want to predict. The `X` variable contains a mix of numerical (e.g., `carat`) and categorical (e.g., `cut`) columns.
  • Abstraction: The pandas methods `df[...]` and `df.drop(...)` abstract the low-level data selection and column removal.
y = df["price"]
X = df.drop(columns=["price"])

3. Split into Training and Testing Sets

  • What the code does: The train_test_split() function from sklearn divides the `X` and `y` data into four parts: `X_train`, `X_test`, `y_train`, and `y_test`.
  • Behind the scenes: Since this is a regression problem, we don't use `stratify`. The split is randomized to ensure the training and testing sets contain a representative sample of the data. The `random_state=42` ensures a reproducible split.
  • Abstraction: This function abstracts the manual, randomized splitting of data, a critical step for reliable model evaluation.
from sklearn.model_selection import train_test_split

X_train, X_test, y_train, y_test = train_test_split(
    X, y, test_size=0.2, random_state=42
)

4. Preprocessing with ColumnTransformer

  • What the code does: It identifies numeric and categorical columns and uses a sklearn `ColumnTransformer` to apply a `StandardScaler` to numeric columns and a `OneHotEncoder` to categorical ones.
  • Behind the scenes: The `StandardScaler` standardizes the numerical features. The `OneHotEncoder` converts categorical features like `cut` and `color` into a numerical format that the regression model can understand.
  • Abstraction: The `ColumnTransformer` is a powerful tool that abstracts the process of applying different preprocessing steps to different subsets of columns in a single, unified step.
from sklearn.compose import ColumnTransformer
from sklearn.preprocessing import StandardScaler, OneHotEncoder

num_cols = X_train.select_dtypes(include=["number"]).columns.tolist()
cat_cols = X_train.select_dtypes(include=["object", "category"]).columns.tolist()

preprocessor = ColumnTransformer(
    transformers=[
        ("num", StandardScaler(with_mean=False), num_cols),
        ("cat", OneHotEncoder(handle_unknown="ignore"), cat_cols),
    ],
    remainder="passthrough"
)

5. Build the Pipeline

  • What the code does: It instantiates a RandomForestRegressor and combines it with the `preprocessor` into a `Pipeline` object from sklearn.
  • Behind the scenes: A RandomForestRegressor works similarly to a classifier, but instead of taking a majority vote, it averages the predictions of all the trees in its ensemble to produce a single continuous value.
  • Abstraction: The `Pipeline` class abstracts the complex and error-prone process of managing data flow between the preprocessing and the final estimator.
from sklearn.pipeline import Pipeline
from sklearn.ensemble import RandomForestRegressor

rf_regressor = RandomForestRegressor(n_estimators=300, random_state=42, n_jobs=-1)

pipe = Pipeline([
    ("preprocessor", preprocessor),
    ("regressor", rf_regressor)
])

6. & 7. Train and Predict

  • What the code does: The `pipe.fit()` method trains the entire pipeline on the training data. The `pipe.predict()` method then generates predictions on the test data.
  • Behind the scenes: When you call `fit()`, the pipeline first fits the `preprocessor` on `X_train` and then fits the RandomForestRegressor. When you call `predict()`, the pipeline automatically applies the same transformations to `X_test` before the trained regressor makes a prediction.
  • Abstraction: The `Pipeline` handles all these steps internally, ensuring data leakage is prevented and the workflow is smooth and consistent.
pipe.fit(X_train, y_train)
y_pred = pipe.predict(X_test)

8. Evaluate the Model

  • What the code does: The mean_absolute_error() and r2_score() functions from sklearn.metrics are used to calculate the model's performance on the test set.
  • Behind the scenes: The `R²` score (coefficient of determination) represents the proportion of the variance in the dependent variable that is predictable from the independent variables. An R² of 1 means the model perfectly predicts the target variable.
  • Abstraction: These functions abstract the complex calculations required to compute these statistical metrics, providing a single score that summarizes model performance.
from sklearn.metrics import mean_absolute_error, r2_score

mae = mean_absolute_error(y_test, y_pred)
r2 = r2_score(y_test, y_pred)

print(f"Mean Absolute Error: {mae:.4f}")
print(f"R-squared Score: {r2:.4f}")

Comments

Popular posts from this blog

Elevator Q-Learning Pipeline

MountainCar Q-Learning Pipeline

SMS Spam Detection Pipeline