Skip to content

Commit

Permalink
add: cargo format
Browse files Browse the repository at this point in the history
  • Loading branch information
KjetilIN committed Sep 23, 2024
1 parent 6fd9fcb commit cab742c
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 108 deletions.
27 changes: 7 additions & 20 deletions examples/perceptron_OR.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,40 +3,27 @@ use rustic_ml::perceptron::Perceptron;

fn main() {
//Initialize the perceptron with a learning rate of 0.1
let mut perceptron = Perceptron::init()
.learning_rate(0.1)
.bias(-1.0);
let mut perceptron = Perceptron::init().learning_rate(0.1).bias(-1.0);

// OR training data set
// This training dataset includes all possible versions of the OR
let x_train: Vec<(f64, f64)> = vec![
(0.0, 0.0),
(0.0, 1.0),
(1.0, 0.0),
(1.0, 1.0),
];
let x_train: Vec<(f64, f64)> = vec![(0.0, 0.0), (0.0, 1.0), (1.0, 0.0), (1.0, 1.0)];

// Corresponding labels (binary classification: 0 or 1)
let y_train = vec![0.0, 1.0, 1.0, 1.0];


// Train the perceptron using the fit method
// Alternatively use the fit() method to not log the accuracy over time
// Here with 10 epochs
// Here with 10 epochs
perceptron.fit_with_logging(&x_train, &y_train, 10);

// Test data
let test_data = vec![
(0.0, 0.0),
(1.0, 0.0),
(0.0, 1.0),
(1.0, 1.0),
];

// Test data
let test_data = vec![(0.0, 0.0), (1.0, 0.0), (0.0, 1.0), (1.0, 1.0)];

// Calculate the accuracy for the model
let accuracy = perceptron.calculate_accuracy(&test_data, &y_train);

// Print the accuracy
// Print the accuracy
println!("Model accuracy: {}%", accuracy);

// Print model details
Expand Down
24 changes: 6 additions & 18 deletions examples/perceptron_movie_classification.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
use rustic_ml::perceptron::Perceptron;

fn main() {
// Lets imagine we have the following data:
// Lets imagine we have the following data:
//
// ________________________________________________
// | Movie # | Alice | Bob | Profitable? |
Expand All @@ -21,34 +21,22 @@ fn main() {
// Our goal is to classify a profitable movie, based on two critics score.
// The score goes from 1-6.


//Initialize the perceptron with a learning rate of 0.1
let mut perceptron = Perceptron::init()
.learning_rate(1.0)
.bias(-1.0);

let mut perceptron = Perceptron::init().learning_rate(1.0).bias(-1.0);

// Setting up training data
let x_train: Vec<(f64, f64)> = vec![
(1.0, 1.0),
(4.0, 3.0),
(3.0, 5.0),
(5.0, 6.0),
(2.0, 3.0),
];
// Setting up training data
let x_train: Vec<(f64, f64)> = vec![(1.0, 1.0), (4.0, 3.0), (3.0, 5.0), (5.0, 6.0), (2.0, 3.0)];
let y_train = vec![0.0, 1.0, 1.0, 1.0, 0.0];


// Training until it learns the system.
// This is something we can do since we know the data is linearly separable. (Plot out the data and see for yourself).
// This is something we can do since we know the data is linearly separable. (Plot out the data and see for yourself).
// We can also use `fit_until_halt` if we don't want to log each epoch
perceptron.fit_until_halt_with_logging(&x_train, &y_train);


// Calculate the accuracy for the model
let accuracy = perceptron.calculate_accuracy(&x_train, &y_train);

// Print the accuracy
// Print the accuracy
println!("Model accuracy: {}%", accuracy);

// Print model details
Expand Down
Loading

0 comments on commit cab742c

Please sign in to comment.