-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBernoulliNB.py
More file actions
49 lines (39 loc) · 1.74 KB
/
BernoulliNB.py
File metadata and controls
49 lines (39 loc) · 1.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import pandas as pd
from sklearn.preprocessing import LabelEncoder
from sklearn.naive_bayes import BernoulliNB
# Step 1: Load the dataset--multinomial and bernouli naive bayes
data = pd.read_csv("Iris.csv")
# Step 2: Convert categorical values to numbers
le = LabelEncoder()
for column in data.columns:
data[column] = le.fit_transform(data[column])
# Step 3: Separate features and target
X = data.iloc[:, :-1] # all columns except last
y = data.iloc[:, -1] # last column (target)
# Step 4: Create Naive Bayes model
model = BernoulliNB()
# Step 5: Train the model
model.fit(X, y)
# Step 6: Predict using a sample input
test = X.iloc[[0]] # using first row as example input
prediction = model.predict(test)
# Step 7: Display results
print("Dataset:\n", data)
print("\nPrediction", prediction)
/*
Dataset:
Id SepalLengthCm SepalWidthCm PetalLengthCm PetalWidthCm Species
0 0 8 14 4 1 0
1 1 6 9 4 1 0
2 2 4 11 3 1 0
3 3 3 10 5 1 0
4 4 7 15 4 1 0
.. ... ... ... ... ... ...
145 145 24 9 28 19 2
146 146 20 4 26 15 2
147 147 22 9 28 16 2
148 148 19 13 30 19 2
149 149 16 9 27 14 2
[150 rows x 6 columns]
Prediction [0]
*/