/// Adds a new training example.
Adds a new training example.
Adds a new training example .
fn add_example(&mut self, x: &ArrayView1<f64>, y: Label) -> Result<(), ()> {
        let x = self.array_to_index.map(*x);
        self.train_x.push(x);
        self.train_y.push(y);
        let mut old_priors_pred = match self.priors_count.predict() {
            Some(pred) => pred,
            None => {
                self.add_first_example(x, y);
                return Ok(());
            }
        };
        // If max prior changed, update predictions for those that were
        // predicted with priors.
        let priors_changed = self.priors_count.add_example(y);
        if priors_changed {
            let new_pred = self.priors_count.predict().unwrap();
            for (xi, &yi) in self.test_x.iter().zip(&self.test_y) {
                // Match points for which we random guess.
                let joint = self.joint_count.get(xi).expect("shouldn't happen");
                if joint.predict().is_none() {
                    let old_error = if yi != old_priors_pred { 1 } else { 0 };
                    let new_error = if yi != new_pred { 1 } else { 0 };
                    self.error_count = self.error_count + new_error - old_error;
                }
            }
            // NOTE: we also need to update the value of old_priors_pred,
            // because otherwise we'll have issues when updating w.r.t.
            // the joint distribution later in this function.
            old_priors_pred = new_pred;
        }
        // Update joint counts (and error), but only if `x` appears
        // in the test set.
        if let Some(joint) = self.joint_count.get_mut(&x) {
            let old_pred = match joint.predict() {
                Some(pred) => pred,
                None => old_priors_pred,
            };
            // Only update prediction if max P(o,s) changed.
            let joint_changed = joint.add_example(y);
            if joint_changed {
                // Predict again.
                let new_pred = joint.predict().unwrap();
                for (&xi, &yi) in self.test_x.iter().zip(&self.test_y) {
                    // Only update predictions for observations with value `x`.
                    if xi == x {
                        let old_error = if yi != old_pred { 1 } else { 0 };
                        let new_error = if yi != new_pred { 1 } else { 0 };
                        self.error_count = self.error_count + new_error - old_error;
                    }
                }
            }
        }
        Ok(())
    }
fn add_example ( & mut self , x : & ArrayView1 < f64 > , y : Label ) -> Result < ( ) , ( ) > { let x = self . array_to_index . map ( * x ) ; self . train_x . push ( x ) ; self . train_y . push ( y ) ; let mut old_priors_pred = match self . priors_count . predict ( ) { Some ( pred ) => pred , None => { self . add_first_example ( x , y ) ; return Ok ( ( ) ) ; } } ; // If max prior changed, update predictions for those that were // predicted with priors. let priors_changed = self . priors_count . add_example ( y ) ; if priors_changed { let new_pred = self . priors_count . predict ( ) . unwrap ( ) ; for ( xi , & yi ) in self . test_x . iter ( ) . zip ( & self . test_y ) { // Match points for which we random guess. let joint = self . joint_count . get ( xi ) . expect ( " " ) ; if joint . predict ( ) . is_none ( ) { let old_error = if yi != old_priors_pred { 1 } else { 0 } ; let new_error = if yi != new_pred { 1 } else { 0 } ; self . error_count = self . error_count + new_error - old_error ; } } // NOTE: we also need to update the value of old_priors_pred, // because otherwise we'll have issues when updating w.r.t. // the joint distribution later in this function. old_priors_pred = new_pred ; } // Update joint counts (and error), but only if `x` appears // in the test set. if let Some ( joint ) = self . joint_count . get_mut ( & x ) { let old_pred = match joint . predict ( ) { Some ( pred ) => pred , None => old_priors_pred , } ; // Only update prediction if max P(o,s) changed. let joint_changed = joint . add_example ( y ) ; if joint_changed { // Predict again. let new_pred = joint . predict ( ) . unwrap ( ) ; for ( & xi , & yi ) in self . test_x . iter ( ) . zip ( & self . test_y ) { // Only update predictions for observations with value `x`. if xi == x { let old_error = if yi != old_pred { 1 } else { 0 } ; let new_error = if yi != new_pred { 1 } else { 0 } ; self . error_count = self . error_count + new_error - old_error ; } } } } Ok ( ( ) ) }
Adds a new training example.
Adds a new training example .
&mut self
x
&ArrayView1<f64>
y
Label