1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
//! - k-Nearest Nerighbors
//!
//! Contains implemention of k-nearest search using
//! kd-tree, ball-tree and brute-force.
//!
//! # Usage
//!
//! ```
//! # #[macro_use] extern crate rulinalg; extern crate rusty_machine; fn main() {
//! use rusty_machine::learning::knn::KNNClassifier;
//! use rusty_machine::learning::SupModel;
//! use rusty_machine::linalg::Vector;
//!
//! let data = matrix![1., 1., 1.;
//!                    1., 2., 3.;
//!                    2., 3., 1.;
//!                    2., 2., 0.];
//! let target = Vector::new(vec![0, 0, 1, 1]);
//!
//! // train the model to search 2-nearest
//! let mut knn = KNNClassifier::new(2);
//! knn.train(&data, &target).unwrap();
//!
//! // predict new points
//! let res = knn.predict(&matrix![2., 3., 0.; 1., 1., 2.]).unwrap();
//! assert_eq!(res, Vector::new(vec![1, 0]));
//! # }
//! ```
use std::f64;
use std::collections::BTreeMap;

use linalg::{Matrix, BaseMatrix, Vector};
use learning::{LearningResult, SupModel};
use learning::error::{Error, ErrorKind};

mod binary_tree;
mod brute_force;

pub use self::binary_tree::{KDTree, BallTree};
pub use self::brute_force::BruteForce;

/// k-Nearest Neighbor Classifier
#[derive(Debug)]
pub struct KNNClassifier<S: KNearestSearch> {
    k: usize,

    searcher: S,
    target: Option<Vector<usize>>,
}

impl Default for KNNClassifier<KDTree> {
    /// Constructs an untrained KNN Classifier with searching 5 neighbors.
    ///
    /// # Examples
    ///
    /// ```
    /// use rusty_machine::learning::knn::KNNClassifier;
    /// let _ = KNNClassifier::default();
    /// ```
    fn default() -> Self {
        KNNClassifier {
            k: 5,
            searcher: KDTree::default(),
            target: None
        }
    }
}

impl KNNClassifier<KDTree> {
    /// Constructs an untrained KNN Classifier with specified
    /// number of search neighbors.
    ///
    /// # Examples
    ///
    /// ```
    /// use rusty_machine::learning::knn::KNNClassifier;
    /// let _ = KNNClassifier::new(3);
    /// ```
    pub fn new(k: usize) -> Self {
        KNNClassifier {
            k: k,
            searcher: KDTree::default(),
            target: None
        }
    }
}

impl<S: KNearestSearch> KNNClassifier<S> {
    /// Constructs an untrained KNN Classifier with specified
    /// k and leafsize for KDTree.
    ///
    /// # Examples
    ///
    /// ```
    /// use rusty_machine::learning::knn::{KNNClassifier, BallTree};
    /// let _ = KNNClassifier::new_specified(3, BallTree::new(10));
    /// ```
    pub fn new_specified(k: usize, searcher: S) -> Self {
        KNNClassifier {
            k: k,
            searcher: searcher,
            target: None
        }
    }
}

impl<S: KNearestSearch> SupModel<Matrix<f64>, Vector<usize>> for KNNClassifier<S> {

    fn predict(&self, inputs: &Matrix<f64>) -> LearningResult<Vector<usize>> {
        match self.target {
            Some(ref target) => {

                let mut results: Vec<usize> = Vec::with_capacity(inputs.rows());
                for row in inputs.row_iter() {
                    let (idx, _) = self.searcher.search(row.raw_slice(), self.k)?;
                    let res = target.select(&idx);
                    let (uniques, counts) = freq(res.data());
                    let (id, _) = counts.argmax();
                    results.push(uniques[id]);
                }
                Ok(Vector::new(results))
            },
            _ => Err(Error::new_untrained())
        }
    }

    fn train(&mut self, inputs: &Matrix<f64>, targets: &Vector<usize>) -> LearningResult<()> {
        if inputs.rows() != targets.size() {
            return Err(Error::new(ErrorKind::InvalidData,
                                  "inputs and targets must be the same length"));
        }
        if inputs.rows() < self.k {
            return Err(Error::new(ErrorKind::InvalidData,
                                  "inputs number of rows must be equal or learger than k"));
        }
        self.searcher.build(inputs.clone());
        self.target = Some(targets.clone());
        Ok(())
    }
}

/// Container for k-Nearest search results
struct KNearest {
    // number to search
    k: usize,
    // tuple of index and its distances, sorted by distances
    pairs: Vec<(usize, f64)>,
}

impl KNearest {

    fn new(k: usize, index: Vec<usize>, distances: Vec<f64>) -> Self {
        debug_assert!(!index.is_empty(), "index can't be empty");
        debug_assert!(index.len() == distances.len(),
                      "index and distance must have the same length");

        let mut pairs: Vec<(usize, f64)> = index.into_iter()
                                                .zip(distances.into_iter())
                                                .collect();
        // sort by distance, take k elements
        pairs.sort_by(|x, y| x.1.partial_cmp(&y.1).unwrap());
        pairs.truncate(k);

        KNearest {
            k: k,
            pairs: pairs
        }
    }

