2

My math is very week and now still figuring how to achieve this with PHP. I was using excel TREND() function. Let's say I have 2 lists of known values

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

And now I need to know the value y when the x = 428. By using excel TREND() function, I get 0.055 as the value y.

Anyone can show me how PHP handle this kind of math questions?
Any help would be appreciated. Thank you.

4

1 回答 1

8

ExcelTREND()使用最小二乘法

数学很难,所以我会使用图书馆来完成繁重的工作。在这种情况下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]);

输出:

0.054973333333333235

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


你当然可以round()number_format()你希望的结果,例如:

echo number_format($regression->predict([428]), 3);

给出:

0.055
于 2016-09-24T14:22:01.047 回答