pub fn new(
        test_x: &ArrayView2<f64>,
        test_y: &ArrayView1<Label>,
        max_n: usize,
        distance: D,
        strategy: KNNStrategy,
    ) -> KNNEstimator<D> {
        assert_eq!(test_x.nrows(), test_y.len());
        assert!(!test_y.is_empty());

        // How we select k given n.
        let k_from_n = knn_strategy(strategy);
        // max_k specifies the maximum number of neighbors to store
        // (excluding ties); a smaller max_k improves performances, but
        // its value should be sufficiently large to give correct
        // results once we've seen all the training data.
        let max_k = k_from_n(max_n);

        let neighbors = test_x
            .outer_iter()
            .map(|x| NearestNeighbors::new(&x, max_k, distance))
            .collect::<Vec<_>>();
        // We initially set all predictions to 0. Therefore, we need to
        // adjust the error count accordingly. Note that this is updated as
        // soon as add_example() is called.
        // A more proper way to do this in Rust would be to set
        // predictions (and errors, k_error_count, ...) to an Option value.
        // TODO: should we?
        let errors = test_y
            .iter()
            .map(|y| if *y != 0 { 1 } else { 0 })
            .collect::<Vec<_>>();
        let error_count = errors.iter().sum();

        KNNEstimator {
            neighbors,
            errors,
            predictions: vec![0; test_y.len()],
            labels: test_y.to_vec(),
            current_k: 1,
            k_error_count: error_count,
            n: 0,
            k_from_n,
        }
    }
/// Create a new k-NN estimator.
Create a new k-NN estimator.
Create a new k - NN estimator .
pub fn new(
        test_x: &ArrayView2<f64>,
        test_y: &ArrayView1<Label>,
        max_n: usize,
        distance: D,
        strategy: KNNStrategy,
    ) -> KNNEstimator<D> {
        assert_eq!(test_x.nrows(), test_y.len());
        assert!(!test_y.is_empty());
        // How we select k given n.
        let k_from_n = knn_strategy(strategy);
        // max_k specifies the maximum number of neighbors to store
        // (excluding ties); a smaller max_k improves performances, but
        // its value should be sufficiently large to give correct
        // results once we've seen all the training data.
        let max_k = k_from_n(max_n);
        let neighbors = test_x
            .outer_iter()
            .map(|x| NearestNeighbors::new(&x, max_k, distance))
            .collect::<Vec<_>>();
        // We initially set all predictions to 0. Therefore, we need to
        // adjust the error count accordingly. Note that this is updated as
        // soon as add_example() is called.
        // A more proper way to do this in Rust would be to set
        // predictions (and errors, k_error_count, ...) to an Option value.
        // TODO: should we?
        let errors = test_y
            .iter()
            .map(|y| if *y != 0 { 1 } else { 0 })
            .collect::<Vec<_>>();
        let error_count = errors.iter().sum();
        KNNEstimator {
            neighbors,
            errors,
            predictions: vec![0; test_y.len()],
            labels: test_y.to_vec(),
            current_k: 1,
            k_error_count: error_count,
            n: 0,
            k_from_n,
        }
    }
pub fn new ( test_x : & ArrayView2 < f64 > , test_y : & ArrayView1 < Label > , max_n : usize , distance : D , strategy : KNNStrategy , ) -> KNNEstimator < D > { assert_eq ! ( test_x nrows ( ) test_y len ( ) ) ; assert ! ( test_y is_empty ( ) ) ; // How we select k given n. let k_from_n = knn_strategy ( strategy ) ; // max_k specifies the maximum number of neighbors to store // (excluding ties); a smaller max_k improves performances, but // its value should be sufficiently large to give correct // results once we've seen all the training data. let max_k = k_from_n ( max_n ) ; let neighbors = test_x . outer_iter ( ) . map ( | x | NearestNeighbors :: new ( & x , max_k , distance ) ) . collect :: < Vec < _ > > ( ) ; // We initially set all predictions to 0. Therefore, we need to // adjust the error count accordingly. Note that this is updated as // soon as add_example() is called. // A more proper way to do this in Rust would be to set // predictions (and errors, k_error_count, ...) to an Option value. // TODO: should we? let errors = test_y . iter ( ) . map ( | y | if * y != 0 { 1 } else { 0 } ) . collect :: < Vec < _ > > ( ) ; let error_count = errors . iter ( ) . sum ( ) ; KNNEstimator { neighbors , errors , predictions : vec ! [ 0 test_y len ( ) ] , labels : test_y . to_vec ( ) , current_k : 1 , k_error_count : error_count , n : 0 , k_from_n , } }
Create a new k-NN estimator.
Create a new k - NN estimator .
test_x
&ArrayView2<f64>
test_y
&ArrayView1<Label>
max_n
usize
distance
D
strategy
KNNStrategy
test_x
&ArrayView2<f64>
test_y
&ArrayView1<Label>
max_n
usize
distance
D
strategy
KNNStrategy