0

早上好,斯塔克。我正在尝试找出一种预测数据趋势的方法。我想知道是否有更好的方法可以做到这一点。我可以查看任何内置函数或库吗?

这就是我所拥有的:(http://3v4l.org/RGU3i

$PopulationOfTexas = array(
    1999 => 20.56, // in millions
    2000 => 21.56,
    2001 => 22.56,
    2002 => 23.56
);

//generate an array sohwing the difference in each year compared to the previous year
$differneces = array();
$lastyear = null;
foreach($PopulationOfTexas as $k=>$v){
    if(empty($lastyear)){$lastyear = $k; continue;}
    $differneces[$k] = $k - $lastyear;
    $lastyear = $k;

    //use this later
    $lastitem = array("year"=>$k, "data"=>$v);
}

//get the average difference per year
$count = 0;
$total = 0;
foreach($differneces as $k=>$v){
    $count++;
    $total += $v;
}

$average = number_format(($total/$count), 2);

//make a prediction
$predictions = array();
for($i=0;$i<5;$i++){
    $year = isset($year) ? $year+1 : $lastitem["year"]+1;
    $prediction = isset($prediction) ? $prediction+floatval($average) : $lastitem["data"]+floatval($average);
    $predictions[$year] = $prediction;
}

print_r($predictions);
4

1 回答 1

1

该算法完全被破坏了,因为它计算的是数组键(年份值、1999、2000 等)而不是数组值(人口)的平均增量,因此结果始终为 1。

这被您的样本总体数据总是增加一的事实所掩盖,如果您添加了更多的变化,您可能会发现错误。修理:

foreach($PopulationOfTexas as $k=>$v){
    if(empty($lastyear)){$lastyear = $v; continue;}
    $differneces[$k] = $v - $lastyear;
    $lastyear = $v;

    //use this later
    $lastitem = array("year"=>$k, "data"=>$v);
}

更一般地说,该算法非常简单,因为它将预测一个平坦的增加/减少。

于 2015-07-29T15:08:54.147 回答