1
---
2
name: Classification Modeling
3
description: Build binary and multiclass classification models using logistic regression, decision trees, and ensemble methods for categorical prediction and classification
4
---
5
6
# Classification Modeling
7
8
## Overview
9
10
Classification modeling predicts categorical target values, assigning observations to discrete classes or categories based on input features.
11
12
## When to Use
13
14
- Predicting binary outcomes like customer churn, loan default, or email spam
15
- Classifying items into multiple categories such as product types or sentiment
16
- Building credit scoring models or risk assessment systems
17
- Identifying disease diagnosis or medical condition from patient data
18
- Predicting customer purchase likelihood or response to marketing
19
- Detecting fraud, anomalies, or quality defects in production systems
20
21
## Classification Types
22
23
- **Binary Classification**: Two classes (yes/no, success/failure)
24
- **Multiclass**: More than two classes
25
- **Multi-label**: Multiple classes per observation
26
27
## Common Algorithms
28
29
- **Logistic Regression**: Linear classification
30
- **Decision Trees**: Rule-based non-linear
31
- **Random Forest**: Ensemble of decision trees
32
- **Gradient Boosting**: Sequential tree building
33
- **SVM**: Support Vector Machines
34
- **Naive Bayes**: Probabilistic classifier
35
36
## Key Metrics
37
38
- **Accuracy**: Overall correct predictions
39
- **Precision**: True positives / (true + false positives)
40
- **Recall**: True positives / (true + false negatives)
41
- **F1-Score**: Harmonic mean of precision/recall
42
- **AUC-ROC**: Area under receiver operating characteristic curve
43
44
## Implementation with Python
45
46
```python
47
import pandas as pd
48
import numpy as np
49
import matplotlib.pyplot as plt
50
from sklearn.model_selection import train_test_split, cross_val_score
51
from sklearn.preprocessing import StandardScaler
52
from sklearn.linear_model import LogisticRegression
53
from sklearn.tree import DecisionTreeClassifier
54
from sklearn.ensemble import RandomForestClassifier, GradientBoostingClassifier
55
from sklearn.metrics import (
56
confusion_matrix, classification_report, roc_auc_score, roc_curve,
57
precision_recall_curve, f1_score, accuracy_score
58
)
59
import seaborn as sns
60
61
# Generate sample binary classification data
62
np.random.seed(42)
63
from sklearn.datasets import make_classification
64
65
X, y = make_classification(
66
n_samples=1000, n_features=20, n_informative=10,
67
n_redundant=5, random_state=42
68
)
69
70
X_train, X_test, y_train, y_test = train_test_split(
71
X, y, test_size=0.2, random_state=42
72
)
73
74
# Standardize features
75
scaler = StandardScaler()
76
X_train_scaled = scaler.fit_transform(X_train)
77
X_test_scaled = scaler.transform(X_test)
78
79
# Logistic Regression
80
lr_model = LogisticRegression(max_iter=1000)
81
lr_model.fit(X_train_scaled, y_train)
82
y_pred_lr = lr_model.predict(X_test_scaled)
83
y_proba_lr = lr_model.predict_proba(X_test_scaled)[:, 1]
84
85
print("Logistic Regression:")
86
print(classification_report(y_test, y_pred_lr))
87
print(f"AUC-ROC: {roc_auc_score(y_test, y_proba_lr):.4f}\n")
88
89
# Decision Tree
90
dt_model = DecisionTreeClassifier(max_depth=10, random_state=42)
91
dt_model.fit(X_train, y_train)
92
y_pred_dt = dt_model.predict(X_test)
93
y_proba_dt = dt_model.predict_proba(X_test)[:, 1]
94
95
print("Decision Tree:")
96
print(classification_report(y_test, y_pred_dt))
97
print(f"AUC-ROC: {roc_auc_score(y_test, y_proba_dt):.4f}\n")
98
99
# Random Forest
100
rf_model = RandomForestClassifier(n_estimators=100, max_depth=10, random_state=42)
101
rf_model.fit(X_train, y_train)
102
y_pred_rf = rf_model.predict(X_test)
103
y_proba_rf = rf_model.predict_proba(X_test)[:, 1]
104
105
print("Random Forest:")
106
print(classification_report(y_test, y_pred_rf))
107
print(f"AUC-ROC: {roc_auc_score(y_test, y_proba_rf):.4f}\n")
108
109
# Gradient Boosting
110
gb_model = GradientBoostingClassifier(n_estimators=100, max_depth=5, random_state=42)
111
gb_model.fit(X_train, y_train)
112
y_pred_gb = gb_model.predict(X_test)
113
y_proba_gb = gb_model.predict_proba(X_test)[:, 1]
114
115
print("Gradient Boosting:")
116
print(classification_report(y_test, y_pred_gb))
117
print(f"AUC-ROC: {roc_auc_score(y_test, y_proba_gb):.4f}\n")
118
119
# Confusion matrices
120
fig, axes = plt.subplots(2, 2, figsize=(12, 10))
121
122
models = [
123
(y_pred_lr, 'Logistic Regression'),
124
(y_pred_dt, 'Decision Tree'),
125
(y_pred_rf, 'Random Forest'),
126
(y_pred_gb, 'Gradient Boosting'),
127
]
128
129
for idx, (y_pred, title) in enumerate(models):
130
cm = confusion_matrix(y_test, y_pred)
131
ax = axes[idx // 2, idx % 2]
132
sns.heatmap(cm, annot=True, fmt='d', cmap='Blues', ax=ax)
133
ax.set_title(title)
134
ax.set_ylabel('True Label')
135
ax.set_xlabel('Predicted Label')
136
137
plt.tight_layout()
138
plt.show()
139
140
# ROC Curves
141
plt.figure(figsize=(10, 8))
142
143
probas = [
144
(y_proba_lr, 'Logistic Regression'),
145
(y_proba_dt, 'Decision Tree'),
146
(y_proba_rf, 'Random Forest'),
147
(y_proba_gb, 'Gradient Boosting'),
148
]
149
150
for y_proba, label in probas:
151
fpr, tpr, _ = roc_curve(y_test, y_proba)
152
auc = roc_auc_score(y_test, y_proba)
153
plt.plot(fpr, tpr, label=f'{label} (AUC={auc:.4f})')
154
155
plt.plot([0, 1], [0, 1], 'k--', label='Random Classifier')
156
plt.xlabel('False Positive Rate')
157
plt.ylabel('True Positive Rate')
158
plt.title('ROC Curves Comparison')
159
plt.legend()
160
plt.grid(True, alpha=0.3)
161
plt.show()
162
163
# Precision-Recall Curves
164
plt.figure(figsize=(10, 8))
165
166
for y_proba, label in probas:
167
precision, recall, _ = precision_recall_curve(y_test, y_proba)
168
f1 = f1_score(y_test, (y_proba > 0.5).astype(int))
169
plt.plot(recall, precision, label=f'{label} (F1={f1:.4f})')
170
171
plt.xlabel('Recall')
172
plt.ylabel('Precision')
173
plt.title('Precision-Recall Curves')
174
plt.legend()
175
plt.grid(True, alpha=0.3)
176
plt.show()
177
178
# Feature importance
179
fig, axes = plt.subplots(1, 2, figsize=(14, 5))
180
181
# Tree-based feature importance
182
feature_importance_rf = pd.Series(
183
rf_model.feature_importances_, index=range(X.shape[1])
184
).sort_values(ascending=False)
185
186
axes[0].barh(range(10), feature_importance_rf.values[:10])
187
axes[0].set_yticks(range(10))
188
axes[0].set_yticklabels([f'Feature {i}' for i in feature_importance_rf.index[:10]])
189
axes[0].set_title('Random Forest - Top 10 Features')
190
axes[0].set_xlabel('Importance')
191
192
# Logistic regression coefficients
193
lr_coef = pd.Series(lr_model.coef_[0], index=range(X.shape[1])).abs().sort_values(ascending=False)
194
axes[1].barh(range(10), lr_coef.values[:10])
195
axes[1].set_yticks(range(10))
196
axes[1].set_yticklabels([f'Feature {i}' for i in lr_coef.index[:10]])
197
axes[1].set_title('Logistic Regression - Top 10 Features (abs coef)')
198
axes[1].set_xlabel('Absolute Coefficient')
199
200
plt.tight_layout()
201
plt.show()
202
203
# Model comparison
204
results = pd.DataFrame({
205
'Model': ['Logistic Regression', 'Decision Tree', 'Random Forest', 'Gradient Boosting'],
206
'Accuracy': [
207
accuracy_score(y_test, y_pred_lr),
208
accuracy_score(y_test, y_pred_dt),
209
accuracy_score(y_test, y_pred_rf),
210
accuracy_score(y_test, y_pred_gb),
211
],
212
'AUC-ROC': [
213
roc_auc_score(y_test, y_proba_lr),
214
roc_auc_score(y_test, y_proba_dt),
215
roc_auc_score(y_test, y_proba_rf),
216
roc_auc_score(y_test, y_proba_gb),
217
],
218
'F1-Score': [
219
f1_score(y_test, y_pred_lr),
220
f1_score(y_test, y_pred_dt),
221
f1_score(y_test, y_pred_rf),
222
f1_score(y_test, y_pred_gb),
223
]
224
})
225
226
print("Model Comparison:")
227
print(results)
228
229
# Cross-validation
230
cv_scores = cross_val_score(
231
RandomForestClassifier(n_estimators=100, random_state=42),
232
X_train, y_train, cv=5, scoring='roc_auc'
233
)
234
print(f"\nCross-validation AUC scores: {cv_scores}")
235
print(f"Mean CV AUC: {cv_scores.mean():.4f} (+/- {cv_scores.std():.4f})")
236
237
# Probability calibration
238
from sklearn.calibration import calibration_curve
239
240
prob_true, prob_pred = calibration_curve(y_test, y_proba_rf, n_bins=10)
241
242
plt.figure(figsize=(8, 6))
243
plt.plot(prob_pred, prob_true, 'o-', label='Random Forest')
244
plt.plot([0, 1], [0, 1], 'k--', label='Perfect Calibration')
245
plt.xlabel('Mean Predicted Probability')
246
plt.ylabel('Fraction of Positives')
247
plt.title('Calibration Curve')
248
plt.legend()
249
plt.grid(True, alpha=0.3)
250
plt.show()
251
```
252
253
## Class Imbalance Handling
254
255
- **Oversampling**: Increase minority class samples
256
- **Undersampling**: Reduce majority class samples
257
- **SMOTE**: Synthetic minority oversampling
258
- **Class weights**: Penalize misclassifying minority class
259
260
## Threshold Selection
261
262
- **Default (0.5)**: Equal misclassification cost
263
- **Custom threshold**: Based on business requirements
264
- **Optimal**: Maximizing F1-score or AUC
265
266
## Deliverables
267
268
- Classification metrics (accuracy, precision, recall, F1)
269
- Confusion matrices for all models
270
- ROC and Precision-Recall curves
271
- Feature importance analysis
272
- Model comparison table
273
- Recommendations for best model
274
- Probability calibration plots
275