rusty_machine::learning::svm

Struct SVM

Source
pub struct SVM<K: Kernel> {
    pub optim_iters: usize,
    /* private fields */
}
Expand description

Support Vector Machine

Fields§

§optim_iters: usize

Number of iterations for training.

Implementations§

Source§

impl<K: Kernel> SVM<K>

Source

pub fn new(ker: K, lambda: f64) -> SVM<K>

Constructs an untrained SVM with specified kernel and lambda which determins the hardness of the margin.

§Examples
use rusty_machine::learning::svm::SVM;
use rusty_machine::learning::toolkit::kernel::SquaredExp;

let _ = SVM::new(SquaredExp::default(), 0.3);
Examples found in repository?
examples/svm-sign_learner.rs (line 32)
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
fn main() {
    println!("Sign learner sample:");

    println!("Training...");
    // Training data
    let inputs = Matrix::new(11, 1, vec![
                             -0.1, -2., -9., -101., -666.7,
                             0., 0.1, 1., 11., 99., 456.7
                             ]);
    let targets = Vector::new(vec![
                              -1., -1., -1., -1., -1.,
                              1., 1., 1., 1., 1., 1.
                              ]);

    // Trainee
    let mut svm_mod = SVM::new(HyperTan::new(100., 0.), 0.3);
    // Our train function returns a Result<(), E>
    svm_mod.train(&inputs, &targets).unwrap();

    println!("Evaluation...");
    let mut hits = 0;
    let mut misses = 0;
    // Evaluation
    //   Note: We could pass all input values at once to the `predict` method!
    //         Here, we use a loop just to count and print logs.
    for n in (-1000..1000).filter(|&x| x % 100 == 0) {
        let nf = n as f64;
        let input = Matrix::new(1, 1, vec![nf]);
        let out = svm_mod.predict(&input).unwrap();
        let res = if out[0] * nf > 0. {
            hits += 1;
            true
        } else if nf == 0. {
            hits += 1;
            true
        } else {
            misses += 1;
            false
        };

        println!("{} -> {}: {}", Matrix::data(&input)[0], out[0], res);
    }

    println!("Performance report:");
    println!("Hits: {}, Misses: {}", hits, misses);
    let hits_f = hits as f64;
    let total = (hits + misses) as f64;
    println!("Accuracy: {}", (hits_f / total) * 100.);
}

Trait Implementations§

Source§

impl<K: Debug + Kernel> Debug for SVM<K>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for SVM<SquaredExp>

The default Support Vector Machine.

The defaults are:

  • ker = SquaredExp::default()
  • lambda = 0.3
  • optim_iters = 100
Source§

fn default() -> SVM<SquaredExp>

Returns the “default value” for a type. Read more
Source§

impl<K: Kernel> SupModel<Matrix<f64>, Vector<f64>> for SVM<K>

Train the model using the Pegasos algorithm and predict the model output from new data.

Source§

fn predict(&self, inputs: &Matrix<f64>) -> LearningResult<Vector<f64>>

Predict output from inputs.
Source§

fn train( &mut self, inputs: &Matrix<f64>, targets: &Vector<f64>, ) -> LearningResult<()>

Train the model using inputs and targets.

Auto Trait Implementations§

§

impl<K> Freeze for SVM<K>
where K: Freeze,

§

impl<K> RefUnwindSafe for SVM<K>
where K: RefUnwindSafe,

§

impl<K> Send for SVM<K>
where K: Send,

§

impl<K> Sync for SVM<K>
where K: Sync,

§

impl<K> Unpin for SVM<K>
where K: Unpin,

§

impl<K> UnwindSafe for SVM<K>
where K: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.