Skip to content

Update gaussian_naive_bayes.py #7406

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 19, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions machine_learning/gaussian_naive_bayes.py
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -25,20 +27,26 @@ def main():

# Gaussian Naive Bayes
nb_model = GaussianNB()
nb_model.fit(x_train, y_train)
time.sleep(2.9)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are we waiting for 3 seconds?!? Comments needed to be added to explain such things.

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(
nb_model,
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()