Skip to content

Commit 4af760d

Browse files
AyushGarg76Copilot
andauthored
Potential fix for pull request finding
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
1 parent 4845529 commit 4af760d

1 file changed

Lines changed: 11 additions & 0 deletions

File tree

machine_learning/uniform_manifold_approximation_and_projection.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,12 +125,23 @@ def find_nearest_neighbors(distance_matrix: ndarray, n_neighbors: int) -> ndarra
125125
Row i contains the indices of point i's k nearest neighbours
126126
(not including itself).
127127
128+
Raises:
129+
ValueError: If `n_neighbors` is less than 1 or greater than the
130+
maximum number of non-self neighbours available, `n_samples - 1`.
131+
128132
>>> dist = np.array([[0., 1., 2.], [1., 0., 3.], [2., 3., 0.]])
129133
>>> nn = find_nearest_neighbors(dist, n_neighbors=1)
130134
>>> nn.tolist()
131135
[[1], [0], [0]]
132136
"""
133137
n_samples = distance_matrix.shape[0]
138+
max_neighbors = n_samples - 1
139+
if not 1 <= n_neighbors <= max_neighbors:
140+
raise ValueError(
141+
f"n_neighbors must be between 1 and {max_neighbors} for a "
142+
f"distance matrix with {n_samples} samples; got {n_neighbors}."
143+
)
144+
134145
neighbor_indices = np.zeros((n_samples, n_neighbors), dtype=int)
135146

136147
for i in range(n_samples):

0 commit comments

Comments
 (0)