/// Adds a new example to the k-NN estimator's training data.
///
/// This also updates the prediction, if necessary.
Adds a new example to the k-NN estimator's training data.
This also updates the prediction, if necessary.
Adds a new example to the k - NN estimator ' s training data . This also updates the prediction if necessary .
fn add_example(&mut self, x: &ArrayView1<f64>, y: Label) -> Result<(), ()> {
        // Update k with respect to n.
        self.set_k((self.k_from_n)(self.n))?;
        // We update n here, before error are (possibly) raised.
        self.n += 1;
        // We copy because we're using them in the closure below.
        let current_k = self.current_k;
        // Update errors and neighbors as appropriate.
        for (neigh, true_y, old_pred, old_error) in izip!(
            &mut self.neighbors,
            &self.labels,
            &mut self.predictions,
            &mut self.errors
        ) {
            if neigh.add_example(x, y) {
                if neigh.updated_k > current_k {
                    continue;
                }
                // Update prediction.
                let pred = neigh.predict(current_k)?;
                if pred == *old_pred {
                    continue;
                }
                // Update error.
                let error = if pred != *true_y { 1 } else { 0 };
                *old_pred = pred;
                match (error, *old_error) {
                    (1, 0) => self.k_error_count += 1,
                    (0, 1) => self.k_error_count -= 1,
                    // No need to update. Note that we do not need
                    // to set *old_error = error either, as they're
                    // also the same. So we can return now.
                    _ => continue,
                };
                *old_error = error;
            }
        }
        Ok(())
    }
fn add_example ( & mut self , x : & ArrayView1 < f64 > , y : Label ) -> Result < ( ) , ( ) > { // Update k with respect to n. self . set_k ( ( self . k_from_n ) ( self . n ) ) ? ; // We update n here, before error are (possibly) raised. self . n += 1 ; // We copy because we're using them in the closure below. let current_k = self . current_k ; // Update errors and neighbors as appropriate. for ( neigh , true_y , old_pred , old_error ) in izip ! ( mut self neighbors self labels mut self predictions mut self errors ) { if neigh . add_example ( x , y ) { if neigh . updated_k > current_k { continue ; } // Update prediction. let pred = neigh . predict ( current_k ) ? ; if pred == * old_pred { continue ; } // Update error. let error = if pred != * true_y { 1 } else { 0 } ; * old_pred = pred ; match ( error , * old_error ) { ( 1 , 0 ) => self . k_error_count += 1 , ( 0 , 1 ) => self . k_error_count -= 1 , // No need to update. Note that we do not need // to set *old_error = error either, as they're // also the same. So we can return now. _ => continue , } ; * old_error = error ; } } Ok ( ( ) ) }
Adds a new example to the k-NN estimator's training data.
Adds a new example to the k - NN estimator ' s training data .
&mut self
x
&ArrayView1<f64>
y
Label