pub struct Vector<T> { /* private fields */ }Expand description
The Vector struct.
Can be instantiated with any type.
Implementations§
Source§impl<T> Vector<T>
impl<T> Vector<T>
Sourcepub fn new<U>(data: U) -> Vector<T>
pub fn new<U>(data: U) -> Vector<T>
Constructor for Vector struct.
Requires the vector data.
§Examples
use rulinalg::vector::Vector;
let vec = Vector::new(vec![1.0,2.0,3.0,4.0]);Examples found in repository?
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}Sourcepub fn data(&self) -> &Vec<T>
pub fn data(&self) -> &Vec<T>
Returns a non-mutable reference to the underlying data.
Examples found in repository?
44fn main() {
45 println!("K-Means clustering example:");
46
47 const SAMPLES_PER_CENTROID: usize = 2000;
48
49 println!("Generating {0} samples from each centroids:",
50 SAMPLES_PER_CENTROID);
51 // Choose two cluster centers, at (-0.5, -0.5) and (0, 0.5).
52 let centroids = Matrix::new(2, 2, vec![-0.5, -0.5, 0.0, 0.5]);
53 println!("{}", centroids);
54
55 // Generate some data randomly around the centroids
56 let samples = generate_data(¢roids, SAMPLES_PER_CENTROID, 0.4);
57
58 // Create a new model with 2 clusters
59 let mut model = KMeansClassifier::new(2);
60
61 // Train the model
62 println!("Training the model...");
63 // Our train function returns a Result<(), E>
64 model.train(&samples).unwrap();
65
66 let centroids = model.centroids().as_ref().unwrap();
67 println!("Model Centroids:\n{:.3}", centroids);
68
69 // Predict the classes and partition into
70 println!("Classifying the samples...");
71 let classes = model.predict(&samples).unwrap();
72 let (first, second): (Vec<usize>, Vec<usize>) = classes.data().iter().partition(|&x| *x == 0);
73
74 println!("Samples closest to first centroid: {}", first.len());
75 println!("Samples closest to second centroid: {}", second.len());
76}Source§impl<T> Vector<T>where
T: Copy + PartialOrd,
impl<T> Vector<T>where
T: Copy + PartialOrd,
Sourcepub fn argmax(&self) -> (usize, T)
pub fn argmax(&self) -> (usize, T)
Find the argmax of the Vector.
Returns the index of the largest value in the vector.
§Examples
use rulinalg::vector::Vector;
let a = Vector::new(vec![1.0,2.0,0.0,5.0]);
let b = a.argmax();
assert_eq!(b.0, 3);
assert_eq!(b.1, 5.0);Trait Implementations§
Source§impl<'a, T> AddAssign<&'a T> for Vector<T>
Performs
addition
assignment between a vector and a scalar.
impl<'a, T> AddAssign<&'a T> for Vector<T>
Performs addition assignment between a vector and a scalar.
Source§fn add_assign(&mut self, _rhs: &T)
fn add_assign(&mut self, _rhs: &T)
+= operation. Read moreSource§impl<'a, T> AddAssign<&'a Vector<T>> for Vector<T>
Performs elementwise
addition
assignment between two vectors.
impl<'a, T> AddAssign<&'a Vector<T>> for Vector<T>
Performs elementwise addition assignment between two vectors.
Source§fn add_assign(&mut self, _rhs: &Vector<T>)
fn add_assign(&mut self, _rhs: &Vector<T>)
+= operation. Read moreSource§impl<T> AddAssign<T> for Vector<T>
Performs
addition
assignment between a vector and a scalar.
impl<T> AddAssign<T> for Vector<T>
Performs addition assignment between a vector and a scalar.
Source§fn add_assign(&mut self, _rhs: T)
fn add_assign(&mut self, _rhs: T)
+= operation. Read moreSource§impl<T> AddAssign for Vector<T>
Performs elementwise
addition
assignment between two vectors.
impl<T> AddAssign for Vector<T>
Performs elementwise addition assignment between two vectors.
Source§fn add_assign(&mut self, _rhs: Vector<T>)
fn add_assign(&mut self, _rhs: Vector<T>)
+= operation. Read moreSource§impl<'a, T> DivAssign<&'a T> for Vector<T>
Performs
division
assignment between a vector and a scalar.
impl<'a, T> DivAssign<&'a T> for Vector<T>
Performs division assignment between a vector and a scalar.
Source§fn div_assign(&mut self, _rhs: &T)
fn div_assign(&mut self, _rhs: &T)
/= operation. Read moreSource§impl<T> DivAssign<T> for Vector<T>
Performs
division
assignment between a vector and a scalar.
impl<T> DivAssign<T> for Vector<T>
Performs division assignment between a vector and a scalar.
Source§fn div_assign(&mut self, _rhs: T)
fn div_assign(&mut self, _rhs: T)
/= operation. Read moreSource§impl<'a, T> IntoIterator for &'a Vector<T>
impl<'a, T> IntoIterator for &'a Vector<T>
Source§impl<T> IntoIterator for Vector<T>
impl<T> IntoIterator for Vector<T>
Source§impl<'a, T> MulAssign<&'a T> for Vector<T>
Performs
multiplication
assignment between a vector and a scalar.
impl<'a, T> MulAssign<&'a T> for Vector<T>
Performs multiplication assignment between a vector and a scalar.
Source§fn mul_assign(&mut self, _rhs: &T)
fn mul_assign(&mut self, _rhs: &T)
*= operation. Read moreSource§impl<T> MulAssign<T> for Vector<T>
Performs
multiplication
assignment between a vector and a scalar.
impl<T> MulAssign<T> for Vector<T>
Performs multiplication assignment between a vector and a scalar.
Source§fn mul_assign(&mut self, _rhs: T)
fn mul_assign(&mut self, _rhs: T)
*= operation. Read moreSource§impl<'a, T> SubAssign<&'a T> for Vector<T>
Performs
subtraction
assignment between a vector and a scalar.
impl<'a, T> SubAssign<&'a T> for Vector<T>
Performs subtraction assignment between a vector and a scalar.
Source§fn sub_assign(&mut self, _rhs: &T)
fn sub_assign(&mut self, _rhs: &T)
-= operation. Read moreSource§impl<'a, T> SubAssign<&'a Vector<T>> for Vector<T>
Performs elementwise
subtraction
assignment between two vectors.
impl<'a, T> SubAssign<&'a Vector<T>> for Vector<T>
Performs elementwise subtraction assignment between two vectors.
Source§fn sub_assign(&mut self, _rhs: &Vector<T>)
fn sub_assign(&mut self, _rhs: &Vector<T>)
-= operation. Read moreSource§impl<T> SubAssign<T> for Vector<T>
Performs
subtraction
assignment between a vector and a scalar.
impl<T> SubAssign<T> for Vector<T>
Performs subtraction assignment between a vector and a scalar.
Source§fn sub_assign(&mut self, _rhs: T)
fn sub_assign(&mut self, _rhs: T)
-= operation. Read moreSource§impl<T> SubAssign for Vector<T>
Performs elementwise
subtraction
assignment between two vectors.
impl<T> SubAssign for Vector<T>
Performs elementwise subtraction assignment between two vectors.
Source§fn sub_assign(&mut self, _rhs: Vector<T>)
fn sub_assign(&mut self, _rhs: Vector<T>)
-= operation. Read moreSource§impl<C: Criterion> SupModel<Matrix<f64>, Vector<f64>> for GenLinearModel<C>
Supervised model trait for the GLM.
impl<C: Criterion> SupModel<Matrix<f64>, Vector<f64>> for GenLinearModel<C>
Supervised model trait for the GLM.
Predictions are made from the model by computing g^-1(Xb).
The model is trained using Iteratively Re-weighted Least Squares.
Source§impl SupModel<Matrix<f64>, Vector<f64>> for LinRegressor
impl SupModel<Matrix<f64>, Vector<f64>> for LinRegressor
Source§fn train(
&mut self,
inputs: &Matrix<f64>,
targets: &Vector<f64>,
) -> LearningResult<()>
fn train( &mut self, inputs: &Matrix<f64>, targets: &Vector<f64>, ) -> LearningResult<()>
Train the linear regression model.
Takes training data and output values as input.
§Examples
use rusty_machine::learning::lin_reg::LinRegressor;
use rusty_machine::linalg::Matrix;
use rusty_machine::linalg::Vector;
use rusty_machine::learning::SupModel;
let mut lin_mod = LinRegressor::default();
let inputs = Matrix::new(3,1, vec![2.0, 3.0, 4.0]);
let targets = Vector::new(vec![5.0, 6.0, 7.0]);
lin_mod.train(&inputs, &targets).unwrap();Source§impl<A> SupModel<Matrix<f64>, Vector<f64>> for LogisticRegressor<A>where
A: OptimAlgorithm<BaseLogisticRegressor>,
impl<A> SupModel<Matrix<f64>, Vector<f64>> for LogisticRegressor<A>where
A: OptimAlgorithm<BaseLogisticRegressor>,
Source§fn train(
&mut self,
inputs: &Matrix<f64>,
targets: &Vector<f64>,
) -> LearningResult<()>
fn train( &mut self, inputs: &Matrix<f64>, targets: &Vector<f64>, ) -> LearningResult<()>
Train the logistic regression model.
Takes training data and output values as input.
§Examples
use rusty_machine::learning::logistic_reg::LogisticRegressor;
use rusty_machine::linalg::Matrix;
use rusty_machine::linalg::Vector;
use rusty_machine::learning::SupModel;
let mut logistic_mod = LogisticRegressor::default();
let inputs = Matrix::new(3,2, vec![1.0, 2.0, 1.0, 3.0, 1.0, 4.0]);
let targets = Vector::new(vec![5.0, 6.0, 7.0]);
logistic_mod.train(&inputs, &targets).unwrap();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.
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.