1 Star 0 Fork 0

yutiansut / Peroxide

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
reg.rs 1.34 KB
一键复制 编辑 原始数据 按行查看 历史
Axect 提交于 2020-05-30 20:29 . fuga complete
use crate::structure::polynomial::Polynomial;
/// Simple Least Square 2D
///
/// # Type
///
/// (Vec<f64>, Vec<f64>) -> Polynomial
///
/// # Examples
/// ```
/// #[macro_use]
/// extern crate peroxide;
/// use peroxide::fuga::*;
///
/// fn main() {
/// let a = c!(1,2,3,4,5);
/// let b = c!(1.2, 1.8, 3.2, 3.8, 5.0);
/// let ls = least_square(a, b);
/// ls.print(); // 0.96x + 0.1200
/// }
/// ```
pub fn least_square(node_x: Vec<f64>, node_y: Vec<f64>) -> Polynomial {
let l = node_x.len();
assert_eq!(l, node_y.len());
let mut x_bar = 0f64;
let mut t_bar = 0f64;
let mut xt_bar = 0f64;
let mut x_sq_bar = 0f64;
for i in 0..l {
let x = node_x[i];
let t = node_y[i];
x_bar += x;
t_bar += t;
xt_bar += x * t;
x_sq_bar += x * x;
}
x_bar /= l as f64;
t_bar /= l as f64;
xt_bar /= l as f64;
x_sq_bar /= l as f64;
let x_bar_sq = x_bar.clone() * x_bar.clone();
let x_bar_t_bar = x_bar.clone() * t_bar.clone();
let w1 = (xt_bar - x_bar_t_bar) / (x_sq_bar - x_bar_sq);
let w0 = t_bar - w1 * x_bar;
Polynomial::new(vec![w1, w0])
}
// Polynomial Regression
//pub fn poly_reg(node_x: Vec<f64>, node_y: Vec<f64>) -> Polynomial {
// let n = node_x.len();
// assert_eq!(n, node_y.len());
// let a = matrix(vec![1f64; n], n, 1, Col);
//
//}
1
https://gitee.com/yutiansut/Peroxide.git
git@gitee.com:yutiansut/Peroxide.git
yutiansut
Peroxide
Peroxide
master

搜索帮助

53164aa7 5694891 3bd8fe86 5694891