简体   繁体   中英

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

where in php-ml looping variable is always error array_unshift like this:

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. Looking at your function you should change it to:

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

Remove the foreach loop.

However I'm confused by your code as you are performing array_unshift on an empty array. You can achieve the same thing with:

$samples = array(1);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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