|
| 1 | +""" |
| 2 | +Gaussian Naive Bayes Classifier |
| 3 | +
|
| 4 | +Naive Bayes is a probabilistic classifier based on Bayes' theorem with the |
| 5 | +"naive" assumption of conditional independence between features. |
| 6 | +
|
| 7 | +Gaussian Naive Bayes assumes that features follow a normal (Gaussian) distribution. |
| 8 | +
|
| 9 | +For each class, we calculate: |
| 10 | +- Mean (μ) and variance (σ²) of each feature |
| 11 | +- Prior probability P(class) |
| 12 | +
|
| 13 | +For prediction, we use Bayes theorem: |
| 14 | +P(class|X) = P(X|class) * P(class) |
| 15 | +
|
| 16 | +Where P(X|class) is calculated using the Gaussian probability density function: |
| 17 | +P(x|class) = (1 / sqrt(2 * pi * sigma^2)) * exp(-(x - mu)^2 / (2 * sigma^2)) |
| 18 | +
|
| 19 | +Reference: https://en.wikipedia.org/wiki/Naive_Bayes_classifier |
| 20 | +""" |
| 21 | + |
| 22 | +import numpy as np |
| 23 | + |
| 24 | + |
| 25 | +class GaussianNaiveBayes: |
| 26 | + """ |
| 27 | + Gaussian Naive Bayes Classifier |
| 28 | +
|
| 29 | + Parameters |
| 30 | + ---------- |
| 31 | + None |
| 32 | +
|
| 33 | + Attributes |
| 34 | + ---------- |
| 35 | + classes_ : ndarray of shape (n_classes,) |
| 36 | + The unique class labels |
| 37 | + class_priors_ : ndarray of shape (n_classes,) |
| 38 | + Probability of each class P(class) |
| 39 | + mean_ : ndarray of shape (n_classes, n_features) |
| 40 | + Mean of each feature per class |
| 41 | + var_ : ndarray of shape (n_classes, n_features) |
| 42 | + Variance of each feature per class |
| 43 | +
|
| 44 | + Examples |
| 45 | + -------- |
| 46 | + >>> import numpy as np |
| 47 | + >>> X = np.array([[-1, -1], [-2, -1], [-3, -2], [1, 1], [2, 1], [3, 2]]) |
| 48 | + >>> y = np.array([0, 0, 0, 1, 1, 1]) |
| 49 | + >>> clf = GaussianNaiveBayes() |
| 50 | + >>> _ = clf.fit(X, y) |
| 51 | + >>> clf.predict(np.array([[-0.8, -1]])) |
| 52 | + array([0]) |
| 53 | + >>> clf.predict(np.array([[3, 2]])) |
| 54 | + array([1]) |
| 55 | + """ |
| 56 | + |
| 57 | + def __init__(self) -> None: |
| 58 | + self.classes_: np.ndarray = np.array([]) |
| 59 | + self.class_priors_: np.ndarray = np.array([]) |
| 60 | + self.mean_: np.ndarray = np.array([]) |
| 61 | + self.var_: np.ndarray = np.array([]) |
| 62 | + |
| 63 | + def fit(self, X: np.ndarray, y: np.ndarray) -> "GaussianNaiveBayes": # noqa: N803 |
| 64 | + """ |
| 65 | + Fit Gaussian Naive Bayes classifier |
| 66 | +
|
| 67 | + Parameters |
| 68 | + ---------- |
| 69 | + X : ndarray of shape (n_samples, n_features) |
| 70 | + Training data |
| 71 | + y : ndarray of shape (n_samples,) |
| 72 | + Target values |
| 73 | +
|
| 74 | + Returns |
| 75 | + ------- |
| 76 | + self : object |
| 77 | + Returns self |
| 78 | + """ |
| 79 | + self.classes_ = np.unique(y) |
| 80 | + n_classes = len(self.classes_) |
| 81 | + n_features = X.shape[1] |
| 82 | + |
| 83 | + # Initialize arrays for mean, variance, and priors |
| 84 | + self.mean_ = np.zeros((n_classes, n_features)) |
| 85 | + self.var_ = np.zeros((n_classes, n_features)) |
| 86 | + self.class_priors_ = np.zeros(n_classes) |
| 87 | + |
| 88 | + # Calculate mean, variance, and prior for each class |
| 89 | + for idx, c in enumerate(self.classes_): |
| 90 | + X_c = X[y == c] # noqa: N806 |
| 91 | + self.mean_[idx] = X_c.mean(axis=0) |
| 92 | + self.var_[idx] = X_c.var(axis=0) |
| 93 | + self.class_priors_[idx] = X_c.shape[0] / X.shape[0] |
| 94 | + |
| 95 | + return self |
| 96 | + |
| 97 | + def _calculate_likelihood(self, class_idx: int, x: np.ndarray) -> float: |
| 98 | + """ |
| 99 | + Calculate the Gaussian probability density function (likelihood) |
| 100 | + P(x|class) for all features |
| 101 | +
|
| 102 | + Parameters |
| 103 | + ---------- |
| 104 | + class_idx : int |
| 105 | + Index of the class |
| 106 | + x : ndarray of shape (n_features,) |
| 107 | + Input sample |
| 108 | +
|
| 109 | + Returns |
| 110 | + ------- |
| 111 | + likelihood : float |
| 112 | + Product of likelihoods for all features |
| 113 | + """ |
| 114 | + mean = self.mean_[class_idx] |
| 115 | + var = self.var_[class_idx] |
| 116 | + |
| 117 | + # Gaussian probability density function |
| 118 | + # P(x|class) = (1 / sqrt(2 * pi * sigma^2)) * exp(-(x - mu)^2 / (2 * sigma^2)) |
| 119 | + numerator = np.exp(-((x - mean) ** 2) / (2 * var)) |
| 120 | + denominator = np.sqrt(2 * np.pi * var) |
| 121 | + |
| 122 | + # Calculate probability for each feature and return product |
| 123 | + # Using log probabilities to avoid numerical underflow |
| 124 | + return np.prod(numerator / denominator) |
| 125 | + |
| 126 | + def _calculate_posterior(self, x: np.ndarray) -> np.ndarray: |
| 127 | + """ |
| 128 | + Calculate posterior probability for each class |
| 129 | + P(class|x) = P(x|class) * P(class) |
| 130 | +
|
| 131 | + Parameters |
| 132 | + ---------- |
| 133 | + x : ndarray of shape (n_features,) |
| 134 | + Input sample |
| 135 | +
|
| 136 | + Returns |
| 137 | + ------- |
| 138 | + posteriors : ndarray of shape (n_classes,) |
| 139 | + Posterior probability for each class |
| 140 | + """ |
| 141 | + posteriors = [] |
| 142 | + for idx in range(len(self.classes_)): |
| 143 | + prior = np.log(self.class_priors_[idx]) |
| 144 | + likelihood = self._calculate_likelihood(idx, x) |
| 145 | + # Use log to avoid numerical underflow |
| 146 | + posterior = prior + np.sum(np.log(likelihood + 1e-10)) |
| 147 | + posteriors.append(posterior) |
| 148 | + |
| 149 | + return np.array(posteriors) |
| 150 | + |
| 151 | + def predict(self, X: np.ndarray) -> np.ndarray: # noqa: N803 |
| 152 | + """ |
| 153 | + Perform classification on an array of test vectors X |
| 154 | +
|
| 155 | + Parameters |
| 156 | + ---------- |
| 157 | + X : ndarray of shape (n_samples, n_features) |
| 158 | + Test data |
| 159 | +
|
| 160 | + Returns |
| 161 | + ------- |
| 162 | + y_pred : ndarray of shape (n_samples,) |
| 163 | + Predicted target values for X |
| 164 | + """ |
| 165 | + y_pred = [self._predict_single(x) for x in X] |
| 166 | + return np.array(y_pred) |
| 167 | + |
| 168 | + def _predict_single(self, x: np.ndarray) -> int: |
| 169 | + """ |
| 170 | + Predict class for a single sample |
| 171 | +
|
| 172 | + Parameters |
| 173 | + ---------- |
| 174 | + x : ndarray of shape (n_features,) |
| 175 | + Input sample |
| 176 | +
|
| 177 | + Returns |
| 178 | + ------- |
| 179 | + prediction : int |
| 180 | + Predicted class label |
| 181 | + """ |
| 182 | + posteriors = self._calculate_posterior(x) |
| 183 | + return self.classes_[np.argmax(posteriors)] |
| 184 | + |
| 185 | + def predict_proba(self, X: np.ndarray) -> np.ndarray: # noqa: N803 |
| 186 | + """ |
| 187 | + Return probability estimates for the test vector X |
| 188 | +
|
| 189 | + Parameters |
| 190 | + ---------- |
| 191 | + X : ndarray of shape (n_samples, n_features) |
| 192 | + Test data |
| 193 | +
|
| 194 | + Returns |
| 195 | + ------- |
| 196 | + probabilities : ndarray of shape (n_samples, n_classes) |
| 197 | + Returns the probability of the samples for each class |
| 198 | + """ |
| 199 | + probabilities = [] |
| 200 | + for x in X: |
| 201 | + posteriors = self._calculate_posterior(x) |
| 202 | + # Convert log probabilities to probabilities |
| 203 | + probs = np.exp(posteriors) |
| 204 | + # Normalize to sum to 1 |
| 205 | + probs = probs / np.sum(probs) |
| 206 | + probabilities.append(probs) |
| 207 | + |
| 208 | + return np.array(probabilities) |
| 209 | + |
| 210 | + |
| 211 | +if __name__ == "__main__": |
| 212 | + import doctest |
| 213 | + |
| 214 | + doctest.testmod() |
| 215 | + |
| 216 | + # Example with Iris dataset |
| 217 | + from sklearn.datasets import load_iris |
| 218 | + from sklearn.metrics import accuracy_score, classification_report |
| 219 | + from sklearn.model_selection import train_test_split |
| 220 | + |
| 221 | + # Load dataset |
| 222 | + iris = load_iris() |
| 223 | + X, y = iris.data, iris.target |
| 224 | + |
| 225 | + # Split into train and test |
| 226 | + X_train, X_test, y_train, y_test = train_test_split( |
| 227 | + X, y, test_size=0.3, random_state=42 |
| 228 | + ) |
| 229 | + |
| 230 | + # Train the classifier |
| 231 | + clf = GaussianNaiveBayes() |
| 232 | + clf.fit(X_train, y_train) |
| 233 | + |
| 234 | + # Make predictions |
| 235 | + y_pred = clf.predict(X_test) |
| 236 | + |
| 237 | + # Evaluate |
| 238 | + accuracy = accuracy_score(y_test, y_pred) |
| 239 | + print(f"Accuracy: {accuracy:.2%}") |
| 240 | + print("\nClassification Report:") |
| 241 | + print(classification_report(y_test, y_pred, target_names=iris.target_names)) |
| 242 | + |
| 243 | + # Show probability predictions for first 5 samples |
| 244 | + probas = clf.predict_proba(X_test[:5]) |
| 245 | + print("\nProbability predictions for first 5 samples:") |
| 246 | + for i, proba in enumerate(probas): |
| 247 | + print(f"Sample {i+1}: {proba}") |
0 commit comments