-
Notifications
You must be signed in to change notification settings - Fork 0
Visualisation
|
Training progress |
Classification regions |
Prediction breakdown |
Performance analysis |
Track how the cost decreases during training to ensure convergence.
import matplotlib.pyplot as plt
import numpy as np
def plot_cost_history(cost_history):
"""
Plot cost function over iterations
Parameters:
-----------
cost_history : list
List of cost values from training
"""
plt.figure(figsize=(12, 6))
# Main plot
plt.plot(cost_history, linewidth=2.5, color='#2E86AB', label='Training Cost')
# Styling
plt.xlabel('Iterations', fontsize=14, fontweight='bold')
plt.ylabel('Cost (Binary Cross-Entropy)', fontsize=14, fontweight='bold')
plt.title('π Cost Function Convergence', fontsize=16, fontweight='bold', pad=20)
plt.grid(True, alpha=0.3, linestyle='--')
plt.legend(fontsize=12, loc='upper right')
# Add annotations
final_cost = cost_history[-1]
plt.annotate(f'Final Cost: {final_cost:.4f}',
xy=(len(cost_history)-1, final_cost),
xytext=(len(cost_history)*0.7, final_cost*1.2),
fontsize=11,
bbox=dict(boxstyle='round,pad=0.5', facecolor='yellow', alpha=0.7),
arrowprops=dict(arrowstyle='->', color='black', lw=1.5))
plt.tight_layout()
plt.show()
# Usage
plot_cost_history(model.cost_history)Good Convergence β
Oscillation β No Convergence β
Cost Cost Cost
β\ β/\ βββββββββ
β \ β \/\ β
β \___ β /\ β
β ββββ β / \/ β
βββββββββββ Iter βββββββββββ Iter βββββββββββ Iter
| Pattern | Meaning | Action |
|: --------|:--------|:-------|
| π Smooth decrease | Learning well | β
Continue |
| π Plateau | Converged | β
Can stop |
| γ°οΈ Oscillation | Learning rate too high |
Visualize how the model separates the two classes in feature space.
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.colors import ListedColormap
def plot_decision_boundary(X, y, model, title="Decision Boundary"):
"""
Plot decision boundary for 2D features
Parameters:
-----------
X : array, shape (m, 2) - only works with 2 features
y : array, shape (m,)
model : trained LogisticRegression model
title : str, plot title
"""
# Set up the figure
plt.figure(figsize=(12, 8))
# Create color maps
cmap_light = ListedColormap(['#FFAAAA', '#AAAAFF'])
cmap_bold = ListedColormap(['#FF0000', '#0000FF'])
# Create mesh
h = 0.02 # step size in mesh
x_min, x_max = X[:, 0]. min() - 1, X[:, 0].max() + 1
y_min, y_max = X[:, 1].min() - 1, X[:, 1].max() + 1
xx, yy = np.meshgrid(np.arange(x_min, x_max, h),
np.arange(y_min, y_max, h))
# Predict on mesh
Z = model.predict(np.c_[xx.ravel(), yy.ravel()])
Z = Z.reshape(xx.shape)
# Plot decision boundary
plt.contourf(xx, yy, Z, cmap=cmap_light, alpha=0.4)
plt.contour(xx, yy, Z, colors='black', linewidths=2, levels=[0.5])
# Plot data points
scatter = plt.scatter(X[:, 0], X[:, 1], c=y, cmap=cmap_bold,
edgecolor='black', s=100, alpha=0.8)
# Add legend
plt.legend(*scatter.legend_elements(), title="Classes", loc="upper right", fontsize=11)
# Labels and title
plt.xlabel('Feature 1', fontsize=14, fontweight='bold')
plt.ylabel('Feature 2', fontsize=14, fontweight='bold')
plt.title(f'π― {title}', fontsize=16, fontweight='bold', pad=20)
plt.grid(True, alpha=0.2)
plt.tight_layout()
plt.show()
# Usage (only works with 2 features)
# For more features, use PCA to reduce to 2D first
from sklearn.decomposition import PCA
pca = PCA(n_components=2)
X_2d = pca.fit_transform(X_train)
plot_decision_boundary(X_2d, y_train, model) Feature 2
β
1 β β β β β β
β β β β β β β
0 β βββββββββββββββ β Decision Boundary
β Γ Γ Γ Γ Γ
-1 βΓ Γ Γ Γ
βββββββββββββββββ Feature 1
-1 0 1
β = Class 1 (Positive)
Γ = Class 0 (Negative)
Show the breakdown of correct and incorrect predictions.
import seaborn as sns
import matplotlib. pyplot as plt
from sklearn.metrics import confusion_matrix
import numpy as np
def plot_confusion_matrix(y_true, y_pred, class_names=['Class 0', 'Class 1']):
"""
Plot beautiful confusion matrix
Enhanced by GitHub Copilot for better visuals
Parameters:
-----------
y_true : array, true labels
y_pred : array, predicted labels
class_names : list, names of classes
"""
# Compute confusion matrix
cm = confusion_matrix(y_true, y_pred)
# Create figure
fig, ax = plt.subplots(figsize=(10, 8))
# Plot heatmap with custom colors
sns.heatmap(cm, annot=True, fmt='d', cmap='Blues',
cbar_kws={'label': 'Count'},
linewidths=2, linecolor='black',
square=True, ax=ax,
annot_kws={'size': 20, 'weight': 'bold'})
# Labels
ax.set_xlabel('Predicted Label', fontsize=14, fontweight='bold', labelpad=10)
ax.set_ylabel('True Label', fontsize=14, fontweight='bold', labelpad=10)
ax.set_title('π― Confusion Matrix\n(Enhanced by GitHub Copilot)',
fontsize=16, fontweight='bold', pad=20)
# Set tick labels
ax.set_xticklabels(class_names, fontsize=12)
ax.set_yticklabels(class_names, fontsize=12, rotation=0)
# Add performance metrics as text
tn, fp, fn, tp = cm.ravel()
accuracy = (tp + tn) / (tp + tn + fp + fn)
precision = tp / (tp + fp) if (tp + fp) > 0 else 0
recall = tp / (tp + fn) if (tp + fn) > 0 else 0
f1 = 2 * (precision * recall) / (precision + recall) if (precision + recall) > 0 else 0
# Add text box with metrics
textstr = f'Accuracy: {accuracy:.2%}\nPrecision: {precision:.2%}\nRecall: {recall:.2%}\nF1-Score: {f1:.2%}'
props = dict(boxstyle='round', facecolor='wheat', alpha=0.8)
ax.text(1.35, 0.5, textstr, transform=ax. transAxes, fontsize=12,
verticalalignment='center', bbox=props)
plt.tight_layout()
plt.show()
# Print detailed breakdown
print("π Confusion Matrix Breakdown:")
print(f" True Negatives (TN): {tn}")
print(f" False Positives (FP): {fp}")
print(f" False Negatives (FN): {fn}")
print(f" True Positives (TP): {tp}")
# Usage
y_pred = model.predict(X_test)
plot_confusion_matrix(y_test, y_pred) Predicted
Neg Pos
βββββββββββββββββββββββ
N β TN β FP β
Actual e β (Correctβ (Type I β
g β Reject)β Error) β
βββββββββββββββββββββββ€
P β FN β TP β
o β(Type II β (Correct β
s β Error) β Accept) β
βββββββββββββββββββββββ
| Metric | Formula | Meaning | |: -------|:--------|:--------| | Accuracy | (TP+TN)/(TP+TN+FP+FN) | Overall correctness | | Precision | TP/(TP+FP) | Positive prediction accuracy | | Recall | TP/(TP+FN) | True positive detection rate | | F1-Score | 2Γ(PrecisionΓRecall)/(Precision+Recall) | Harmonic mean |
Evaluate model performance across all classification thresholds.
from sklearn.metrics import roc_curve, auc
import matplotlib.pyplot as plt
def plot_roc_curve(y_true, y_pred_proba):
"""
Plot ROC curve with AUC score
Enhanced by GitHub Copilot for stunning visuals
Parameters:
-----------
y_true : array, true binary labels
y_pred_proba : array, predicted probabilities
"""
# Compute ROC curve
fpr, tpr, thresholds = roc_curve(y_true, y_pred_proba)
roc_auc = auc(fpr, tpr)
# Create figure
plt.figure(figsize=(10, 8))
# Plot ROC curve
plt. plot(fpr, tpr, color='darkorange', linewidth=3,
label=f'ROC Curve (AUC = {roc_auc:.3f})')
# Plot diagonal (random classifier)
plt.plot([0, 1], [0, 1], color='navy', linewidth=2,
linestyle='--', label='Random Classifier (AUC = 0.5)')
# Fill area under curve
plt.fill_between(fpr, tpr, alpha=0.3, color='darkorange')
# Styling
plt.xlim([0.0, 1.0])
plt.ylim([0.0, 1.05])
plt.xlabel('False Positive Rate (1 - Specificity)', fontsize=14, fontweight='bold')
plt.ylabel('True Positive Rate (Sensitivity)', fontsize=14, fontweight='bold')
plt.title('π ROC Curve - Receiver Operating Characteristic\n(Enhanced by GitHub Copilot)',
fontsize=16, fontweight='bold', pad=20)
plt.legend(loc="lower right", fontsize=12, frameon=True, shadow=True)
plt.grid(True, alpha=0.3, linestyle='--')
# Add annotations for interpretation
if roc_auc > 0.9:
interpretation = "Excellent Model! π"
color = 'green'
elif roc_auc > 0.8:
interpretation = "Good Model β
"
color = 'blue'
elif roc_auc > 0.7:
interpretation = "Fair Model β οΈ"
color = 'orange'
else:
interpretation = "Poor Model β"
color = 'red'
plt.text(0.6, 0.2, interpretation, fontsize=14, fontweight='bold',
bbox=dict(boxstyle='round', facecolor=color, alpha=0.3))
plt.tight_layout()
plt.show()
print(f"\nπ ROC Analysis:")
print(f" AUC Score: {roc_auc:. 4f}")
print(f" Interpretation: {interpretation}")
# Usage
y_pred_proba = model.predict_proba(X_test)
plot_roc_curve(y_test, y_pred_proba)TPR (Sensitivity)
1 β βββββ
β /
β /
0. 5 β /
β /
β /
0 ββββββββββββββββ FPR
0 0.5 1
Perfect Classifier: Hugs top-left corner
Random Classifier: Diagonal line
| AUC Score | Interpretation | Quality |
|---|---|---|
| 0.9 - 1.0 | Excellent | πππππ |
| 0.8 - 0.9 | Good | ππππ |
| 0.7 - 0.8 | Fair | πππ |
| 0.6 - 0.7 | Poor | ππ |
| 0.5 - 0.6 | Very Poor | π |
| < 0.5 | Worse than random | β |
from sklearn.metrics import precision_recall_curve, average_precision_score
import matplotlib. pyplot as plt
def plot_precision_recall_curve(y_true, y_pred_proba):
"""
Plot Precision-Recall curve
Parameters:
-----------
y_true : array, true labels
y_pred_proba : array, predicted probabilities
"""
precision, recall, thresholds = precision_recall_curve(y_true, y_pred_proba)
avg_precision = average_precision_score(y_true, y_pred_proba)
plt.figure(figsize=(10, 8))
# Plot PR curve
plt.plot(recall, precision, linewidth=3, color='purple',
label=f'PR Curve (AP = {avg_precision:.3f})')
# Fill area
plt.fill_between(recall, precision, alpha=0.3, color='purple')
# Styling
plt.xlabel('Recall (Sensitivity)', fontsize=14, fontweight='bold')
plt.ylabel('Precision', fontsize=14, fontweight='bold')
plt.title('π Precision-Recall Curve', fontsize=16, fontweight='bold', pad=20)
plt.legend(loc='upper right', fontsize=12, frameon=True, shadow=True)
plt.grid(True, alpha=0.3, linestyle='--')
plt.xlim([0.0, 1.0])
plt.ylim([0.0, 1.05])
plt.tight_layout()
plt.show()
# Usage
plot_precision_recall_curve(y_test, y_pred_proba)import matplotlib.pyplot as plt
import numpy as np
def plot_feature_importance(model, feature_names):
"""
Plot feature importance based on learned weights
Parameters:
-----------
model : trained LogisticRegression model
feature_names : list, names of features
"""
# Get absolute weights
importance = np.abs(model.weights)
# Sort by importance
indices = np.argsort(importance)[::-1]
plt.figure(figsize=(12, 8))
# Create bar plot
colors = plt.cm.viridis(np.linspace(0, 1, len(importance)))
bars = plt.barh(range(len(importance)), importance[indices], color=colors)
# Styling
plt.yticks(range(len(importance)), [feature_names[i] for i in indices])
plt.xlabel('Importance (Absolute Weight)', fontsize=14, fontweight='bold')
plt.ylabel('Features', fontsize=14, fontweight='bold')
plt.title('π Feature Importance Analysis', fontsize=16, fontweight='bold', pad=20)
plt.grid(True, alpha=0.3, axis='x')
# Add value labels
for i, bar in enumerate(bars):
width = bar. get_width()
plt.text(width, bar.get_y() + bar.get_height()/2,
f'{width:.4f}',
ha='left', va='center', fontsize=10, fontweight='bold')
plt.tight_layout()
plt.show()
# Usage (if you have feature names)
feature_names = data. feature_names # from sklearn dataset
plot_feature_importance(model, feature_names)import matplotlib.pyplot as plt
import seaborn as sns
def plot_probability_distribution(y_true, y_pred_proba):
"""
Plot distribution of predicted probabilities
Parameters:
-----------
y_true : array, true labels
y_pred_proba : array, predicted probabilities
"""
plt.figure(figsize=(12, 6))
# Separate probabilities by true class
prob_class_0 = y_pred_proba[y_true == 0]
prob_class_1 = y_pred_proba[y_true == 1]
# Plot histograms
plt.hist(prob_class_0, bins=50, alpha=0.6, color='red',
label='True Class 0', edgecolor='black')
plt.hist(prob_class_1, bins=50, alpha=0.6, color='blue',
label='True Class 1', edgecolor='black')
# Add decision threshold
plt.axvline(x=0.5, color='green', linestyle='--', linewidth=2,
label='Decision Threshold (0.5)')
# Styling
plt.xlabel('Predicted Probability', fontsize=14, fontweight='bold')
plt.ylabel('Frequency', fontsize=14, fontweight='bold')
plt.title('π Probability Distribution by True Class',
fontsize=16, fontweight='bold', pad=20)
plt.legend(fontsize=12, loc='upper center')
plt.grid(True, alpha=0.3, axis='y')
plt.tight_layout()
plt.show()
# Usage
plot_probability_distribution(y_test, y_pred_proba)import matplotlib.pyplot as plt
from matplotlib.gridspec import GridSpec
def create_visualization_dashboard(model, X_train, X_test, y_train, y_test):
"""
Create comprehensive visualization dashboard
Parameters:
-----------
model : trained model
X_train, X_test : training and test features
y_train, y_test : training and test labels
"""
# Make predictions
y_pred = model.predict(X_test)
y_pred_proba = model. predict_proba(X_test)
# Create figure with subplots
fig = plt.figure(figsize=(20, 12))
gs = GridSpec(2, 3, figure=fig, hspace=0.3, wspace=0.3)
# 1. Cost History
ax1 = fig.add_subplot(gs[0, 0])
ax1.plot(model.cost_history, linewidth=2, color='#2E86AB')
ax1.set_xlabel('Iterations', fontweight='bold')
ax1.set_ylabel('Cost', fontweight='bold')
ax1.set_title('π Learning Curve', fontweight='bold', pad=10)
ax1.grid(True, alpha=0.3)
# 2. Confusion Matrix
ax2 = fig.add_subplot(gs[0, 1])
cm = confusion_matrix(y_test, y_pred)
sns.heatmap(cm, annot=True, fmt='d', cmap='Blues', ax=ax2,
linewidths=2, cbar=False, square=True,
annot_kws={'size': 16, 'weight': 'bold'})
ax2.set_xlabel('Predicted', fontweight='bold')
ax2.set_ylabel('Actual', fontweight='bold')
ax2.set_title('π― Confusion Matrix', fontweight='bold', pad=10)
# 3. ROC Curve
ax3 = fig.add_subplot(gs[0, 2])
fpr, tpr, _ = roc_curve(y_test, y_pred_proba)
roc_auc = auc(fpr, tpr)
ax3.plot(fpr, tpr, linewidth=2, color='darkorange',
label=f'AUC = {roc_auc:.3f}')
ax3.plot([0, 1], [0, 1], 'k--', linewidth=1)
ax3.fill_between(fpr, tpr, alpha=0.3, color='darkorange')
ax3.set_xlabel('FPR', fontweight='bold')
ax3.set_ylabel('TPR', fontweight='bold')
ax3.set_title('π ROC Curve', fontweight='bold', pad=10)
ax3.legend(loc='lower right')
ax3.grid(True, alpha=0.3)
# 4. Precision-Recall Curve
ax4 = fig.add_subplot(gs[1, 0])
precision, recall, _ = precision_recall_curve(y_test, y_pred_proba)
avg_precision = average_precision_score(y_test, y_pred_proba)
ax4.plot(recall, precision, linewidth=2, color='purple',
label=f'AP = {avg_precision:.3f}')
ax4.fill_between(recall, precision, alpha=0.3, color='purple')
ax4.set_xlabel('Recall', fontweight='bold')
ax4.set_ylabel('Precision', fontweight='bold')
ax4.set_title('π Precision-Recall', fontweight='bold', pad=10)
ax4.legend(loc='upper right')
ax4.grid(True, alpha=0.3)
# 5. Probability Distribution
ax5 = fig.add_subplot(gs[1, 1])
prob_0 = y_pred_proba[y_test == 0]
prob_1 = y_pred_proba[y_test == 1]
ax5.hist(prob_0, bins=30, alpha=0.6, color='red', label='Class 0')
ax5.hist(prob_1, bins=30, alpha=0.6, color='blue', label='Class 1')
ax5.axvline(x=0.5, color='green', linestyle='--', linewidth=2)
ax5.set_xlabel('Predicted Probability', fontweight='bold')
ax5.set_ylabel('Frequency', fontweight='bold')
ax5.set_title('π Probability Distribution', fontweight='bold', pad=10)
ax5.legend()
ax5.grid(True, alpha=0.3, axis='y')
# 6. Metrics Summary
ax6 = fig.add_subplot(gs[1, 2])
ax6.axis('off')
# Calculate metrics
tn, fp, fn, tp = cm.ravel()
accuracy = (tp + tn) / (tp + tn + fp + fn)
precision_score = tp / (tp + fp) if (tp + fp) > 0 else 0
recall_score = tp / (tp + fn) if (tp + fn) > 0 else 0
f1 = 2 * (precision_score * recall_score) / (precision_score + recall_score) if (precision_score + recall_score) > 0 else 0
metrics_text = f"""
π MODEL PERFORMANCE METRICS
βββββββββββββββββββββββββββββββ
β
Accuracy: {accuracy:.2%}
π― Precision: {precision_score:.2%}
π Recall: {recall_score:.2%}
β F1-Score: {f1:.2%}
π AUC-ROC: {roc_auc:.2%}
βββββββββββββββββββββββββββββββ
CONFUSION MATRIX VALUES:
True Negatives (TN): {tn}
False Positives (FP): {fp}
False Negatives (FN): {fn}
True Positives (TP): {tp}
"""
ax6.text(0.1, 0.5, metrics_text, fontsize=12, fontfamily='monospace',
verticalalignment='center',
bbox=dict(boxstyle='round', facecolor='lightblue', alpha=0.8))
plt.suptitle('π― LOGISTIC REGRESSION - COMPLETE ANALYSIS DASHBOARD',
fontsize=20, fontweight='bold', y=0.98)
plt.show()
# Usage
create_visualization_dashboard(model, X_train, X_test, y_train, y_test)# Professional color schemes
colors = {
'primary': '#2E86AB', # Blue
'secondary': '#A23B72', # Purple
'success': '#06A77D', # Green
'warning': '#F18F01', # Orange
'danger': '#C73E1D', # Red
'info': '#6A98F0', # Light Blue
}
# Seaborn palettes
sns.set_palette("husl") # Colorful
sns.set_palette("Set2") # Soft colors
sns.set_palette("dark") # Dark theme# Set global theme
plt.style.use('seaborn-v0_8-darkgrid') # Professional look
plt.style.use('ggplot') # R-style
plt.style.use('fivethirtyeight') # FiveThirtyEight style
# Custom theme
sns.set_theme(style="whitegrid", palette="pastel")
|
|
# Save high-quality figures
plt.savefig('confusion_matrix.png', dpi=300, bbox_inches='tight')
plt.savefig('roc_curve.pdf', format='pdf', bbox_inches='tight')
plt.savefig('dashboard.svg', format='svg', bbox_inches='tight')
# Save all formats
formats = ['png', 'pdf', 'svg']
for fmt in formats:
plt.savefig(f'visualization. {fmt}', dpi=300, bbox_inches='tight')π¨ Confusion Matrix & ROC Curve visualizations enhanced by GitHub Copilot