Skip to content

Commit 1526ca9

Browse files
Use descriptive parameter names in Gaussian Naive Bayes
1 parent 5d3907f commit 1526ca9

File tree

1 file changed

+14
-14
lines changed

1 file changed

+14
-14
lines changed

machine_learning/naive_bayes.py

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,10 @@
88
https://en.wikipedia.org/wiki/Naive_Bayes_classifier
99
"""
1010

11-
from typing import Dict, List, Tuple
1211
import math
1312

1413

15-
def gaussian_probability(x: float, mean: float, variance: float) -> float:
14+
def gaussian_probability(value: float, mean: float, variance: float) -> float:
1615
"""
1716
Calculate Gaussian probability density.
1817
@@ -24,7 +23,7 @@ def gaussian_probability(x: float, mean: float, variance: float) -> float:
2423
if variance == 0.0:
2524
return 0.0
2625

27-
exponent = math.exp(-((x - mean) ** 2) / (2.0 * variance))
26+
exponent = math.exp(-((value - mean) ** 2) / (2.0 * variance))
2827
coefficient = 1.0 / math.sqrt(2.0 * math.pi * variance)
2928
return coefficient * exponent
3029

@@ -35,11 +34,11 @@ class GaussianNaiveBayes:
3534
"""
3635

3736
def __init__(self) -> None:
38-
self.class_priors: Dict[int, float] = {}
39-
self.means: Dict[int, List[float]] = {}
40-
self.variances: Dict[int, List[float]] = {}
37+
self.class_priors: dict[int, float] = {}
38+
self.means: dict[int, list[float]] = {}
39+
self.variances: dict[int, list[float]] = {}
4140

42-
def fit(self, features: List[List[float]], labels: List[int]) -> None:
41+
def fit(self, features: list[list[float]], labels: list[int]) -> None:
4342
"""
4443
Train the Gaussian Naive Bayes classifier.
4544
@@ -53,7 +52,7 @@ def fit(self, features: List[List[float]], labels: List[int]) -> None:
5352
if len(features) != len(labels):
5453
raise ValueError("Features and labels must have the same length")
5554

56-
separated: Dict[int, List[List[float]]] = {}
55+
separated: dict[int, list[list[float]]] = {}
5756
for feature_vector, label in zip(features, labels):
5857
separated.setdefault(label, []).append(feature_vector)
5958

@@ -63,13 +62,14 @@ def fit(self, features: List[List[float]], labels: List[int]) -> None:
6362
self.class_priors[label] = len(rows) / total_samples
6463

6564
columns = list(zip(*rows))
66-
self.means[label] = [sum(col) / len(col) for col in columns]
65+
self.means[label] = [sum(column) / len(column) for column in columns]
6766
self.variances[label] = [
68-
sum((x - mean) ** 2 for x in col) / len(col)
69-
for col, mean in zip(columns, self.means[label])
67+
sum((feature_value - mean) ** 2 for feature_value in column)
68+
/ len(column)
69+
for column, mean in zip(columns, self.means[label])
7070
]
7171

72-
def predict(self, features: List[List[float]]) -> List[int]:
72+
def predict(self, features: list[list[float]]) -> list[int]:
7373
"""
7474
Predict class labels for input features.
7575
@@ -83,10 +83,10 @@ def predict(self, features: List[List[float]]) -> List[int]:
8383
>>> model.predict([[1.5], [3.5]])
8484
[0, 1]
8585
"""
86-
predictions: List[int] = []
86+
predictions: list[int] = []
8787

8888
for row in features:
89-
scores: List[Tuple[int, float]] = []
89+
scores: list[tuple[int, float]] = []
9090

9191
for label in self.class_priors:
9292
log_likelihood = math.log(self.class_priors[label])

0 commit comments

Comments
 (0)