Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit f2054af

Browse filesBrowse files
committed
Improve test coverage of KernelDensity with bounds.
1 parent 3ab81c6 commit f2054af
Copy full SHA for f2054af

File tree

2 files changed

+20
-5
lines changed
Filter options

2 files changed

+20
-5
lines changed

‎sklearn/neighbors/_kde.py

Copy file name to clipboardExpand all lines: sklearn/neighbors/_kde.py
+5-5Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -202,11 +202,6 @@ def _choose_algorithm(self, algorithm, metric):
202202
return algorithm
203203

204204
def _evaluate_hypercube_faces(self):
205-
lower, upper = np.transpose(self.bounds)
206-
if np.any(upper <= lower):
207-
raise ValueError(
208-
f"invalid bounds: upper ({upper}) is not larger than lower ({lower})"
209-
)
210205
n_dims = len(self.bounds)
211206
# Start the queue with all vertices, then recursively create all faces.
212207
queue = set(itertools.product(*self.bounds))
@@ -434,6 +429,11 @@ def _validate_data(
434429
):
435430
if self.bounds is not None:
436431
lower, upper = np.transpose(self.bounds)
432+
if np.any(upper <= lower):
433+
raise ValueError(
434+
f"invalid bounds: upper ({upper}) is not larger than lower "
435+
f"({lower})"
436+
)
437437
if np.any(X < lower):
438438
raise ValueError("samples must be larger than lower bound")
439439
if np.any(X > upper):

‎sklearn/neighbors/tests/test_kde.py

Copy file name to clipboardExpand all lines: sklearn/neighbors/tests/test_kde.py
+15Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,21 @@ def test_kde_hypercube_faces(n_dims, expected_counts):
274274
np.testing.assert_array_equal(actual_counts, expected_counts)
275275

276276

277+
def test_kde_with_bounds_invalid():
278+
with pytest.raises(ValueError, match="invalid bounds"):
279+
KernelDensity(bounds=[(3, 1)]).fit(None)
280+
281+
kde = KernelDensity(bounds=[(0, 2)]).fit(np.ones((3, 1)))
282+
with pytest.raises(NotImplementedError):
283+
kde.sample()
284+
285+
with pytest.raises(ValueError, match="samples must be larger"):
286+
KernelDensity(bounds=[(1, 2)]).fit(np.zeros((3, 1)))
287+
288+
with pytest.raises(ValueError, match="samples must be smaller"):
289+
KernelDensity(bounds=[(1, 2)]).fit(3 * np.ones((3, 1)))
290+
291+
277292
def test_kde_bounded_density():
278293
n_samples, n_features = (100_000, 2)
279294
rng = np.random.RandomState(0)

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.