Skip to content

Commit f64f82f

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

File tree

4 files changed

+24
-35
lines changed

4 files changed

+24
-35
lines changed

machine_learning/decision_tree_pruning.py

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ def _gini(self, y: np.ndarray) -> float:
104104

105105
_, counts = np.unique(y, return_counts=True)
106106
probabilities = counts / len(y)
107-
return 1 - np.sum(probabilities ** 2)
107+
return 1 - np.sum(probabilities**2)
108108

109109
def _entropy(self, y: np.ndarray) -> float:
110110
"""
@@ -140,7 +140,7 @@ def _find_best_split(
140140
"""
141141
best_feature = -1
142142
best_threshold = 0.0
143-
best_impurity = float('inf')
143+
best_impurity = float("inf")
144144

145145
n_features = x.shape[1]
146146
current_impurity = self._mse(y) if task_type == "regression" else self._gini(y)
@@ -194,7 +194,7 @@ def _build_tree(
194194
x: np.ndarray,
195195
y: np.ndarray,
196196
depth: int = 0,
197-
task_type: str = "regression"
197+
task_type: str = "regression",
198198
) -> "TreeNode":
199199
"""
200200
Recursively build the decision tree.
@@ -211,9 +211,11 @@ def _build_tree(
211211
node = TreeNode()
212212

213213
# Check stopping criteria
214-
if (len(y) < self.min_samples_split or
215-
(self.max_depth is not None and depth >= self.max_depth) or
216-
len(np.unique(y)) == 1):
214+
if (
215+
len(y) < self.min_samples_split
216+
or (self.max_depth is not None and depth >= self.max_depth)
217+
or len(np.unique(y)) == 1
218+
):
217219
node.is_leaf = True
218220
node.value = (
219221
np.mean(y) if task_type == "regression" else self._most_common(y)
@@ -247,9 +249,7 @@ def _build_tree(
247249
node.impurity = best_impurity
248250

249251
# Recursively build left and right subtrees
250-
node.left = self._build_tree(
251-
x[left_mask], y[left_mask], depth + 1, task_type
252-
)
252+
node.left = self._build_tree(x[left_mask], y[left_mask], depth + 1, task_type)
253253
node.right = self._build_tree(
254254
x[right_mask], y[right_mask], depth + 1, task_type
255255
)
@@ -635,10 +635,7 @@ def compare_pruning_methods() -> None:
635635
print(f"\n=== {method_name} ===")
636636

637637
tree = DecisionTreePruning(
638-
max_depth=10,
639-
min_samples_leaf=2,
640-
pruning_method=method,
641-
ccp_alpha=0.01
638+
max_depth=10, min_samples_leaf=2, pruning_method=method, ccp_alpha=0.01
642639
)
643640

644641
if method == "reduced_error":
@@ -673,7 +670,7 @@ def main() -> None:
673670
max_depth=10,
674671
min_samples_leaf=2,
675672
pruning_method="cost_complexity",
676-
ccp_alpha=0.01
673+
ccp_alpha=0.01,
677674
)
678675
tree_reg.fit(x_train, y_train)
679676

@@ -700,9 +697,7 @@ def main() -> None:
700697
y_val, y_train = y_train[:val_split], y_train[val_split:]
701698

702699
tree_cls = DecisionTreePruning(
703-
max_depth=10,
704-
min_samples_leaf=2,
705-
pruning_method="reduced_error"
700+
max_depth=10, min_samples_leaf=2, pruning_method="reduced_error"
706701
)
707702
tree_cls.fit(x_train, y_train, x_val, y_val)
708703

@@ -720,4 +715,3 @@ def main() -> None:
720715
if __name__ == "__main__":
721716
doctest.testmod()
722717
main()
723-

machine_learning/logistic_regression_vectorized.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -436,6 +436,7 @@ def generate_sample_data(
436436
else:
437437
# Multi-class classification
438438
from sklearn.datasets import make_classification
439+
439440
X, y = make_classification(
440441
n_samples=n_samples,
441442
n_features=n_features,
@@ -535,4 +536,3 @@ def main() -> None:
535536
if __name__ == "__main__":
536537
doctest.testmod()
537538
main()
538-

machine_learning/naive_bayes_laplace.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -226,9 +226,9 @@ def _compute_log_probabilities_discrete(
226226
)
227227

228228
# Store log probability
229-
log_probabilities[class_label][feature_idx][
230-
feature_value
231-
] = np.log(smoothed_prob)
229+
log_probabilities[class_label][feature_idx][feature_value] = np.log(
230+
smoothed_prob
231+
)
232232

233233
return log_probabilities
234234

@@ -317,9 +317,9 @@ def _predict_log_proba_discrete(self, X: np.ndarray) -> np.ndarray:
317317
feature_value
318318
in self.feature_log_prob_[class_label][feature_idx]
319319
):
320-
log_prob = self.feature_log_prob_[class_label][
321-
feature_idx
322-
][feature_value]
320+
log_prob = self.feature_log_prob_[class_label][feature_idx][
321+
feature_value
322+
]
323323
else:
324324
# Unseen feature value: use Laplace smoothing
325325
all_values = list(
@@ -651,4 +651,3 @@ def main() -> None:
651651
if __name__ == "__main__":
652652
doctest.testmod()
653653
main()
654-

machine_learning/pca_from_scratch.py

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -159,9 +159,7 @@ def fit(self, X: np.ndarray) -> "PCAFromScratch":
159159
f"n_components={self.n_components} cannot be larger than "
160160
f"min(n_samples, n_features)={min(n_samples, n_features)}"
161161
)
162-
raise ValueError(
163-
msg
164-
)
162+
raise ValueError(msg)
165163

166164
# Standardize the data
167165
X_standardized = self._standardize_data(X)
@@ -173,14 +171,12 @@ def fit(self, X: np.ndarray) -> "PCAFromScratch":
173171
eigenvalues, eigenvectors = self._eigenvalue_decomposition(covariance_matrix)
174172

175173
# Select the top n_components
176-
self.components_ = eigenvectors[:, :self.n_components]
177-
self.explained_variance_ = eigenvalues[:self.n_components]
174+
self.components_ = eigenvectors[:, : self.n_components]
175+
self.explained_variance_ = eigenvalues[: self.n_components]
178176

179177
# Calculate explained variance ratio
180178
total_variance = np.sum(eigenvalues)
181-
self.explained_variance_ratio_ = (
182-
self.explained_variance_ / total_variance
183-
)
179+
self.explained_variance_ratio_ = self.explained_variance_ / total_variance
184180

185181
return self
186182

@@ -326,7 +322,7 @@ def main() -> None:
326322
print(f"\nReconstruction error (MSE): {reconstruction_error:.6f}")
327323

328324
# Compare with sklearn
329-
print("\n" + "="*50)
325+
print("\n" + "=" * 50)
330326
print("Comparison with scikit-learn:")
331327
compare_with_sklearn()
332328

0 commit comments

Comments
 (0)