| name | Classification Modeling |
| description | Build binary and multiclass classification models using logistic regression, decision trees, and ensemble methods for categorical prediction and classification |
Classification Modeling
Overview
Classification modeling predicts categorical target values, assigning observations to discrete classes or categories based on input features.
When to Use
- Predicting binary outcomes like customer churn, loan default, or email spam
- Classifying items into multiple categories such as product types or sentiment
- Building credit scoring models or risk assessment systems
- Identifying disease diagnosis or medical condition from patient data
- Predicting customer purchase likelihood or response to marketing
- Detecting fraud, anomalies, or quality defects in production systems
Classification Types
- Binary Classification: Two classes (yes/no, success/failure)
- Multiclass: More than two classes
- Multi-label: Multiple classes per observation
Common Algorithms
- Logistic Regression: Linear classification
- Decision Trees: Rule-based non-linear
- Random Forest: Ensemble of decision trees
- Gradient Boosting: Sequential tree building
- SVM: Support Vector Machines
- Naive Bayes: Probabilistic classifier
Key Metrics
- Accuracy: Overall correct predictions
- Precision: True positives / (true + false positives)
- Recall: True positives / (true + false negatives)
- F1-Score: Harmonic mean of precision/recall
- AUC-ROC: Area under receiver operating characteristic curve
Implementation with Python
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split, cross_val_score
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LogisticRegression
from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import RandomForestClassifier, GradientBoostingClassifier
from sklearn.metrics import (
confusion_matrix, classification_report, roc_auc_score, roc_curve,
precision_recall_curve, f1_score, accuracy_score
)
import seaborn as sns
np.random.seed(42)
from sklearn.datasets import make_classification
X, y = make_classification(
n_samples=1000, n_features=20, n_informative=10,
n_redundant=5, random_state=42
)
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.2, random_state=42
)
scaler = StandardScaler()
X_train_scaled = scaler.fit_transform(X_train)
X_test_scaled = scaler.transform(X_test)
lr_model = LogisticRegression(max_iter=1000)
lr_model.fit(X_train_scaled, y_train)
y_pred_lr = lr_model.predict(X_test_scaled)
y_proba_lr = lr_model.predict_proba(X_test_scaled)[:, 1]
print("Logistic Regression:")
print(classification_report(y_test, y_pred_lr))
print()
dt_model = DecisionTreeClassifier(max_depth=, random_state=)
dt_model.fit(X_train, y_train)
y_pred_dt = dt_model.predict(X_test)
y_proba_dt = dt_model.predict_proba(X_test)[:, ]
()
(classification_report(y_test, y_pred_dt))
()
rf_model = RandomForestClassifier(n_estimators=, max_depth=, random_state=)
rf_model.fit(X_train, y_train)
y_pred_rf = rf_model.predict(X_test)
y_proba_rf = rf_model.predict_proba(X_test)[:, ]
()
(classification_report(y_test, y_pred_rf))
()
gb_model = GradientBoostingClassifier(n_estimators=, max_depth=, random_state=)
gb_model.fit(X_train, y_train)
y_pred_gb = gb_model.predict(X_test)
y_proba_gb = gb_model.predict_proba(X_test)[:, ]
()
(classification_report(y_test, y_pred_gb))
()
fig, axes = plt.subplots(, , figsize=(, ))
models = [
(y_pred_lr, ),
(y_pred_dt, ),
(y_pred_rf, ),
(y_pred_gb, ),
]
idx, (y_pred, title) (models):
cm = confusion_matrix(y_test, y_pred)
ax = axes[idx // , idx % ]
sns.heatmap(cm, annot=, fmt=, cmap=, ax=ax)
ax.set_title(title)
ax.set_ylabel()
ax.set_xlabel()
plt.tight_layout()
plt.show()
plt.figure(figsize=(, ))
probas = [
(y_proba_lr, ),
(y_proba_dt, ),
(y_proba_rf, ),
(y_proba_gb, ),
]
y_proba, label probas:
fpr, tpr, _ = roc_curve(y_test, y_proba)
auc = roc_auc_score(y_test, y_proba)
plt.plot(fpr, tpr, label=)
plt.plot([, ], [, ], , label=)
plt.xlabel()
plt.ylabel()
plt.title()
plt.legend()
plt.grid(, alpha=)
plt.show()
plt.figure(figsize=(, ))
y_proba, label probas:
precision, recall, _ = precision_recall_curve(y_test, y_proba)
f1 = f1_score(y_test, (y_proba > ).astype())
plt.plot(recall, precision, label=)
plt.xlabel()
plt.ylabel()
plt.title()
plt.legend()
plt.grid(, alpha=)
plt.show()
fig, axes = plt.subplots(, , figsize=(, ))
feature_importance_rf = pd.Series(
rf_model.feature_importances_, index=(X.shape[])
).sort_values(ascending=)
axes[].barh((), feature_importance_rf.values[:])
axes[].set_yticks(())
axes[].set_yticklabels([ i feature_importance_rf.index[:]])
axes[].set_title()
axes[].set_xlabel()
lr_coef = pd.Series(lr_model.coef_[], index=(X.shape[])).().sort_values(ascending=)
axes[].barh((), lr_coef.values[:])
axes[].set_yticks(())
axes[].set_yticklabels([ i lr_coef.index[:]])
axes[].set_title()
axes[].set_xlabel()
plt.tight_layout()
plt.show()
results = pd.DataFrame({
: [, , , ],
: [
accuracy_score(y_test, y_pred_lr),
accuracy_score(y_test, y_pred_dt),
accuracy_score(y_test, y_pred_rf),
accuracy_score(y_test, y_pred_gb),
],
: [
roc_auc_score(y_test, y_proba_lr),
roc_auc_score(y_test, y_proba_dt),
roc_auc_score(y_test, y_proba_rf),
roc_auc_score(y_test, y_proba_gb),
],
: [
f1_score(y_test, y_pred_lr),
f1_score(y_test, y_pred_dt),
f1_score(y_test, y_pred_rf),
f1_score(y_test, y_pred_gb),
]
})
()
(results)
cv_scores = cross_val_score(
RandomForestClassifier(n_estimators=, random_state=),
X_train, y_train, cv=, scoring=
)
()
()
sklearn.calibration calibration_curve
prob_true, prob_pred = calibration_curve(y_test, y_proba_rf, n_bins=)
plt.figure(figsize=(, ))
plt.plot(prob_pred, prob_true, , label=)
plt.plot([, ], [, ], , label=)
plt.xlabel()
plt.ylabel()
plt.title()
plt.legend()
plt.grid(, alpha=)
plt.show()
Class Imbalance Handling
- Oversampling: Increase minority class samples
- Undersampling: Reduce majority class samples
- SMOTE: Synthetic minority oversampling
- Class weights: Penalize misclassifying minority class
Threshold Selection
- Default (0.5): Equal misclassification cost
- Custom threshold: Based on business requirements
- Optimal: Maximizing F1-score or AUC
Deliverables
- Classification metrics (accuracy, precision, recall, F1)
- Confusion matrices for all models
- ROC and Precision-Recall curves
- Feature importance analysis
- Model comparison table
- Recommendations for best model
- Probability calibration plots