Skip to content

Commit dd95c13

Browse files
frsijnothman
authored andcommitted
DOC Fixed BinaryTree Documentation Examples (issue scikit-learn#10199) (scikit-learn#10205)
1 parent bcefc85 commit dd95c13

File tree

3 files changed

+11
-61
lines changed

3 files changed

+11
-61
lines changed

sklearn/neighbors/ball_tree.pyx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ cdef class BallTree(BinaryTree):
2929
__doc__ = CLASS_DOC.format(**DOC_DICT)
3030
pass
3131

32+
3233
#----------------------------------------------------------------------
3334
# The functions below specialized the Binary Tree as a Ball Tree
3435
#
@@ -39,7 +40,6 @@ cdef class BallTree(BinaryTree):
3940
# distance for the Euclidean metric is the squared-euclidean distance.
4041
# For some metrics, the reduced distance is simply the distance.
4142

42-
4343
cdef int allocate_data(BinaryTree tree, ITYPE_t n_nodes,
4444
ITYPE_t n_features) except -1:
4545
"""Allocate arrays needed for the KD Tree"""

sklearn/neighbors/binary_tree.pxi

Lines changed: 8 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@ Query for k-nearest neighbors
296296
>>> np.random.seed(0)
297297
>>> X = np.random.random((10, 3)) # 10 points in 3 dimensions
298298
>>> tree = {BinaryTree}(X, leaf_size=2) # doctest: +SKIP
299-
>>> dist, ind = tree.query([X[0]], k=3) # doctest: +SKIP
299+
>>> dist, ind = tree.query(X[:1], k=3) # doctest: +SKIP
300300
>>> print(ind) # indices of 3 closest neighbors
301301
[0 3 1]
302302
>>> print(dist) # distances to 3 closest neighbors
@@ -312,7 +312,7 @@ pickle operation: the tree needs not be rebuilt upon unpickling.
312312
>>> tree = {BinaryTree}(X, leaf_size=2) # doctest: +SKIP
313313
>>> s = pickle.dumps(tree) # doctest: +SKIP
314314
>>> tree_copy = pickle.loads(s) # doctest: +SKIP
315-
>>> dist, ind = tree_copy.query(X[0], k=3) # doctest: +SKIP
315+
>>> dist, ind = tree_copy.query(X[:1], k=3) # doctest: +SKIP
316316
>>> print(ind) # indices of 3 closest neighbors
317317
[0 3 1]
318318
>>> print(dist) # distances to 3 closest neighbors
@@ -324,9 +324,9 @@ Query for neighbors within a given radius
324324
>>> np.random.seed(0)
325325
>>> X = np.random.random((10, 3)) # 10 points in 3 dimensions
326326
>>> tree = {BinaryTree}(X, leaf_size=2) # doctest: +SKIP
327-
>>> print(tree.query_radius(X[0], r=0.3, count_only=True))
327+
>>> print(tree.query_radius(X[:1], r=0.3, count_only=True))
328328
3
329-
>>> ind = tree.query_radius(X[0], r=0.3) # doctest: +SKIP
329+
>>> ind = tree.query_radius(X[:1], r=0.3) # doctest: +SKIP
330330
>>> print(ind) # indices of neighbors within distance 0.3
331331
[3 0 1]
332332
@@ -1240,7 +1240,7 @@ cdef class BinaryTree:
12401240
12411241
Parameters
12421242
----------
1243-
X : array-like, last dimension self.dim
1243+
X : array-like, shape = [n_samples, n_features]
12441244
An array of points to query
12451245
k : integer (default = 1)
12461246
The number of nearest neighbors to return
@@ -1272,20 +1272,6 @@ cdef class BinaryTree:
12721272
i : array of integers - shape: x.shape[:-1] + (k,)
12731273
each entry gives the list of indices of
12741274
neighbors of the corresponding point
1275-
1276-
Examples
1277-
--------
1278-
Query for k-nearest neighbors
1279-
1280-
>>> import numpy as np
1281-
>>> np.random.seed(0)
1282-
>>> X = np.random.random((10, 3)) # 10 points in 3 dimensions
1283-
>>> tree = BinaryTree(X, leaf_size=2) # doctest: +SKIP
1284-
>>> dist, ind = tree.query(X[0], k=3) # doctest: +SKIP
1285-
>>> print(ind) # indices of 3 closest neighbors
1286-
[0 3 1]
1287-
>>> print(dist) # distances to 3 closest neighbors
1288-
[ 0. 0.19662693 0.29473397]
12891275
"""
12901276
# XXX: we should allow X to be a pre-built tree.
12911277
X = check_array(X, dtype=DTYPE, order='C')
@@ -1364,7 +1350,7 @@ cdef class BinaryTree:
13641350
13651351
Parameters
13661352
----------
1367-
X : array-like, last dimension self.dim
1353+
X : array-like, shape = [n_samples, n_features]
13681354
An array of points to query
13691355
r : distance within which neighbors are returned
13701356
r can be a single value, or an array of values of shape
@@ -1406,20 +1392,6 @@ cdef class BinaryTree:
14061392
dist : array of objects, shape = X.shape[:-1]
14071393
each element is a numpy double array
14081394
listing the distances corresponding to indices in i.
1409-
1410-
Examples
1411-
--------
1412-
Query for neighbors in a given radius
1413-
1414-
>>> import numpy as np
1415-
>>> np.random.seed(0)
1416-
>>> X = np.random.random((10, 3)) # 10 points in 3 dimensions
1417-
>>> tree = BinaryTree(X, leaf_size=2) # doctest: +SKIP
1418-
>>> print(tree.query_radius(X[0], r=0.3, count_only=True))
1419-
3
1420-
>>> ind = tree.query_radius(X[0], r=0.3) # doctest: +SKIP
1421-
>>> print(ind) # indices of neighbors within distance 0.3
1422-
[3 0 1]
14231395
"""
14241396
if count_only and return_distance:
14251397
raise ValueError("count_only and return_distance "
@@ -1513,7 +1485,7 @@ cdef class BinaryTree:
15131485
15141486
Parameters
15151487
----------
1516-
X : array_like
1488+
X : array-like, shape = [n_samples, n_features]
15171489
An array of points to query. Last dimension should match dimension
15181490
of training data.
15191491
h : float
@@ -1544,17 +1516,6 @@ cdef class BinaryTree:
15441516
-------
15451517
density : ndarray
15461518
The array of (log)-density evaluations, shape = X.shape[:-1]
1547-
1548-
Examples
1549-
--------
1550-
Compute a gaussian kernel density estimate:
1551-
1552-
>>> import numpy as np
1553-
>>> np.random.seed(1)
1554-
>>> X = np.random.random((100, 3))
1555-
>>> tree = BinaryTree(X) # doctest: +SKIP
1556-
>>> tree.kernel_density(X[:3], h=0.1, kernel='gaussian')
1557-
array([ 6.94114649, 7.83281226, 7.2071716 ])
15581519
"""
15591520
cdef DTYPE_t h_c = h
15601521
cdef DTYPE_t log_atol = log(atol)
@@ -1657,7 +1618,7 @@ cdef class BinaryTree:
16571618
16581619
Parameters
16591620
----------
1660-
X : array_like
1621+
X : array-like, shape = [n_samples, n_features]
16611622
An array of points to query. Last dimension should match dimension
16621623
of training data.
16631624
r : array_like
@@ -1672,18 +1633,6 @@ cdef class BinaryTree:
16721633
counts : ndarray
16731634
counts[i] contains the number of pairs of points with distance
16741635
less than or equal to r[i]
1675-
1676-
Examples
1677-
--------
1678-
Compute the two-point autocorrelation function of X:
1679-
1680-
>>> import numpy as np
1681-
>>> np.random.seed(0)
1682-
>>> X = np.random.random((30, 3))
1683-
>>> r = np.linspace(0, 1, 5)
1684-
>>> tree = BinaryTree(X) # doctest: +SKIP
1685-
>>> tree.two_point_correlation(X, r)
1686-
array([ 30, 62, 278, 580, 820])
16871636
"""
16881637
cdef ITYPE_t n_features = self.data.shape[1]
16891638
cdef ITYPE_t i

sklearn/neighbors/kd_tree.pyx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,14 @@ VALID_METRICS = ['EuclideanDistance', 'ManhattanDistance',
1515
'ChebyshevDistance', 'MinkowskiDistance']
1616

1717

18-
# Inherit KDTree from BinaryTree
1918
include "binary_tree.pxi"
2019

20+
# Inherit KDTree from BinaryTree
2121
cdef class KDTree(BinaryTree):
2222
__doc__ = CLASS_DOC.format(**DOC_DICT)
2323
pass
2424

25+
2526
#----------------------------------------------------------------------
2627
# The functions below specialized the Binary Tree as a KD Tree
2728
#

0 commit comments

Comments
 (0)