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>
impl<K: Kernel> SVM<K>
Sourcepub fn new(ker: K, lambda: f64) -> SVM<K>
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)
17fn main() {
18 println!("Sign learner sample:");
19
20 println!("Training...");
21 // Training data
22 let inputs = Matrix::new(11, 1, vec![
23 -0.1, -2., -9., -101., -666.7,
24 0., 0.1, 1., 11., 99., 456.7
25 ]);
26 let targets = Vector::new(vec![
27 -1., -1., -1., -1., -1.,
28 1., 1., 1., 1., 1., 1.
29 ]);
30
31 // Trainee
32 let mut svm_mod = SVM::new(HyperTan::new(100., 0.), 0.3);
33 // Our train function returns a Result<(), E>
34 svm_mod.train(&inputs, &targets).unwrap();
35
36 println!("Evaluation...");
37 let mut hits = 0;
38 let mut misses = 0;
39 // Evaluation
40 // Note: We could pass all input values at once to the `predict` method!
41 // Here, we use a loop just to count and print logs.
42 for n in (-1000..1000).filter(|&x| x % 100 == 0) {
43 let nf = n as f64;
44 let input = Matrix::new(1, 1, vec![nf]);
45 let out = svm_mod.predict(&input).unwrap();
46 let res = if out[0] * nf > 0. {
47 hits += 1;
48 true
49 } else if nf == 0. {
50 hits += 1;
51 true
52 } else {
53 misses += 1;
54 false
55 };
56
57 println!("{} -> {}: {}", Matrix::data(&input)[0], out[0], res);
58 }
59
60 println!("Performance report:");
61 println!("Hits: {}, Misses: {}", hits, misses);
62 let hits_f = hits as f64;
63 let total = (hits + misses) as f64;
64 println!("Accuracy: {}", (hits_f / total) * 100.);
65}
Trait Implementations§
Source§impl Default for SVM<SquaredExp>
The default Support Vector Machine.
impl Default for SVM<SquaredExp>
The default Support Vector Machine.
The defaults are:
ker
=SquaredExp::default()
lambda
=0.3
optim_iters
=100
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more