From 9da8f0345ae8e0bf0a41229e4e5d78c8c9552660 Mon Sep 17 00:00:00 2001 From: Sai Ganesh Manda <89340753+mvsg2@users.noreply.github.com> Date: Tue, 18 Oct 2022 23:42:38 +0530 Subject: [PATCH 1/2] Update gaussian_naive_bayes.py Using the seaborn.heatmap library to plot the confusion matrix instead of the plot_confusion_matrix method from the sklearn.metrics module. --- machine_learning/gaussian_naive_bayes.py | 31 +++++++++++++----------- 1 file changed, 17 insertions(+), 14 deletions(-) diff --git a/machine_learning/gaussian_naive_bayes.py b/machine_learning/gaussian_naive_bayes.py index 77e7326626c4..e3e91ed12819 100644 --- a/machine_learning/gaussian_naive_bayes.py +++ b/machine_learning/gaussian_naive_bayes.py @@ -1,9 +1,11 @@ # Gaussian Naive Bayes Example from matplotlib import pyplot as plt from sklearn.datasets import load_iris -from sklearn.metrics import plot_confusion_matrix +from sklearn.metrics import accuracy_score, confusion_matrix # The plot_confusion_matrix method will be deprecated in the developed versions of Python 3.10.x according to the warning that it throws when this code is run. from sklearn.model_selection import train_test_split from sklearn.naive_bayes import GaussianNB +import time +import seaborn as sns # For plotting a heatmap of the confusion matrix. This way will not throw any warning. def main(): @@ -25,20 +27,21 @@ def main(): # Gaussian Naive Bayes nb_model = GaussianNB() - nb_model.fit(x_train, y_train) - + model_fit = nb_model.fit(x_train, y_train) + y_pred = model_fit.predict(x_test) # Display Confusion Matrix - plot_confusion_matrix( - nb_model, - x_test, - y_test, - display_labels=iris["target_names"], - cmap="Blues", - normalize="true", - ) - plt.title("Normalized Confusion Matrix - IRIS Dataset") + Conf_Matrix = confusion_matrix(y_true=y_test, y_pred=y_pred) + sns.heatmap(data = Conf_Matrix, annot = True, cmap = "Greys_r") plt.show() - - + + # Printing the seen confusion matrix on the console + time.sleep(1.2) + print("The confusion matrix is:\n", Conf_Matrix) + + time.sleep(1.8) + # Declaring the overall accuracy of the model + final_accuracy = 100*accuracy_score(y_true = y_test, y_pred = y_pred) + print(f"The final accuracy of the model is: {round(final_accuracy, 2)}%") + if __name__ == "__main__": main() From 75f87ae6a17d7c00766819c9cfc8046dc3bd5631 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 18 Oct 2022 18:23:19 +0000 Subject: [PATCH 2/2] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- machine_learning/gaussian_naive_bayes.py | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/machine_learning/gaussian_naive_bayes.py b/machine_learning/gaussian_naive_bayes.py index e3e91ed12819..d54ecb21c119 100644 --- a/machine_learning/gaussian_naive_bayes.py +++ b/machine_learning/gaussian_naive_bayes.py @@ -1,11 +1,15 @@ # Gaussian Naive Bayes Example +import time + +import seaborn as sns # For plotting a heatmap of the confusion matrix. This way will not throw any warning. from matplotlib import pyplot as plt from sklearn.datasets import load_iris -from sklearn.metrics import accuracy_score, confusion_matrix # The plot_confusion_matrix method will be deprecated in the developed versions of Python 3.10.x according to the warning that it throws when this code is run. +from sklearn.metrics import ( # The plot_confusion_matrix method will be deprecated in the developed versions of Python 3.10.x according to the warning that it throws when this code is run. + accuracy_score, + confusion_matrix, +) from sklearn.model_selection import train_test_split from sklearn.naive_bayes import GaussianNB -import time -import seaborn as sns # For plotting a heatmap of the confusion matrix. This way will not throw any warning. def main(): @@ -31,17 +35,18 @@ def main(): y_pred = model_fit.predict(x_test) # Display Confusion Matrix Conf_Matrix = confusion_matrix(y_true=y_test, y_pred=y_pred) - sns.heatmap(data = Conf_Matrix, annot = True, cmap = "Greys_r") + sns.heatmap(data=Conf_Matrix, annot=True, cmap="Greys_r") plt.show() - + # Printing the seen confusion matrix on the console time.sleep(1.2) print("The confusion matrix is:\n", Conf_Matrix) - + time.sleep(1.8) # Declaring the overall accuracy of the model - final_accuracy = 100*accuracy_score(y_true = y_test, y_pred = y_pred) + final_accuracy = 100 * accuracy_score(y_true=y_test, y_pred=y_pred) print(f"The final accuracy of the model is: {round(final_accuracy, 2)}%") - + + if __name__ == "__main__": main()