dongxie548548 2015-07-29 14:15
浏览 94
已采纳

预测数据趋势[关闭]

Good morning, Stack. I'm trying to figure out a way to predict trends in data.. I'm wondering if there is a better way to do this.. Are there any built-in functions or libraries I can look into?

Here's what I've got: (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);
  • 写回答

1条回答 默认 最新

  • duancheng3042 2015-07-29 15:08
    关注

    The algorithm is completely broken because it is calculating the average increase of the array keys (the year values, 1999, 2000 etc) rather than the array values (population), so the result is always 1.

    This is masked by the fact your sample population data always increases by one, if you had added more variation you would probably have spotted the error. To fix:

    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);
    }
    

    In more general terms the algorithm is extremely simplistic in that it will predict a flat increase / decrease.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路