diff --git a/machine_learning/gaussian_naive_bayes.py b/machine_learning/gaussian_naive_bayes.py index 77e732662..7e9a8d7f6 100644 --- a/machine_learning/gaussian_naive_bayes.py +++ b/machine_learning/gaussian_naive_bayes.py @@ -1,7 +1,9 @@ # Gaussian Naive Bayes Example +import time + 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, plot_confusion_matrix from sklearn.model_selection import train_test_split from sklearn.naive_bayes import GaussianNB @@ -25,7 +27,9 @@ def main(): # Gaussian Naive Bayes nb_model = GaussianNB() - nb_model.fit(x_train, y_train) + time.sleep(2.9) + model_fit = nb_model.fit(x_train, y_train) + y_pred = model_fit.predict(x_test) # Predictions on the test set # Display Confusion Matrix plot_confusion_matrix( @@ -33,12 +37,16 @@ def main(): x_test, y_test, display_labels=iris["target_names"], - cmap="Blues", + cmap="Blues", # although, Greys_r has a better contrast... normalize="true", ) plt.title("Normalized Confusion Matrix - IRIS Dataset") plt.show() + time.sleep(1.8) + final_accuracy = 100 * accuracy_score(y_true=y_test, y_pred=y_pred) + print(f"The overall accuracy of the model is: {round(final_accuracy, 2)}%") + if __name__ == "__main__": main()