Breast Cancer Classification with SVM and RFE in Python: Step-by-Step Machine Learning Workflow

Classified in Electronics

Written on in English with a size of 5.5 KB

1. Libraries

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.datasets import load_breast_cancer TE LO DA
from sklearn.model_selection import train_test_split
from sklearn.svm import SVC
from sklearn.feature_selection import RFE
from sklearn.preprocessing import StandardScaler
from sklearn.compose import ColumnTransformer
from sklearn.pipeline import Pipeline
from sklearn.model_selection import GridSearchCV
from sklearn.metrics import classification_report
from sklearn.preprocessing import OneHotEncoder

2. Load Data

data = load_breast_cancer()
X = pd.DataFrame(data.data, columns=data.feature_names)
y = pd.Series(data.target)

3. Clean Data

datos.isnull().sum()

datos.replace("?", np.nan, inplace=True)
datos.dropna(inplace=True)

4. Train and Test Split

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

5. Group Categories

lista_categoricas = [' ', ' ',]
lista_numericas = [' ', ' ']

6. Transformation

transformacion = StandardScaler()
X_train_tr = transformacion.fit_transform(X_train)
X_test_tr = transformacion.transform(X_test)

When variables are not of the same type:

preprocessor = ColumnTransformer([
('lista_numericas', StandardScaler(), lista_numericas),
('lista_categoricas', OneHotEncoder(drop='first'), lista_categoricas)])

7. RFE

from sklearn.linear_model import LogisticRegression
modelo_base = LogisticRegression(max_iter=4000) or SVC(kernel='linear')
rfe = RFE(estimator=modelo_base, n_features_to_select=5)
rfe.fit(X, y)

rfe.ranking_
rfe.support_
rfe = RFE(estimator=modelo_base, n_features_to_select=4)
X_train_tr_rfe = rfe.fit_transform(X_train_tr, y_train)
X_test_tr_rfe = rfe.transform(X_test_tr)

8. Grid Search

svc = SVC(probability=True)
grid = {
'C': [0.1, 1, 50],
'kernel': ['linear', 'rbf', 'poly'], CHANGE ACCORDING TO
'gamma': [0.001, 0.01, 0.1],
'degree': [2, 3],
'class_weight': [None]

scoring = ['recall_macro', 'f1_macro', 'accuracy']
gridsearch = GridSearchCV(SVC(max_iter=1000, probability=True), param_grid=grid, cv=4, refit='accuracy',
scoring=scoring)

gridsearch.fit(X_train_tr_rfe, y_train)

9. Evaluation

results = pd.DataFrame(gridsearch.cv_results_)

best_param = gridsearch.best_params_
best_param

best_model = gridsearch.best_estimator_
best_model

y_pred = best_model.predict_proba(X_test_tr_rfe)

accuracy = accuracy_score(y_test, y_pred)
print(f"Accuracy: {accuracy:.4f}")
print(classification_report(y_test, y_pred))

ConfusionMatrixDisplay.from_estimator(svm_model, X_test, y_test, cmap='Blues')
plt.show()

10. Boxplot

svc1 = results[['split0_test_accuracy', 'split1_test_accuracy', 'split2_test_accuracy', 'split3_test_accuracy']].iloc[1]
svc3 = results[['split0_test_accuracy', 'split1_test_accuracy', 'split2_test_accuracy', 'split3_test_accuracy']].iloc[3]

plt.figure(figsize=(8,4))
plt.boxplot([svc1.values, svc3.values, svc5.values, svc15.values], labels=['svc1', 'svc3', 'svc5', 'svc15'])

11. ROC Curve

f1 = f1_score(y_test, y_fit1, average='weighted')
roc_auc = roc_auc_score(y_test, model1.decision_function(X_test)) # AUC-ROC

print(f"F1-score: {f1:.4f}")
print(f"AUC-ROC: {roc_auc:.4f}")

# Convert labels to numeric values if necessary
y_test_numeric_model1 = np.where(y_test == '>50K', 1, 0)

# Compute the ROC curve
fpr, tpr, _ = roc_curve(y_test_numeric_model1, model1.decision_function(X_test))
roc_auc = auc(fpr, tpr)

# Display the AUC value
print(f'The area under the ROC curve (AUC) is: {roc_auc:.4f}')

plt.figure(figsize=(5,5))
plt.plot(fpr, tpr, color='orange', lw=2, linestyle='--', label=f'ROC curve (AUC = {roc_auc:.2f})')
plt.plot([0,1], [0,1], color='navy', lw=1, linestyle='--')

plt.xlim([0.0, 1.0])
plt.ylim([0.0, 1.05])
plt.xlabel('FPR = 1 - Specificity')
plt.ylabel('Sensitivity (Recall)')
plt.title('ROC Curve Model1')
plt.legend(loc='lower right')
plt.show()
plt.close()

Related entries: