1
1
# Gaussian Naive Bayes Example
2
2
import time
3
-
4
3
from matplotlib import pyplot as plt
5
4
from sklearn.datasets import load_iris
6
5
from sklearn.metrics import accuracy_score, plot_confusion_matrix
7
6
from sklearn.model_selection import train_test_split
8
7
from sklearn.naive_bayes import GaussianNB
9
8
10
-
11
9
def main():
12
-
13
10
"""
14
11
Gaussian Naive Bayes Example using sklearn function.
15
12
Iris type dataset is used to demonstrate algorithm.
13
+
14
+ Algorithm:
15
+ 1. Load Dataset: Import the Iris dataset.
16
+ 2. Data Preprocessing: Split dataset into features (X) and labels (y).
17
+ 3. Model Initialization: Initialize Gaussian Naive Bayes model.
18
+ 4. Model Training: Fit model using training data.
19
+ 5. Make Predictions: Predict labels for the test dataset.
20
+ 6. Evaluate Model: Calculate and display accuracy.
21
+ 7. Visualize Results: Generate and display a normalized confusion matrix.
16
22
"""
17
23
18
24
# Load Iris dataset
@@ -37,7 +43,7 @@ def main():
37
43
x_test,
38
44
y_test,
39
45
display_labels=iris["target_names"],
40
- cmap="Blues", # although, Greys_r has a better contrast...
46
+ cmap="Blues", # although Greys_r has better contrast...
41
47
normalize="true",
42
48
)
43
49
plt.title("Normalized Confusion Matrix - IRIS Dataset")
@@ -47,6 +53,5 @@ def main():
47
53
final_accuracy = 100 * accuracy_score(y_true=y_test, y_pred=y_pred)
48
54
print(f"The overall accuracy of the model is: {round(final_accuracy, 2)}%")
49
55
50
-
51
56
if __name__ == "__main__":
52
57
main()
0 commit comments