我在PHP中实现了线性回归的代码,其目的是为每个客户提供交货日期(无论每个客户在数据库中有多少交货日期),然后将这些交货日期(转换为天数)用作线性回归方程作为y参数,最终预测未来的交货日期/订单日期,并且基于该预测和与当前日期的比较,系统将采取诸如发送推送通知提醒之类的一些动作。我的实现代码如下:
$conn = mysql_connect($dbhost,$dbuser,$dbpass) or die (mysql_error());
mysql_select_db($db, $conn) or die(mysql_error());
//Outer Loop to save all eligable customer ID's
$idarray=array();
$firstloop=mysql_query("Select id FROM ordering GROUP BY id",$conn);
$number_of_rows_initial=mysql_num_rows($firstloop);
while (($row = mysql_fetch_array($firstloop, MYSQL_ASSOC)) !== false){
$idarray[] = $row; // add the row in to the results (data) array
// print_r($idarray);
}
for($counter=0; $counter<$number_of_rows_initial; $counter++)
{
$id=$idarray[$counter]['id'];
//First confirm if user is already registered via website
$result = mysql_query("SELECT TO_DAYS(date) FROM ordering WHERE id=$id",$conn);
$num_rows = mysql_num_rows($result);
// mysql_query($result,$conn) or die(mysql_error();
//echo $num_rows;
$data = array();
$days=array();
$xAxis=array();
$ldate=array();
while (($row = mysql_fetch_array($result, MYSQL_ASSOC)) !== false){
$data[] = $row; // add the row in to the results (data) array
}
// print_r($data); // print result
//This part is working now!!!!
for ($x = $num_rows-1 ; $x > 0; $x--) {
$days[$x] = $data[$x]['TO_DAYS(date)'] -$data[$x-1]['TO_DAYS(date)'];
$xAxis[$x]=$x;
}
$days[0]=30;
print_r("<br />",$days);
//print_r($xAxis);
//my implementation of regression calculation
// calculate sums
$x_sum = (array_sum($xAxis));
$y_sum = array_sum($days);
echo "<br />","Customer ID is: ",$id,"<br />";
echo "Sum of X Axis: ",$x_sum, "<br />";
echo "Sum of Y Axis: ",$y_sum,"<br />" ;
$xx_sum = 0;
$xy_sum = 0;
for($i = 0; $i < $num_rows; $i++) {
$xy_sum+=($xAxis[$i]*$days[$i]);
$xx_sum+=($xAxis[$i]*$xAxis[$i]);
}
// calculate slope
$m = (($num_rows * $xy_sum) - ($x_sum * $y_sum)) / (($num_rows * $xx_sum) - ($x_sum * $x_sum));
// calculate intercept
$b = ($y_sum - ($m * $x_sum)) / $num_rows;
$predict=($m*($num_rows+1))+$b;
echo "The Slope of the Representative line is: ",$m,"<br />";
echo "The Y intercept is ",$b,"<br />";
echo "Predicted next date of delivery in days (from last delivery date) is: ", $predict,"<br />";
//Final Date Calculation
$lastdate=mysql_query("SELECT TO_DAYS(date) FROM ordering WHERE id=$id",$conn);
$num_rows_date = mysql_num_rows($lastdate);
while (($row_date = mysql_fetch_array($lastdate, MYSQL_ASSOC)) !== false){
$ldate[] = $row_date; // add the row in to the results (data) array
}
echo "Total number of records for the given id are: ",$num_rows_date, "<br />";
$ldatefinal=$ldate[$num_rows_date-1]['TO_DAYS(date)'];
echo "Last date in the records for the given ID is ",$ldatefinal,"<br />";
echo "Last date + Prediction days = ",$ldatefinal+$predict,"<br />";
$currentdate=mysql_query("SELECT TO_DAYS(CURDATE())",$conn);
$dcrow = mysql_fetch_assoc($currentdate);
//$dhold=$dcrow['TO_DAYS(CURDATE())'];
//echo "Current Date is: ",$dcrow['TO_DAYS(CURDATE())'],"<br /" ;
//UPON EXECUTION NOTHING BELOW GETS ExEcuted ??
//echo "Last date + Predicted days = ";
if (( (($ldatefinal+$predict) - $dcrow['TO_DAYS(CURDATE())']) <3 )&& $predict>0){
echo "Time to Order<br />","</br />";
}else if((( (($ldatefinal+$predict) - $dcrow['TO_DAYS(CURDATE())']) >3 && $predict>0) )){
echo "You Got Gas!!<br />","</br />";
}else{
echo "Linear Regression not Suitable for this Record ID","<br />";
}
}
mysql_close($conn);
除了预测带有负数的时候,每件事情看起来都很完美,我现在选择通过使用不适合此记录ID的“回声”线性回归来忽略这些,“
“;”但我相信必须有更好的方法来适应这种情况,我只是不确定如何在我的情况下,特别是使用日期格式。任何帮助将不胜感激。我必须说这个论坛,当然它的用户对我来说过去他们的时间和知识非常友好和慷慨,希望你们这次也能指出我正确的方向。 Hamood