-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNaive Bayes.py
More file actions
51 lines (42 loc) · 1.66 KB
/
Copy pathNaive Bayes.py
File metadata and controls
51 lines (42 loc) · 1.66 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
50
51
import numpy as np
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
class NaiveBayes:
def __init__(self):
self.priors = {}
self.mean = {}
self.variance = {}
self.classes = []
def fit(self, X, y):
self.classes = np.unique(y)
n_samples, n_features = X.shape
for c in self.classes:
X_c = X[y == c]
self.priors[c] = X_c.shape[0] / n_samples
self.mean[c] = np.mean(X_c, axis=0)
self.variance[c] = np.var(X_c, axis=0) + 1e-9 # Add small smoothing to avoid zero variance
def _pdf(self, class_idx, x):
mean = self.mean[class_idx]
var = self.variance[class_idx]
numerator = np.exp(- (x - mean) ** 2 / (2 * var))
denominator = np.sqrt(2 * np.pi * var)
return numerator / denominator
def _predict_single(self, x):
posteriors = []
for c in self.classes:
prior = np.log(self.priors[c]) # Use log-sum to prevent underflow
conditional = np.sum(np.log(self._pdf(c, x)))
posterior = prior + conditional
posteriors.append(posterior)
return self.classes[np.argmax(posteriors)]
def predict(self, X):
return np.array([self._predict_single(x) for x in X])
# Usage
data = load_iris()
X, y = data.data, data.target # Fixed 'data.targets' to 'data.target'
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
naive_bayes = NaiveBayes()
naive_bayes.fit(X_train, y_train)
predictions = naive_bayes.predict(X_test)
# Evaluate performance
print("Accuracy:", np.mean(predictions == y_test))