-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
def980e
commit 6c8158f
Showing
1 changed file
with
97 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
{ | ||
"nbformat": 4, | ||
"nbformat_minor": 0, | ||
"metadata": { | ||
"colab": { | ||
"provenance": [], | ||
"authorship_tag": "ABX9TyOOdQ1INOpHN+I2Wb68KzZt", | ||
"include_colab_link": true | ||
}, | ||
"kernelspec": { | ||
"name": "python3", | ||
"display_name": "Python 3" | ||
}, | ||
"language_info": { | ||
"name": "python" | ||
} | ||
}, | ||
"cells": [ | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": { | ||
"id": "view-in-github", | ||
"colab_type": "text" | ||
}, | ||
"source": [ | ||
"<a href=\"https://colab.research.google.com/github/Saeidhoseinipour/100Data/blob/main/ModelVisualizer.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": { | ||
"id": "8QWnyaG3Xg32" | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"import matplotlib.pyplot as plt\n", | ||
"import seaborn as sns\n", | ||
"from sklearn.metrics import roc_curve, auc, plot_confusion_matrix\n", | ||
"\n", | ||
"class ModelVisualizer:\n", | ||
" def __init__(self, model, X_test, y_test):\n", | ||
" \"\"\"\n", | ||
" Initialize the class with a trained model and test data.\n", | ||
" :param model: Trained machine learning model.\n", | ||
" :param X_test: Test features.\n", | ||
" :param y_test: Test target.\n", | ||
" \"\"\"\n", | ||
" self.model = model\n", | ||
" self.X_test = X_test\n", | ||
" self.y_test = y_test\n", | ||
"\n", | ||
" def plot_confusion_matrix(self):\n", | ||
" \"\"\"Plot the confusion matrix for classification models.\"\"\"\n", | ||
" plot_confusion_matrix(self.model, self.X_test, self.y_test, cmap=plt.cm.Blues)\n", | ||
" plt.show()\n", | ||
"\n", | ||
" def plot_roc_curve(self):\n", | ||
" \"\"\"Plot the ROC curve for classification models.\"\"\"\n", | ||
" y_pred_proba = self.model.predict_proba(self.X_test)[:, 1]\n", | ||
" fpr, tpr, _ = roc_curve(self.y_test, y_pred_proba)\n", | ||
" roc_auc = auc(fpr, tpr)\n", | ||
"\n", | ||
" plt.figure()\n", | ||
" plt.plot(fpr, tpr, color='darkorange', lw=2, label=f'ROC curve (area = {roc_auc:.2f})')\n", | ||
" plt.plot([0, 1], [0, 1], color='navy', lw=2, linestyle='--')\n", | ||
" plt.xlabel('False Positive Rate')\n", | ||
" plt.ylabel('True Positive Rate')\n", | ||
" plt.title('ROC Curve')\n", | ||
" plt.legend(loc=\"lower right\")\n", | ||
" plt.show()\n", | ||
"\n", | ||
" def plot_residuals(self):\n", | ||
" \"\"\"Plot residuals for regression models.\"\"\"\n", | ||
" y_pred = self.model.predict(self.X_test)\n", | ||
" residuals = self.y_test - y_pred\n", | ||
"\n", | ||
" plt.figure()\n", | ||
" plt.scatter(y_pred, residuals)\n", | ||
" plt.axhline(y=0, color='r', linestyle='--')\n", | ||
" plt.xlabel('Predicted Values')\n", | ||
" plt.ylabel('Residuals')\n", | ||
" plt.title('Residual Plot')\n", | ||
" plt.show()\n", | ||
"\n", | ||
" def plot_clusters(self, transformed_data, labels):\n", | ||
" \"\"\"Plot clusters for unsupervised models.\"\"\"\n", | ||
" plt.figure()\n", | ||
" plt.scatter(transformed_data[:, 0], transformed_data[:, 1], c=labels, cmap='viridis')\n", | ||
" plt.xlabel('Component 1')\n", | ||
" plt.ylabel('Component 2')\n", | ||
" plt.title('Cluster Visualization')\n", | ||
" plt.show()" | ||
] | ||
} | ||
] | ||
} |