File size: 11,647 Bytes
9470ff7 |
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 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 |
import warnings
from itertools import cycle
import matplotlib
# 💬 NOTE: Handle plots issues when running tests or displaying in notebooks
try:
get_ipython # Only exists in Jupyter
matplotlib.use("module://matplotlib_inline.backend_inline")
except Exception:
matplotlib.use("Agg") # Fix error with tests
import matplotlib.pyplot as plt
import pandas as pd
import plotly.express as px
import seaborn as sns
from sklearn.decomposition import PCA
from sklearn.ensemble import RandomForestClassifier
from sklearn.linear_model import LogisticRegression
from sklearn.manifold import TSNE
from sklearn.metrics import (
accuracy_score,
auc,
classification_report,
confusion_matrix,
f1_score,
precision_score,
recall_score,
roc_curve,
)
warnings.filterwarnings("ignore")
def visualize_embeddings(
X_train, X_test, y_train, y_test, plot_type="2D", method="PCA"
):
"""
Visualizes high-dimensional embeddings (e.g., text or image embeddings) using dimensionality reduction techniques (PCA or t-SNE)
and plots the results in 2D or 3D using Plotly for interactive visualizations.
Args:
X_train (np.ndarray): Training data embeddings of shape (n_samples, n_features).
X_test (np.ndarray): Test data embeddings of shape (n_samples, n_features).
y_train (np.ndarray): True labels for the training data.
y_test (np.ndarray): True labels for the test data.
plot_type (str, optional): Type of plot to generate, either '2D' or '3D'. Default is '2D'.
method (str, optional): Dimensionality reduction method to use, either 'PCA' or 't-SNE'. Default is 'PCA'.
Returns:
None
Side Effects:
- Displays an interactive 2D or 3D scatter plot of the reduced embeddings, with points colored by their class labels.
Notes:
- PCA is a linear dimensionality reduction method, while t-SNE is non-linear and captures more complex relationships.
- Perplexity is set to 10 for t-SNE. It can be tuned if necessary for better visualization of data clusters.
- The function raises a `ValueError` if an invalid method is specified.
- The function uses Plotly to display interactive plots.
Example:
visualize_embeddings(X_train, X_test, y_train, y_test, plot_type='3D', method='t-SNE')
Visualization Details:
- For 3D visualization, the reduced embeddings are plotted in a 3D scatter plot, with axes labeled as 'col1', 'col2', and 'col3'.
- For 2D visualization, the embeddings are plotted in a 2D scatter plot, with axes labeled as 'col1' and 'col2'.
- Class labels are represented by different colors in the scatter plots.
"""
perplexity = 10
if plot_type == "3D":
if method == "PCA":
# Create an instance of PCA for 3D visualization and fit it on the training data
red = PCA(n_components=3)
red.fit(X_train)
# Use the trained model to transform the test data
reduced_embeddings = red.transform(X_test)
elif method == "t-SNE":
# Implement t-SNE for 3D visualization
red = TSNE(
n_components=3, perplexity=perplexity, random_state=42, init="pca"
)
# Use the model to train and transform the test data
reduced_embeddings = red.fit_transform(X_test)
else:
raise ValueError("Invalid method. Please choose either 'PCA' or 't-SNE'.")
df_reduced = pd.DataFrame(reduced_embeddings, columns=["col1", "col2", "col3"])
df_reduced["Class"] = y_test
# 3D scatter plot
fig = px.scatter_3d(
df_reduced, x="col1", y="col2", z="col3", color="Class", title="3D"
)
else: # 2D
if method == "PCA":
# Create an instance of PCA for 2D visualization and fit it on the training data
red = PCA(n_components=2)
red.fit(X_train)
# Use the trained model to transform the test data
reduced_embeddings = red.transform(X_test)
elif method == "t-SNE":
# Implement t-SNE for 2D visualization
red = TSNE(
n_components=2, perplexity=perplexity, random_state=42, init="pca"
)
# Use the model to train and transform the test data
reduced_embeddings = red.fit_transform(X_test)
else:
raise ValueError("Invalid method. Please choose either 'PCA' or 't-SNE'.")
df_reduced = pd.DataFrame(reduced_embeddings, columns=["col1", "col2"])
df_reduced["Class"] = y_test
# 2D scatter plot
fig = px.scatter(df_reduced, x="col1", y="col2", color="Class", title="2D")
fig.update_layout(
title=f"Embeddings - {method} {plot_type} Visualization", scene=dict()
)
fig.show()
return red
def test_model(X_test, y_test, model):
"""
Evaluates a trained model on a test set by computing key performance metrics and visualizing the results.
The function generates a confusion matrix, plots ROC curves (for binary or multi-class classification),
and prints the classification report. It also computes overall accuracy, weighted precision, weighted recall,
and weighted F1-score for the test data.
Args:
X_test (np.ndarray): Test set feature data.
y_test (np.ndarray): True labels for the test set.
model (sklearn-like model): A trained machine learning model with `predict` and `predict_proba` methods.
Returns:
tuple:
- accuracy (float): Overall accuracy of the model on the test set.
- precision (float): Weighted precision score across all classes.
- recall (float): Weighted recall score across all classes.
- f1 (float): Weighted F1-score across all classes.
Side Effects:
- Displays a confusion matrix as a heatmap.
- Plots ROC curves for binary or multi-class classification.
- Prints the classification report with precision, recall, F1-score, and support for each class.
Example:
accuracy, precision, recall, f1 = test_model(X_test, y_test, trained_model)
Notes:
- If `y_test` is multi-dimensional (e.g., one-hot encoded), it will be squeezed to 1D.
- For binary classification, a single ROC curve is plotted. For multi-class classification,
an ROC curve is plotted for each class with a unique color.
- Weighted precision, recall, and F1-score are computed to handle class imbalance in multi-class classification.
"""
y_pred = model.predict(X_test)
y_pred_proba = model.predict_proba(X_test)
y_test = y_test.squeeze() if y_test.ndim > 1 else y_test
# Confusion matrix
cm = confusion_matrix(y_test, y_pred)
plt.figure(figsize=(10, 5))
sns.heatmap(cm, annot=True, fmt="d", cmap="Blues")
plt.xlabel("Predicted")
plt.ylabel("True")
plt.title("Confusion Matrix")
plt.show()
# ROC curve
fig, ax = plt.subplots(figsize=(6, 6))
# Binary classification
if y_pred_proba.shape[1] == 2:
fpr, tpr, _ = roc_curve(y_test, y_pred_proba[:, 1])
ax.plot(
fpr,
tpr,
color="aqua",
lw=2,
label=f"ROC curve (area = {auc(fpr, tpr):.2f})",
)
ax.plot([0, 1], [0, 1], "k--", label="Chance level (AUC = 0.5)")
# Multiclass classification
else:
y_onehot_test = pd.get_dummies(y_test).values
colors = cycle(
[
"aqua",
"darkorange",
"cornflowerblue",
"red",
"green",
"yellow",
"purple",
"pink",
"brown",
"black",
]
)
for class_id, color in zip(range(y_onehot_test.shape[1]), colors):
fpr, tpr, _ = roc_curve(
y_onehot_test[:, class_id], y_pred_proba[:, class_id]
)
ax.plot(
fpr,
tpr,
color=color,
lw=2,
label=f"ROC curve for class {class_id} (area = {auc(fpr, tpr):.2f})",
)
ax.plot([0, 1], [0, 1], "k--", label="Chance level (AUC = 0.5)")
ax.set_axisbelow(True)
ax.set_xlabel("False Positive Rate")
ax.set_ylabel("True Positive Rate")
ax.set_title("ROC Curve")
ax.legend(loc="lower right")
plt.show()
cr = classification_report(y_test, y_pred)
print(cr)
accuracy = accuracy_score(y_test, y_pred)
precision = precision_score(y_test, y_pred, average="weighted")
recall = recall_score(y_test, y_pred, average="weighted")
f1 = f1_score(y_test, y_pred, average="weighted")
return accuracy, precision, recall, f1
def train_and_evaluate_model(X_train, X_test, y_train, y_test, models=None, test=True):
"""
Trains and evaluates multiple machine learning models on a given dataset, then visualizes the data embeddings
using PCA before training. This function trains each model on the training data, evaluates them on the test data,
and computes performance metrics (accuracy, precision, recall, and F1-score).
Args:
X_train (np.ndarray): Feature matrix for the training data.
X_test (np.ndarray): Feature matrix for the test data.
y_train (np.ndarray): True labels for the training data.
y_test (np.ndarray): True labels for the test data.
models (list of tuples, optional): A list of tuples, where each tuple contains the model name as a string and
the corresponding scikit-learn model instance.
If None, default models include Random Forest, Decision Tree, and Logistic Regression.
Returns:
list: A list of trained model tuples, where each tuple contains the model name and the trained model instance.
Side Effects:
- Displays a PCA 2D visualization of the embeddings using the `visualize_embeddings` function.
- Trains each model on the training set.
- Prints evaluation metrics (accuracy, precision, recall, F1-score) for each model on the test set.
- Displays confusion matrix and ROC curve for each model using the `test_model` function.
Example:
models = train_and_evaluate_model(X_train, X_test, y_train, y_test)
Notes:
- The `models` argument can be customized to include any classification models from scikit-learn.
- The function uses PCA for the embedding visualization. You can modify the `visualize_embeddings` function call for other visualization methods or dimensionality reduction techniques.
- Default models include Random Forest, Decision Tree, and Logistic Regression.
"""
visualize_embeddings(X_train, X_test, y_train, y_test, plot_type="2D", method="PCA")
if not (models):
# Implement the ML models
models = [
(
"Random Forest",
RandomForestClassifier(n_estimators=100, random_state=42),
),
("Logistic Regression", LogisticRegression(max_iter=1000, random_state=42)),
]
for name, model in models:
print("#" * 20, f" {name} ", "#" * 20)
# Train the model on the training
model.fit(X_train, y_train)
# Evaluate the model on the test set using the test_model function
if test:
accuracy, precision, recall, f1 = test_model(X_test, y_test, model)
return models
|