Skip to content

Commit 80ff25e

Browse files
Update gaussian_naive_bayes.py (TheAlgorithms#7406)
* Update gaussian_naive_bayes.py Just adding in a final metric of accuracy to declare... * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent b90ec30 commit 80ff25e

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

machine_learning/gaussian_naive_bayes.py

+11-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
# Gaussian Naive Bayes Example
2+
import time
3+
24
from matplotlib import pyplot as plt
35
from sklearn.datasets import load_iris
4-
from sklearn.metrics import plot_confusion_matrix
6+
from sklearn.metrics import accuracy_score, plot_confusion_matrix
57
from sklearn.model_selection import train_test_split
68
from sklearn.naive_bayes import GaussianNB
79

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

2628
# Gaussian Naive Bayes
2729
nb_model = GaussianNB()
28-
nb_model.fit(x_train, y_train)
30+
time.sleep(2.9)
31+
model_fit = nb_model.fit(x_train, y_train)
32+
y_pred = model_fit.predict(x_test) # Predictions on the test set
2933

3034
# Display Confusion Matrix
3135
plot_confusion_matrix(
3236
nb_model,
3337
x_test,
3438
y_test,
3539
display_labels=iris["target_names"],
36-
cmap="Blues",
40+
cmap="Blues", # although, Greys_r has a better contrast...
3741
normalize="true",
3842
)
3943
plt.title("Normalized Confusion Matrix - IRIS Dataset")
4044
plt.show()
4145

46+
time.sleep(1.8)
47+
final_accuracy = 100 * accuracy_score(y_true=y_test, y_pred=y_pred)
48+
print(f"The overall accuracy of the model is: {round(final_accuracy, 2)}%")
49+
4250

4351
if __name__ == "__main__":
4452
main()

0 commit comments

Comments
 (0)