/// Create a k-NN estimator from training and test set.
Create a k-NN estimator from training and test set.
Create a k - NN estimator from training and test set .
pub fn from_data(
        train_x: &ArrayView2<f64>,
        train_y: &ArrayView1<Label>,
        test_x: &ArrayView2<f64>,
        test_y: &ArrayView1<Label>,
        max_n: usize,
        distance: D,
        strategy: KNNStrategy,
    ) -> KNNEstimator<D> {
        assert_eq!(train_x.ncols(), test_x.ncols());
        assert_eq!(train_x.nrows(), train_y.len());
        assert_eq!(test_x.nrows(), test_y.len());
        assert!(!train_x.is_empty());
        assert!(!test_x.is_empty());
        // How we select k given n.
        let k_from_n = knn_strategy(strategy);
        // Maximum number of neighbors to store (excluding ties).
        let max_k = k_from_n(max_n);
        let neighbors = test_x
            .outer_iter()
            .map(|x| {
                NearestNeighbors::from_data(&x, &train_x.view(), &train_y.view(), max_k, distance)
            })
            .collect::<Vec<_>>();
        let n = train_y.len();
        let k = k_from_n(n);
        let mut knn_error = 0;
        let mut errors = Vec::with_capacity(test_y.len());
        let mut predictions = Vec::with_capacity(test_y.len());
        for (neigh, y) in neighbors.iter().zip(test_y) {
            let pred = neigh.predict(k).expect("unexpected error");
            let error = if pred != *y { 1 } else { 0 };
            predictions.push(pred);
            errors.push(error);
            knn_error += error;
        }
        KNNEstimator {
            neighbors,
            errors,
            predictions,
            labels: test_y.to_vec(),
            current_k: k,
            k_error_count: knn_error,
            n,
            k_from_n,
        }
    }
pub fn from_data ( train_x : & ArrayView2 < f64 > , train_y : & ArrayView1 < Label > , test_x : & ArrayView2 < f64 > , test_y : & ArrayView1 < Label > , max_n : usize , distance : D , strategy : KNNStrategy , ) -> KNNEstimator < D > { assert_eq ! ( train_x ncols ( ) test_x ncols ( ) ) ; assert_eq ! ( train_x nrows ( ) train_y len ( ) ) ; assert_eq ! ( test_x nrows ( ) test_y len ( ) ) ; assert ! ( train_x is_empty ( ) ) ; assert ! ( test_x is_empty ( ) ) ; // How we select k given n. let k_from_n = knn_strategy ( strategy ) ; // Maximum number of neighbors to store (excluding ties). let max_k = k_from_n ( max_n ) ; let neighbors = test_x . outer_iter ( ) . map ( | x | { NearestNeighbors :: from_data ( & x , & train_x . view ( ) , & train_y . view ( ) , max_k , distance ) } ) . collect :: < Vec < _ > > ( ) ; let n = train_y . len ( ) ; let k = k_from_n ( n ) ; let mut knn_error = 0 ; let mut errors = Vec :: with_capacity ( test_y . len ( ) ) ; let mut predictions = Vec :: with_capacity ( test_y . len ( ) ) ; for ( neigh , y ) in neighbors . iter ( ) . zip ( test_y ) { let pred = neigh . predict ( k ) . expect ( " " ) ; let error = if pred != * y { 1 } else { 0 } ; predictions . push ( pred ) ; errors . push ( error ) ; knn_error += error ; } KNNEstimator { neighbors , errors , predictions , labels : test_y . to_vec ( ) , current_k : k , k_error_count : knn_error , n , k_from_n , } }
Create a k-NN estimator from training and test set.
Create a k - NN estimator from training and test set .
train_x
&ArrayView2<f64>
train_y
&ArrayView1<Label>
test_x
&ArrayView2<f64>
test_y
&ArrayView1<Label>
max_n
usize
distance
D
strategy
KNNStrategy