简体   繁体   English

为什么不能在 php-ml 最小二乘中使用变量?

[英]why can't using variable in php-ml least square?

where in php-ml looping variable is always error array_unshift like this:在 php-ml 循环变量中的哪里总是错误 array_unshift 像这样:

Fatal error: Uncaught TypeError: array_unshift() expects parameter 1 to be array, string given in C:\xampp\htdocs\prediksi\assets\main system\vendor\php-ai\php-ml\src\Phpml\Regression\LeastSquares.php:100 Stack trace: #0 C:\xampp\htdocs\prediksi\assets\main system\vendor\php-ai\php-ml\src\Phpml\Regression\LeastSquares.php(100): array_unshift('[2018, 1, 1], [...', 1) #1 C:\xampp\htdocs\prediksi\assets\main system\vendor\php-ai\php-ml\src\Phpml\Regression\LeastSquares.php(81): Phpml\Regression\LeastSquares->getSamplesMatrix() #2 C:\xampp\htdocs\prediksi\assets\main system\vendor\php-ai\php-ml\src\Phpml\Regression\LeastSquares.php(42): Phpml\Regression\LeastSquares->computeCoefficients() #3 C:\xampp\htdocs\prediksi\admin\modul\show_predict.php(40): Phpml\Regression\LeastSquares->train(Array, Array) #4 C:\xampp\htdocs\prediksi\admin\index.php(85): require('C:\\xampp\\htdocs...') #5 {main} thrown in C:\xampp\htdocs\prediksi\assets\main system\vendor\php-ai\php-ml\src\Phpml\Regression\LeastSquares.php on line 100

why this happen?为什么会这样?

there is my code to looping:有我的循环代码:

$query1 = "SELECT * FROM master_data";

$result1 = mysqli_query($conn, $query1);

$data1 = "";

while($row1 = mysqli_fetch_array($result1)){
    $TH=number_clean($row1['TH']);
    $B=$row1['B'];

$data1 .= "[".$row1["T"].", ".$B.", ".$TH."], ";

}

$data1 = substr($data1, 0, -2);

$query2 = "SELECT * from master_data";

$result2 = mysqli_query($conn, $query2);

$data2 = "";

while($row2 = mysqli_fetch_array($result2)){

  $data2 .= "".$row2["Penjualan"].", ";

}

$data2 = substr($data2, 0, -2); 

$tgl=$_POST['tgl'];
$dat1=$_POST['data1'];
$dat2=$_POST['data2'];

$samples = [$dat1];
$targets = [$dat2];

$regression = new LeastSquares();
$regression->train($samples, $targets);
$result = round($regression->predict([$t, $b, $th]));
echo $t."<br>";
echo $b."<br>";
echo $th."<br>";
echo $result."<br>";

and the result is in up of my question结果是我的问题

when i use manual (insert one by one) it works当我使用手册(一一插入)时,它可以工作

why this happen?为什么会这样?

The issue is when you are calling array_unshift you are passing a string, not a variable.问题是当您调用 array_unshift 时,您传递的是字符串,而不是变量。 Looking at your function you should change it to:查看您的 function 您应该将其更改为:

private function getSamplesMatrix() { 
    $samples = []; 
    array_unshift($samples, 1); 
    return new Matrix($samples); 
} 

Remove the foreach loop.删除 foreach 循环。

However I'm confused by your code as you are performing array_unshift on an empty array.但是,当您在空数组上执行 array_unshift 时,我对您的代码感到困惑。 You can achieve the same thing with:您可以通过以下方式实现相同的目标:

$samples = array(1);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM