在php-ml中,循环变量总是error 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
为什么会这样?
下面是我的循环代码:
$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>";
结果在我的问题中
当我使用手动(插入一个接一个)它的工作
为什么会这样?
2条答案
按热度按时间myss37ts1#
问题是当你调用array_unshift时,你传递的是一个字符串,而不是一个变量。看看你的函数,你应该把它改为:
删除foreach循环。
然而,我对你的代码感到困惑,因为你在一个空数组上执行array_unshift。您可以通过以下方式实现相同的功能:
13z8s7eq2#
您可以在PHP-ML最小二乘算法中使用变量。您只需要确保传递给算法的数据是预期的格式。
以下是如何在PHP-ML中使用最小二乘算法的变量的示例:
在本例中,我们首先使用Laravel查询构建器从数据库中检索数据。然后,我们通过创建一个数组数组将数据转换为LeastSquares算法期望的格式,其中每个内部数组表示单个数据点,并包含该点的x和y值。然后我们将这些数据传递给LeastSquares算法的train()方法来训练模型。
在训练模型之后,我们可以分别使用getIntercept()和getCoefficients()方法获得回归线的截距和斜率。
请注意,在本例中,我们假设数据库中的x_column和y_column字段包含可用作最小二乘算法输入的数值。如果您的数据格式不同,则可能需要执行额外的处理以使其达到算法所需的格式。