-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvisualize.py
118 lines (100 loc) · 3.55 KB
/
visualize.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import matplotlib.pyplot as plt # type: ignore
import seaborn as sns # type: ignore
import pandas as pd
def plot_feature_importance(model, feature_names, top_n=10):
"""
Plot the top N most important features of a model.
Args:
model: Trained model with feature importance attribute.
feature_names (list): List of feature names.
top_n (int): Number of top features to plot. Defaults to 10.
Returns:
None
"""
importance = model.feature_importances_
feature_importance = pd.DataFrame({
'Feature': feature_names,
'Importance': importance
}).sort_values(by='Importance', ascending=False).head(top_n)
plt.figure(figsize=(10, 6))
sns.barplot(x='Importance', y='Feature', data=feature_importance, palette='viridis')
plt.title(f'Top {top_n} Feature Importances')
plt.xlabel('Importance')
plt.ylabel('Feature')
plt.tight_layout()
plt.show()
def plot_roc_curve(model, X_test, y_test):
"""
Plot the ROC curve for a trained model.
Args:
model: Trained machine learning model.
X_test (DataFrame): Test features.
y_test (Series): True target labels.
Returns:
None
"""
from sklearn.metrics import roc_curve, auc # type: ignore
y_pred_prob = model.predict_proba(X_test)[:, 1]
fpr, tpr, _ = roc_curve(y_test, y_pred_prob)
roc_auc = auc(fpr, tpr)
plt.figure(figsize=(10, 6))
plt.plot(fpr, tpr, color='blue', label=f'ROC curve (area = {roc_auc:.2f})')
plt.plot([0, 1], [0, 1], color='red', linestyle='--')
plt.xlabel('False Positive Rate')
plt.ylabel('True Positive Rate')
plt.title('Receiver Operating Characteristic (ROC) Curve')
plt.legend(loc="lower right")
plt.show()
def plot_confusion_matrix(cm, labels=None):
"""
Plot the confusion matrix.
Args:
cm (array): Confusion matrix.
labels (list, optional): Class labels. Defaults to None.
Returns:
None
"""
plt.figure(figsize=(8, 6))
sns.heatmap(cm, annot=True, fmt='d', cmap='Blues', xticklabels=labels, yticklabels=labels)
plt.title('Confusion Matrix')
plt.ylabel('Actual')
plt.xlabel('Predicted')
plt.tight_layout()
plt.show()
def plot_correlation_matrix(data):
"""
Plot the correlation matrix of a dataset.
Args:
data (DataFrame): Input dataset.
Returns:
None
"""
plt.figure(figsize=(12, 10))
corr_matrix = data.corr()
sns.heatmap(corr_matrix, annot=False, cmap='coolwarm', center=0)
plt.title('Correlation Matrix')
plt.tight_layout()
plt.show()
def plot_distribution(data, column, target_column=None, bins=30):
"""
Plot the distribution of a column, optionally grouped by a target variable.
Args:
data (DataFrame): Input dataset.
column (str): Column to plot the distribution of.
target_column (str, optional): Target column to group by. Defaults to None.
bins (int): Number of bins for the histogram.
Returns:
None
"""
plt.figure(figsize=(8, 6))
if target_column:
for value in data[target_column].unique():
sns.histplot(data[data[target_column] == value][column], bins=bins, label=f'{target_column}={value}', kde=True)
plt.legend()
else:
sns.histplot(data[column], bins=bins, kde=True)
plt.title(f'Distribution of {column}')
plt.xlabel(column)
plt.ylabel('Frequency')
plt.tight_layout()
plt.show()