Struct Ridge

Source
pub struct Ridge { /* private fields */ }
Expand description

Ridge regression model.

Implementations§

Source§

impl Ridge

Source

pub fn new(n_x: u64, n_y: u64, beta: f64) -> Self

Create a new Ridge regression model. ‘n_x’ is the number of input variables and ‘n_y’ is the number of output variables. ‘beta’ is the regularization parameter.

Examples found in repository?
examples/ridge.rs (line 9)
5fn main() {
6    let x = na::DVector::from_vec(vec![1.0, 2.0, 3.0, 4.0]);
7    let d = na::DVector::from_vec(vec![5.0, 6.0]);
8
9    let mut ridge = Ridge::new(4, 2, 0.1);
10    ridge.set_data(&x, &d);
11    let weight = ridge.fit();
12
13    println!("{}", ridge);
14    println!("Weight:\n{}", weight);
15}
More examples
Hide additional examples
examples/xor.rs (line 37)
12fn main() {
13    let (train_input, train_expected_output) = data_gen(TRAIN_STEP, RANDOM_SEED);
14    let (test_input, test_expected_output) = data_gen(TEST_STEP, TEST_RANDOM_SEED);
15
16    let path = format!("{}/examples/graph", env!("CARGO_MANIFEST_DIR"));
17
18    let n_u = train_input.first().unwrap().len() as u64;
19    let n_y = train_expected_output.first().unwrap().len() as u64;
20
21    let mut model = EchoStateNetwork::new(
22        n_u,
23        n_y,
24        N_X,
25        0.1,
26        1.0,
27        0.9,
28        |x| x.tanh(),
29        None,
30        None,
31        1.0,
32        |x| x.clone_owned(),
33        |x| x.clone_owned(),
34        false,
35    );
36
37    let mut optimizer = Ridge::new(N_X, n_y, BETA);
38
39    model.train(&train_input, &train_expected_output, &mut optimizer);
40
41    let estimated_output = model.estimate(&test_input);
42
43    let (bits_l2_error, bits_l1_error) =
44        get_bits_error_rate(estimated_output.clone(), test_expected_output.clone());
45    let (l2_error, l1_error) =
46        get_error_rate(estimated_output.clone(), test_expected_output.clone());
47    println!("Bits Mean Squared Error: {}", bits_l2_error);
48    println!("Bits Mean Absolute Error: {}", bits_l1_error);
49    println!("Mean Squared Error: {}", l2_error);
50    println!("Mean Absolute Error: {}", l1_error);
51
52    let y_estimated = estimated_output.iter().map(|x| x[0]).collect::<Vec<f64>>();
53    let y_expected = test_expected_output
54        .clone()
55        .into_iter()
56        .flatten()
57        .collect::<Vec<f64>>();
58
59    plotter::plot(
60        "XOR",
61        (0..TEST_STEP).map(|v| v as f64).collect::<Vec<f64>>(),
62        vec![y_expected, y_estimated],
63        vec!["Expected".to_string(), "Output".to_string()],
64        Some(&path),
65    )
66    .unwrap();
67
68    write_as_serde(
69        model,
70        optimizer,
71        &train_input,
72        &train_expected_output,
73        &test_input,
74        &test_expected_output,
75        estimated_output,
76        None,
77    );
78}
Source

pub fn set_data(&mut self, x: &DVector<f64>, d: &DVector<f64>)

Update the internal state of the Ridge regression model. ‘x’ is the input vector (explanatory variable) and ‘d’ is the output vector (response variable).

Examples found in repository?
examples/ridge.rs (line 10)
5fn main() {
6    let x = na::DVector::from_vec(vec![1.0, 2.0, 3.0, 4.0]);
7    let d = na::DVector::from_vec(vec![5.0, 6.0]);
8
9    let mut ridge = Ridge::new(4, 2, 0.1);
10    ridge.set_data(&x, &d);
11    let weight = ridge.fit();
12
13    println!("{}", ridge);
14    println!("Weight:\n{}", weight);
15}
Source

pub fn fit(&self) -> DMatrix<f64>

Fit the Ridge regression model and return the weight matrix.

Examples found in repository?
examples/ridge.rs (line 11)
5fn main() {
6    let x = na::DVector::from_vec(vec![1.0, 2.0, 3.0, 4.0]);
7    let d = na::DVector::from_vec(vec![5.0, 6.0]);
8
9    let mut ridge = Ridge::new(4, 2, 0.1);
10    ridge.set_data(&x, &d);
11    let weight = ridge.fit();
12
13    println!("{}", ridge);
14    println!("Weight:\n{}", weight);
15}

Trait Implementations§

Source§

impl Clone for Ridge

Source§

fn clone(&self) -> Ridge

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Ridge

Source§

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

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

impl<'de> Deserialize<'de> for Ridge

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl Display for Ridge

Source§

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

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

impl Serialize for Ridge

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more

Auto Trait Implementations§

§

impl Freeze for Ridge

§

impl RefUnwindSafe for Ridge

§

impl Send for Ridge

§

impl Sync for Ridge

§

impl Unpin for Ridge

§

impl UnwindSafe for Ridge

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<SS, SP> SupersetOf<SS> for SP
where SS: SubsetOf<SP>,

Source§

fn to_subset(&self) -> Option<SS>

The inverse inclusion map: attempts to construct self from the equivalent element of its superset. Read more
Source§

fn is_in_subset(&self) -> bool

Checks if self is actually part of its subset T (and can be converted to it).
Source§

fn to_subset_unchecked(&self) -> SS

Use with care! Same as self.to_subset but without any property checks. Always succeeds.
Source§

fn from_subset(element: &SS) -> SP

The inclusion map: converts self to the equivalent element of its superset.
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
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.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,