    /// Add new index and distances to the container, keeping first k elements which
    /// distances are smaller. Returns the updated farthest distance.
    fn add(&mut self, index: usize, distance: f64) -> f64 {
        // self.pairs can't be empty
        let len = self.pairs.len();
        // index of the last element after the query
        let last_index: usize = if len < self.k {
            len
        } else {
            len - 1
        };

        unsafe {
            if self.pairs.get_unchecked(len - 1).1 < distance {
                if len < self.k {
                    // append to the last
                    self.pairs.push((index, distance));
                }
                self.pairs.get_unchecked(last_index).1
            } else {
                // last element is already compared
                if len >= self.k {
                    self.pairs.pop().unwrap();
                }

                for i in 2..(len + 1) {
                    if self.pairs.get_unchecked(len - i).1 < distance {
                        self.pairs.insert(len - i + 1, (index, distance));
                        return self.pairs.get_unchecked(last_index).1;
                    }
                }
                self.pairs.insert(0, (index, distance));
                self.pairs.get_unchecked(last_index).1
            }
        }
    }

    /// Return the k-th distance with searching point
    fn dist(&self) -> f64 {
        // KNearest should gather k element at least
        let len = self.pairs.len();
        if len < self.k {
            f64::MAX
        } else {
            unsafe {
                // unchecked ver of .last().unwrap(),
                // because self.pairs can't be empty
                self.pairs.get_unchecked(len - 1).1
            }
        }
    }

    /// Extract the search result to k-nearest indices and corresponding distances
    fn get_results(self) -> (Vec<usize>, Vec<f64>) {
        let mut indices: Vec<usize> = Vec::with_capacity(self.k);
        let mut distances: Vec<f64> = Vec::with_capacity(self.k);
        for (i, d) in self.pairs {
            indices.push(i);
            distances.push(d);
        }
        (indices, distances)
    }
}

/// Search K-nearest items
pub trait KNearestSearch: Default{

    /// build data structure for search optimization
    fn build(&mut self, data: Matrix<f64>);

    /// Serch k-nearest items close to the point
    /// Returns a tuple of searched item index and its distances
    fn search(&self, point: &[f64], k: usize) -> Result<(Vec<usize>, Vec<f64>), Error>;
}

/// Count target label frequencies
/// TODO: Used in decisition tree, move impl to somewhere
fn freq(labels: &[usize]) -> (Vector<usize>, Vector<usize>) {
    let mut map: BTreeMap<usize, usize> = BTreeMap::new();
    for l in labels {
        let e = map.entry(*l).or_insert(0);
        *e += 1;
    }

    let mut uniques: Vec<usize> = Vec::with_capacity(map.len());
    let mut counts: Vec<usize> = Vec::with_capacity(map.len());
    for (&k, &v) in &map {
        uniques.push(k);
        counts.push(v);
    }
    (Vector::new(uniques), Vector::new(counts))
}

/// Return distances between given point and data specified with row ids
fn get_distances(data: &Matrix<f64>, point: &[f64], ids: &[usize]) -> Vec<f64> {
    assert!(!ids.is_empty(), "target ids is empty");

    let mut distances: Vec<f64> = Vec::with_capacity(ids.len());
    for id in ids.iter() {
        // ToDo: use .row(*id)
        let row: Vec<f64> = data.select_rows(&[*id]).into_vec();
        // let row: Vec<f64> = self.data.row(*id).into_vec();
        let d = dist(point, &row);
        distances.push(d);
    }
    distances
}

fn dist(v1: &[f64], v2: &[f64]) -> f64 {
    // ToDo: use metrics
    let d: f64 = v1.iter()
                   .zip(v2.iter())
                   .map(|(&x, &y)| (x - y) * (x - y))
                   .fold(0., |s, v| s + v);
    d.sqrt()
}

#[cfg(test)]
mod tests {

    use std::f64;
    use super::KNearest;

    #[test]
    fn test_knearest() {
        let mut kn = KNearest::new(2, vec![1, 2, 3], vec![3., 2., 1.]);
        assert_eq!(kn.k, 2);
        assert_eq!(kn.pairs, vec![(3, 1.), (2, 2.)]);
        assert_eq!(kn.dist(), 2.);

        // update KNearest
        let res = kn.add(10, 3.);
        assert_eq!(res, 2.);
        assert_eq!(kn.k, 2);
        assert_eq!(kn.pairs, vec![(3, 1.), (2, 2.)]);
        assert_eq!(kn.dist(), 2.);

        let res = kn.add(11, 0.);
        assert_eq!(res, 1.);
        assert_eq!(kn.k, 2);
        assert_eq!(kn.pairs, vec![(11, 0.), (3, 1.)]);
        assert_eq!(kn.dist(), 1.);
    }

    #[test]
    fn test_knearest2() {
        let mut kn = KNearest::new(4, vec![1, 2, 3], vec![3., 2., 1.]);
        assert_eq!(kn.k, 4);
        assert_eq!(kn.pairs, vec![(3, 1.), (2, 2.), (1, 3.)]);
        assert_eq!(kn.dist(), f64::MAX);

        let res = kn.add(5, 1.5);
        assert_eq!(res, 3.);
        assert_eq!(kn.k, 4);
        assert_eq!(kn.pairs, vec![(3, 1.), (5, 1.5), (2, 2.), (1, 3.)]);
        assert_eq!(kn.dist(), 3.);

        let res = kn.add(6, 6.);
        assert_eq!(res, 3.);
        assert_eq!(kn.k, 4);
        assert_eq!(kn.pairs, vec![(3, 1.), (5, 1.5), (2, 2.), (1, 3.)]);
        assert_eq!(kn.dist(), 3.);

        let res = kn.add(7, 0.5);
        assert_eq!(res, 2.);
        assert_eq!(kn.k, 4);
        assert_eq!(kn.pairs, vec![(7, 0.5), (3, 1.), (5, 1.5), (2, 2.)]);
        assert_eq!(kn.dist(), 2.);
    }
}