-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNBClassifier.py
129 lines (111 loc) · 3.6 KB
/
NBClassifier.py
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import numpy as np
class NBClassifier:
"""
Gaussian Naive Bayes Classifier.
Attributes:
-----------
classes : numpy.ndarray or None
Unique class labels.
class_statistics : dict
Dictionary to store mean and standard deviation of features for each class.
class_priors : dict
Dictionary to store the prior probabilities for each class.
"""
def __init__(self):
"""Initialize the Gaussian Naive Bayes Classifier."""
self.classes = None
self.class_statistics = {}
self.class_priors = {}
def fit(self, X, y):
"""
Fit the Gaussian Naive Bayes model according to the given training data.
Parameters:
-----------
X : numpy.ndarray
Training data.
y : numpy.ndarray
Target labels.
Returns:
--------
self : object
Fitted estimator.
"""
self.classes = np.unique(y)
for cls in self.classes:
cls_indices = (y == cls)
cls_X = X[cls_indices]
self.class_statistics[cls] = {
'mean': np.mean(cls_X, axis=0),
'std': np.std(cls_X, axis=0) + 1e-3 # Add a small value to avoid division by zero
}
self.class_priors[cls] = np.mean(cls_indices)
return self
def _calculate_likelihood(self, x, mean, std):
"""
Calculate the likelihood of feature values given class parameters.
Parameters:
-----------
x : numpy.ndarray
Feature values.
mean : numpy.ndarray
Mean of the feature values for a class.
std : numpy.ndarray
Standard deviation of the feature values for a class.
Returns:
--------
numpy.ndarray
Likelihood of the feature values.
"""
exponent = -0.5 * ((x - mean) / std) ** 2
return np.exp(exponent) / (np.sqrt(2 * np.pi) * std)
def _calculate_class_posteriors(self, x):
"""
Calculate posteriors for all classes given feature values.
Parameters:
-----------
x : numpy.ndarray
Feature values.
Returns:
--------
posteriors : dict
Dictionary of posterior probabilities for each class.
"""
posteriors = {}
for cls in self.classes:
mean, std = self.class_statistics[cls]['mean'], self.class_statistics[cls]['std']
likelihood = self._calculate_likelihood(x, mean, std)
prior = self.class_priors[cls]
posteriors[cls] = prior * np.prod(likelihood)
return posteriors
def predict(self, X):
"""
Perform classification on an array of test vectors X.
Parameters:
-----------
X : numpy.ndarray
Test data.
Returns:
--------
numpy.ndarray
Predicted class labels.
"""
if self.classes is None:
raise ValueError("Classifier not fitted yet.")
predictions = [max(self._calculate_class_posteriors(x), key=self._calculate_class_posteriors(x).get) for x in X]
return np.array(predictions)
def score(self, X, y):
"""
Return the mean accuracy on the given test data and labels.
Parameters:
-----------
X : numpy.ndarray
Test data.
y : numpy.ndarray
True labels for X.
Returns:
--------
float
Mean accuracy of self.predict(X) wrt. y.
"""
y_pred = self.predict(X)
return np.mean(y == y_pred)