Skip to content

Commit 108e009

Browse files
committed
FIX Gaussian KDE should return array, not scalar
Fixes scikit-learn#3269.
1 parent 5358be5 commit 108e009

File tree

2 files changed

+6
-1
lines changed

2 files changed

+6
-1
lines changed

sklearn/neighbors/kde.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ def sample(self, n_samples=1, random_state=None):
200200
i = rng.randint(data.shape[0], size=n_samples)
201201

202202
if self.kernel == 'gaussian':
203-
return rng.normal(data[i], self.bandwidth)
203+
return np.atleast_2d(rng.normal(data[i], self.bandwidth))
204204

205205
elif self.kernel == 'tophat':
206206
# we first draw points from a d-dimensional normal distribution,

sklearn/neighbors/tests/test_kde.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,11 @@ def test_kernel_density_sampling(n_samples=100, n_features=3):
7979
kde = KernelDensity(bandwidth, kernel=kernel).fit(X)
8080
assert_raises(NotImplementedError, kde.sample, 100)
8181

82+
# non-regression test: used to return a scalar
83+
X = np.random.random((4, 1))
84+
kde = KernelDensity(kernel="gaussian").fit(X)
85+
assert_equal(kde.sample().shape, (1, 1))
86+
8287

8388
def test_kde_algorithm_metric_choice():
8489
"""Smoke test for various metrics and algorithms"""

0 commit comments

Comments
 (0)