Penguin Classification Pipeline

Penguin Classification Pipeline

Penguin Species Classification Pipeline

A step-by-step guide to building an end-to-end machine learning model with Scikit-learn.

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 to clean and structure the data.
  • seaborn: A data visualization library that, in this case, is used as a convenient way to load the example "penguins" dataset.
  • scikit-learn (sklearn): The core machine learning library. It provides all the necessary tools for splitting data, preprocessing, building the model, creating the pipeline, and evaluating performance.

1. Load the Dataset

  • What the code does: It calls sns.load_dataset() to fetch the "penguins" data and stores it in a pandas DataFrame named df.
  • Behind the scenes: The seaborn library downloads the dataset from its repository once and caches it locally, so subsequent calls are faster.
  • 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("penguins")
except Exception as e:
    print(f"Error loading dataset: {e}")
    exit()

2. Clean the Data

  • What the code does: The df.dropna() method is used on the pandas DataFrame to remove rows.
  • Behind the scenes: This method performs a row-wise filter, dropping any row that contains at least one missing value.
  • Abstraction: This simple method call abstracts the iterative process of checking each cell in every row for a missing value (e.g., `NaN`) and removing the entire row.
df = df.dropna()

3. Split Target and Features

  • What the code does: It assigns the "species" column to the target variable `y` and the rest of the DataFrame to the feature variable `X`.
  • Behind the scenes: The `y` variable will contain three distinct classes (species), while `X` will contain a mix of numeric (e.g., bill length) and categorical (e.g., island, sex) columns. This is a manual data preparation step.
  • Abstraction: The pandas methods df[...] and df.drop(...) abstract the low-level data selection and column removal.
y = df["species"]
X = df.drop(columns=["species"])

4. 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: The `stratify=y` parameter ensures the class distribution (ratio of each species) is the same in both the training and testing sets. The `random_state=42` ensures the split is reproducible.
  • Abstraction: This function abstracts the manual, randomized, and stratified splitting of data, which is a critical step in machine learning to prevent overfitting and ensure reliable 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, stratify=y, random_state=42
)

5. & 6. Preprocessing with ColumnTransformer

  • What the code does: It identifies numeric and categorical columns using pandas's `select_dtypes`. Then, it uses a sklearn `ColumnTransformer` to apply a `StandardScaler` to numeric columns and a `OneHotEncoder` to categorical ones.
  • Behind the scenes: The `StandardScaler` will learn the mean and standard deviation of each numeric feature from the training data. The `OneHotEncoder` will learn a list of unique categories for each categorical column. The `handle_unknown="ignore"` parameter is a safeguard against new categories appearing in the test set.
  • 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"
)

7. & 8. Build the Pipeline

  • What the code does: It instantiates a RandomForestClassifier and then combines it with the `preprocessor` into a `Pipeline` object from sklearn.
  • Behind the scenes: A RandomForestClassifier trains many decision trees on bootstrapped samples of the data. Each tree is grown by finding the best feature split to reduce a metric like Gini impurity. During prediction, the trees "vote" on the final class. The `Pipeline` ensures that the preprocessing steps are applied automatically and consistently before the model is trained or makes predictions.
  • Abstraction: The `Pipeline` class abstracts the complex and error-prone process of managing data flow between preprocessing steps and a final estimator.
from sklearn.pipeline import Pipeline
from sklearn.ensemble import RandomForestClassifier

clf = RandomForestClassifier(n_estimators=300, random_state=42, n_jobs=-1)

pipe = Pipeline([
    ("preprocessor", preprocessor),
    ("classifier", clf)
])

9. & 10. 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` (learning the scale and categories), then transforms `X_train` and fits the RandomForestClassifier on the transformed data. When you call `predict()`, the pipeline automatically applies the *same, frozen* transformations to `X_test` before feeding it to the trained classifier for a prediction.
  • How the classifier is fitted: The `Pipeline` automatically takes the output of the `preprocessor` (a high-dimensional, mostly sparse matrix) and passes it to the `fit` method of the RandomForestClassifier, which then grows its ensemble of 300 decision trees based on this transformed data.
  • How the trees are grown: For each of the 300 trees, the algorithm first creates a bootstrapped sample (a random subset with replacement) of the training data. The process of **building the tree** is a recursive method of creating nodes and branches. At each node, it seeks the **optimal split point**, which is the feature and threshold that best divides the data into "purer" subsets. "Purer" means the subsets contain more instances of a single class. This is measured by a metric like Gini impurity. This recursive splitting continues until a **stopping criterion** is met, which prevents the tree from becoming too complex and overfitting the data. Examples of these criteria include:
    • Reaching a maximum depth (e.g., 5 levels deep).
    • A node having fewer than a minimum number of samples.
    • No further improvement in a node's purity can be made.
  • Abstraction: The `Pipeline` manages all these steps internally, preventing the data leakage that would occur if you fit a scaler on the test data, and ensuring a smooth workflow.
pipe.fit(X_train, y_train)
y_pred = pipe.predict(X_test)

12. Evaluate the Model

  • What the code does: The accuracy_score() and f1_score() functions from sklearn.metrics are used to calculate the model's performance on the test set.
  • Behind the scenes: The macro F1 score calculates the F1 score for each class individually and then takes the unweighted average, which is useful for datasets with class imbalance as it treats all classes equally.
  • 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 accuracy_score, f1_score

accuracy = accuracy_score(y_test, y_pred)
f1_macro = f1_score(y_test, y_pred, average="macro")

print(f"Accuracy: {accuracy:.4f}")
print(f"Macro F1 Score: {f1_macro:.4f}")

Comments

Popular posts from this blog

Elevator Q-Learning Pipeline

MountainCar Q-Learning Pipeline

SMS Spam Detection Pipeline