PHP计算趋势线像excel TREND函数

cro*_*sRT 1 php excel trendline

我的数学是一周,现在仍然在想如何用PHP实现这一目标.我使用的是excel TREND()函数.假设我有2个已知值列表

| known_x_values | known_y_values | |----------------|----------------| | 323 | 0.038 | | 373 | 0.046 | | 423 | 0.053 | | 473 | 0.062 | | 523 | 0.071 | | 573 | 0.080 |

现在我需要知道x = 428时的值y.通过使用excel TREND()函数,我得到0.055作为值y.

任何人都可以告诉我PHP如何处理这种数学问题?
任何帮助,将不胜感激.谢谢.

use*_*918 6

Excel TREND()使用最小二乘法.

数学很难,所以我会用一个库来完成繁重的工作.在这种情况下php-ai/php-ml.

例:

use Phpml\Regression\LeastSquares;

$x = [[323], [373], [423], [473], [523], [573]];
$y = [.038, .046, .053, .062, .071, .080];

$regression = new LeastSquares();
$regression->train($x, $y);
echo $regression->predict([428]);
Run Code Online (Sandbox Code Playgroud)

输出:

0.054973333333333235
Run Code Online (Sandbox Code Playgroud)

根据您的精度设置,输出可能略有不同.


您当然可以根据自己的意愿round()number_format()结果,例如:

echo number_format($regression->predict([428]), 3);
Run Code Online (Sandbox Code Playgroud)

得到:

0.055
Run Code Online (Sandbox Code Playgroud)