Wine classification Pipeline

Wine Classification Pipeline

Wine Classification Pipeline

A step-by-step guide to building a machine learning model to classify wine varieties using a K-Nearest Neighbors (K-NN) classifier.

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.
  • scikit-learn (sklearn): The core machine learning library. It provides all the necessary tools for loading the dataset, splitting data, preprocessing, building the model, and evaluating performance.

1. Load the Dataset

  • What the code does: It calls sk_datasets.load_wine() to fetch a dataset containing chemical analysis of wines. The `as_frame=True` argument ensures the data is returned as a pandas DataFrame.
  • Behind the scenes: This dataset is a multi-class classification problem. The goal is to predict one of three wine varieties based on 13 features like `alcohol` and `malic_acid`.
  • Abstraction: The function call abstracts the process of locating, downloading, and loading the pre-packaged dataset into a structured format.
import pandas as pd
from sklearn import datasets as sk_datasets

try:
    data = sk_datasets.load_wine(as_frame=True)
    df = data.frame
except Exception as e:
    print(f"Error loading dataset: {e}")
    exit()

2. Split Target and Features

  • What the code does: It assigns the `target` column to the variable `y` (the labels to predict) and the remaining feature columns to `X`.
  • Behind the scenes: The `target` column contains integer values (0, 1, 2) representing the different wine varieties. The `X` variable contains 13 numerical features.
  • Abstraction: The pandas methods `df[...]` and `df.drop(...)` abstract the low-level data selection and column removal.
y = df["target"]
X = df.drop(columns=["target"])

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: The `stratify=y` parameter is crucial here, as it ensures the percentage of each wine variety is the same in both the training and testing sets, preventing biased evaluation. The `random_state=42` ensures a reproducible split.
  • Abstraction: This function abstracts the manual, randomized, and stratified 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, stratify=y, random_state=42
)

4. Preprocessing with StandardScaler

  • What the code does: It instantiates a `StandardScaler`. This preprocessing step is vital for distance-based algorithms like K-NN, which are sensitive to the scale of the features. The wine dataset contains features with different units and scales.
  • Behind the scenes: The `StandardScaler` standardizes features by removing the mean and scaling to unit variance. This ensures all features contribute equally to the distance calculation used by the K-NN algorithm.
  • Abstraction: The `StandardScaler` abstracts the process of calculating the mean and standard deviation for each feature and applying the transformation formula `(x - mean) / std_dev` to every value.
from sklearn.preprocessing import StandardScaler

preprocessor = StandardScaler()

5. Build the Pipeline

  • What the code does: It instantiates a KNeighborsClassifier with `n_neighbors=5` and combines it with the `preprocessor` into a `Pipeline` object from sklearn.
  • Behind the scenes: The K-NN algorithm is a non-parametric, lazy learning method. It doesn't build a model during training but simply stores the training data. For prediction, it finds the `k` (in this case, 5) nearest data points to the new point and predicts the class based on a majority vote among those neighbors.
  • 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.neighbors import KNeighborsClassifier

knn_clf = KNeighborsClassifier(n_neighbors=5)

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

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 `StandardScaler` on `X_train`. The K-NN model stores the scaled training data. When you call `predict()`, the pipeline automatically applies the same standardization to `X_test` before the K-NN classifier finds the nearest neighbors and 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 accuracy_score() and f1_score() functions from sklearn.metrics are used to calculate the model's performance on the test set.
  • Behind the scenes: In a multi-class problem like this, `f1_score` with `average="macro"` is useful as it calculates the F1 score for each class and takes an unweighted average, ensuring all classes are treated 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