Breast Cancer Classification Pipeline
Breast Cancer Classification Pipeline
A step-by-step guide to building a machine learning model to classify tumors as benign or malignant using a Support Vector Machine (SVM).
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_breast_cancer()to fetch a dataset containing medical measurements of tumors. The `as_frame=True` argument ensures the data is returned as a pandas DataFrame. - Behind the scenes: This dataset is a classic binary classification problem. The goal is to predict if a tumor is malignant (class 1) or benign (class 0) based on 30 features like `mean radius`, `mean texture`, etc.
- 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_breast_cancer(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 binary values (0 for benign, 1 for malignant). The `X` variable contains 30 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 malignant and benign samples 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 SVMs, which are sensitive to the scale of the features. The breast cancer dataset contains features with different units and scales (e.g., `radius_mean` vs. `area_mean`), so scaling is necessary.
- 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 SVM.
- 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 Support Vector Classifier (SVC) with a radial basis function (RBF) kernel and combines it with the `preprocessor` into a `Pipeline` object from sklearn.
- Behind the scenes: An SVM works by finding the optimal hyperplane that separates the data points into classes with the largest possible margin. The "support vectors" are the data points closest to this hyperplane. The RBF kernel allows the SVM to find a non-linear decision boundary in cases where the data is not linearly separable.
- 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.svm import SVC
svm_clf = SVC(kernel="rbf", probability=True, random_state=42)
pipe = Pipeline([
("preprocessor", preprocessor),
("classifier", svm_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` (learning the mean and standard deviation) and then transforms the data. This transformed data is then used to train the SVC to find the optimal separating hyperplane. When you call `predict()`, the pipeline automatically applies the same standardization to `X_test` before the trained SVC 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()andf1_score()functions from sklearn.metrics are used to calculate the model's performance on the test set. - Behind the scenes: In a binary classification problem like this, metrics like F1 score are crucial, especially if one class (e.g., malignant) is more important to identify correctly than the other. The F1 score is the harmonic mean of precision and recall. We also calculate the `roc_auc_score` which measures the model's ability to distinguish between the two classes.
- 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, roc_auc_score
accuracy = accuracy_score(y_test, y_pred)
f1 = f1_score(y_test, y_pred)
proba = pipe.predict_proba(X_test)[:, 1]
roc_auc = roc_auc_score(y_test, proba)
print(f"Accuracy: {accuracy:.4f}")
print(f"F1 Score: {f1:.4f}")
print(f"ROC-AUC: {roc_auc:.4f}")
Comments
Post a Comment