数组的线性拟合或者叫线性回归,判断数据趋势
By
admin
at 2019-05-10 • 0人收藏 • 1654人看过
import console; var shuju = {}; var linearRegression = function(y,x){ var lr = {}; var n = #y; var sum_x = 0; var sum_y = 0; var sum_xy = 0; var sum_xx = 0; var sum_yy = 0; for(i=1;n;1){ sum_x += x[i]; sum_y += y[i]; sum_xy += (x[i] * y[i]); sum_xx += (x[i] * x[i]); sum_yy += (y[i] * y[i]); } lr['倾斜度'] = (n * sum_xy - sum_x * sum_y) / (n * sum_xx - sum_x * sum_x); lr['差值'] = (sum_y - lr.倾斜度 * sum_x) / n; lr['拟合误差'] = ((n * sum_xy - sum_x * sum_y) / math.sqrt((n * sum_xx - sum_x * sum_x) * (n * sum_yy - sum_y * sum_y))) ** 2; return lr; } var ret = linearRegression( {20;36;43;81;4;84},{33;60;4;82;89;83} ); console.log("F(X) = "++ret.倾斜度++"*X"++" + "+ret.差值,"误差为:"++ret.拟合误差); console.pause(true);
1 个回复 | 最后更新于 2019-05-10
登录后方可回帖
如果X轴是连续的数字, 而且你只需要的是趋势,那么可以省略x参数,函数如下: