From 92dad01810c7407c99460b78b3a145c1dfee8e4d Mon Sep 17 00:00:00 2001 From: Luis Moreno Date: Fri, 16 Oct 2020 12:28:30 -0400 Subject: [PATCH] Allow KNN with k=1 --- src/algorithm/sort/heap_select.rs | 3 +++ src/neighbors/knn_regressor.rs | 4 ++-- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/src/algorithm/sort/heap_select.rs b/src/algorithm/sort/heap_select.rs index 063ffc6..ae3ff18 100644 --- a/src/algorithm/sort/heap_select.rs +++ b/src/algorithm/sort/heap_select.rs @@ -41,6 +41,9 @@ impl<'a, T: PartialOrd + Debug> HeapSelection { pub fn heapify(&mut self) { let n = self.heap.len(); + if n <= 1 { + return; + } for i in (0..=(n / 2 - 1)).rev() { self.sift_down(i, n - 1); } diff --git a/src/neighbors/knn_regressor.rs b/src/neighbors/knn_regressor.rs index 04fbd35..0bf283f 100644 --- a/src/neighbors/knn_regressor.rs +++ b/src/neighbors/knn_regressor.rs @@ -116,9 +116,9 @@ impl, T>> KNNRegressor { ))); } - if parameters.k <= 1 { + if parameters.k < 1 { return Err(Failed::fit(&format!( - "k should be > 1, k=[{}]", + "k should be > 0, k=[{}]", parameters.k ))); }