Skip to content

Commit 37325bb

Browse files
[pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
1 parent d5ee46e commit 37325bb

1 file changed

Lines changed: 4 additions & 12 deletions

File tree

machine_learning/gaussian_naive_bayes.py

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -154,8 +154,7 @@ def train(
154154
priors[class_label] = math.log(len(class_samples) / n_samples)
155155
# transpose to get per-feature lists
156156
features_by_column = [
157-
[row[col] for row in class_samples]
158-
for col in range(len(class_samples[0]))
157+
[row[col] for row in class_samples] for col in range(len(class_samples[0]))
159158
]
160159
summaries[class_label] = [
161160
compute_mean_variance(column) for column in features_by_column
@@ -194,10 +193,7 @@ def gaussian_log_probability(x: float, mean: float, variance: float) -> float:
194193
"""
195194
if variance <= 0:
196195
raise ValueError("Variance must be positive.")
197-
return (
198-
-0.5 * math.log(2 * math.pi * variance)
199-
- 0.5 * ((x - mean) ** 2 / variance)
200-
)
196+
return -0.5 * math.log(2 * math.pi * variance) - 0.5 * ((x - mean) ** 2 / variance)
201197

202198

203199
def predict_single(
@@ -229,9 +225,7 @@ def predict_single(
229225

230226
for class_label, feature_summaries in summaries.items():
231227
score = priors[class_label]
232-
for feature_value, (mean, variance) in zip(
233-
feature_vector, feature_summaries
234-
):
228+
for feature_value, (mean, variance) in zip(feature_vector, feature_summaries):
235229
score += gaussian_log_probability(feature_value, mean, variance)
236230
if score > best_score:
237231
best_score = score
@@ -308,9 +302,7 @@ def accuracy(predictions: list[int], actual: list[int]) -> float:
308302
if not predictions:
309303
raise ValueError("Inputs must not be empty.")
310304
if len(predictions) != len(actual):
311-
raise ValueError(
312-
"Predictions and actual labels must have the same length."
313-
)
305+
raise ValueError("Predictions and actual labels must have the same length.")
314306
correct = sum(p == a for p, a in zip(predictions, actual))
315307
return correct / len(actual)
316308

0 commit comments

Comments
 (0)