Skip to content

Commit d0e9ccb

Browse files
committed
fix: weights handling for N=1, empty splits in DecisionTree, and exception types
- LinearRegression: fix np.squeeze causing 0-d array when N=1 (fixes #77) - DecisionTree: handle empty child nodes to prevent ZeroDivisionError (fixes #58) - Replace bare except with specific types (gp.py, trainer.py) - Replace generic Exception with ValueError/RuntimeError (knn.py, general.py)
1 parent b0359af commit d0e9ccb

5 files changed

Lines changed: 8 additions & 5 deletions

File tree

numpy_ml/linear_models/linear_regression.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ def fit(self, X, y, weights=None):
197197
N = X.shape[0]
198198

199199
weights = np.ones(N) if weights is None else np.atleast_1d(weights)
200-
weights = np.squeeze(weights) if weights.size > 1 else weights
200+
weights = np.atleast_1d(np.squeeze(weights)) if weights.size > 1 else weights
201201
err_str = f"weights must have shape ({N},) but got {weights.shape}"
202202
assert weights.shape == (N,), err_str
203203

numpy_ml/nonparametric/gp.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
try:
66
_SCIPY = True
77
from scipy.stats import norm
8-
except:
8+
except ImportError:
99
_SCIPY = False
1010
warnings.warn(
1111
"Could not import scipy.stats. Confidence scores "

numpy_ml/nonparametric/knn.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ def fit(self, X, y):
5656
Targets for the `N` rows in `X`.
5757
"""
5858
if X.ndim != 2:
59-
raise Exception("X must be two-dimensional")
59+
raise ValueError("X must be two-dimensional")
6060
self._ball_tree.fit(X, y)
6161

6262
def predict(self, X):

numpy_ml/preprocessing/general.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ def transform(self, X):
238238
The feature-wise standardized version of `X`.
239239
"""
240240
if not self._is_fit:
241-
raise Exception("Must call `fit` before using the `transform` method")
241+
raise RuntimeError("Must call `fit` before using the `transform` method")
242242
return (X - self._mean) / self._std
243243

244244
def inverse_transform(self, Z):

numpy_ml/trees/dt.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,10 @@ def predict_class_probs(self, X):
120120
return np.array([self._traverse(x, self.root, prob=True) for x in X])
121121

122122
def _grow(self, X, Y, cur_depth=0):
123-
# if all labels are the same, return a leaf
123+
# if all labels are the same, or node is empty, return a leaf
124+
if len(Y) == 0:
125+
prob = np.zeros(self.n_classes) if self.classifier else 0.0
126+
return Leaf(prob)
124127
if len(set(Y)) == 1:
125128
if self.classifier:
126129
prob = np.zeros(self.n_classes)

0 commit comments

Comments
 (0)