#regression #linear #value #predict #data #predicted #000

bin+lib linear-regression

Library for linear regression

1 unstable release

0.1.0 Mar 3, 2023

#685 in Science

MIT/Apache

6KB
52 lines

Linear-Regression-ML

Linear regression algorithm that predicts future values depending on existing data

use linear_regression::{linear_regression, predict}

fn main() {
  /* Some data */
  let data: Vec<(f32, f32)> = vec![
    (17.9, 2013.0),
    (17.63, 2014.0),
    (14.95, 2015.0),
    (15.14, 2016.0),
    (16.24, 2017.0),
    (17.6, 2018.0),
    (17.47, 2019.0),
    (15.84, 2020.0),
    (18.7, 2021.0)
  ];

  for price in &data {
      println!("Year: {}, GDP = ${:.3}B", price.1, price.0);
  }

  // Linear regression prints the equation and returns k and b
  let eq = linear_regression(&data);

  // Test cases for different x values
  predict(&eq, 2022.0);
}

Example:

  • Vertices data: [
     (0.0, 2.1),
     (1.0, 1.92),
     (2.0, 1.84),
     (3.0, 1.71),
     (4.0, 1.64)
    ]
  • Regression line: y = -0.113x + 2.068

  • Predicted value for 5.000 is 1.503
  • Predicted value for 6.000 is 1.390
  • Predicted value for 7.000 is 1.277
  • Predicted value for 8.000 is 1.164
  • Predicted value for 9.000 is 1.051

No runtime deps