Machine Learning in Rust

Installation

To use this library in your Rust project, add the following line to your Cargo.toml file:

                [dependencies]
                machine_learning = "0.1.1"
            

Usage

For detailed documentation on how to use this library and to stay updated with the latest algorithms and methods being added, please visit this documentation regulary or you can watch the project on github here.

Linear Regression

The library currently includes an implementation of linear regression. Here is a sample usage:


use machine_learning::linreg::LinearRegression;

fn main() {
    let x = vec![1.0, 2.0, 3.0, 4.0, 5.0];
    let y = vec![2.0, 4.0, 5.0, 4.0, 5.0];

    let mut model = LinearRegression::new();
    match model.fit(&x, &y) {
        Ok(_) => {
            let prediction = model.predict(6.0);
            println!("Prediction for x=6.0: {}", prediction);

            let r_squared = model.get_r_squared();
            println!("R²: {}", r_squared);

            let residuals = model.get_residuals();
            println!("Residuals: {:?}", residuals);

            let mse = model.mean_squared_error();
            println!("Mean Squared Error: {}", mse);

            let rmse = model.root_mean_squared_error();
            println!("Root Mean Squared Error: {}", rmse);
        }
        Err(e) => println!("Error: {}", e),
    }
}
            

More algorithms and methods will be added regularly. Check the documentation site for updates.

Logistic Regression

The library currently includes an implementation of logistic regression. Here is a sample usage:


use machine_learning::logisticreg::LogisticRegression;

fn main() {
    // Sample data
    let xlarge = vec![
        vec![2.0],
        vec![3.0],
        vec![5.0],
        vec![6.0],
        vec![7.0],
        vec![10.0],
        vec![12.0],
        vec![14.0],
        vec![15.0]
    ];

    let y = vec![0.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0];

    // Create and train the logistic regression model
    let mut model = LogisticRegression::new(1);
    model.fit(&xlarge, &y, 0.1, 1000);

    // Predict probability of passing for a student who studies 8 hours
    let new_data = vec![vec![8.0]];
    let predicted_prob = model.predict_proba(&new_data);
    println!("Predicted probability of passing: {}", predicted_prob[0]);

    // Predict class
    let predicted_class = model.predict(&new_data);
    println!("Predicted class: {}", predicted_class[0]);
}
    

More algorithms and methods will be added regularly. Check the documentation site for updates.

Contributing

Contributions are welcome! Feel free to open an issue or submit a pull request. Your feedback and contributions are highly appreciated.

License

This project is licensed under the MIT License. See the LICENSE file for details.