简体   繁体   English

如何准备从 php 中的 sql 输出的数据以运行 PHP 机器学习库

[英]How to prepare data outputted from sql in php to run through the PHP machine learning Library

I am playing around with the LeastSquares regression algorithm from the PHP ML library.我正在使用 PHP ML 库中的最小二乘回归算法。 I can successfully run the code with the example given but when I try to run data from my database I get a blank screen returned and no error logs on the server.我可以使用给出的示例成功运行代码,但是当我尝试从我的数据库运行数据时,我得到一个空白屏幕,并且服务器上没有错误日志。

Here is the working example php file from the ML library:这是来自 ML 库的工作示例 php 文件:

$samples = [[50000, 2017], [40000, 2015], [75000, 2014], [30000, 2016]];   
$targets = [15000,13000,14500,14000];

regression = new LeastSquares();
$regression->train($samples, $targets);

echo $regression->predict([60000, 2015]);
// returns 4094.82

Here is myScript这是我的脚本

$sql = "SELECT price, mileage, year From table";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
    while($row = $result->fetch_assoc()) {

        // The output I am looking for is [[mileage, year],[mileage, year]..] & [price, price,..]

        $mileage = $row['mileage'];

        $year = $row['year'];
   
        $price = $row['price'];

    }
}


regression = new LeastSquares();
$regression->train($samples, $targets);

echo $regression->predict([60000, 2015]);

I am trying to recreate the $samples and $targets variables with values from my DB table but I am not preparing it correctly.I tried preg_replace to create a comma separated string but that didnt work, I suspect it's because it's a string and not integer values but Im not exactly sure.我正在尝试使用我的数据库表中的值重新创建 $samples 和 $targets 变量,但我没有正确准备它。我尝试 preg_replace 创建一个逗号分隔的字符串但没有奏效,我怀疑这是因为它是一个字符串而不是 integer值,但我不完全确定。 I wrote the example shown so might be a syntax error but im just trying to figure out the correct way to prepare the array values like the ML library gives.我编写了显示的示例,因此可能是语法错误,但我只是想找出像 ML 库提供的那样准备数组值的正确方法。

When I do当我做

var_dump($samples);

var_dump($targets);

I get我明白了

array(4) { [0]=> array(2) { [0]=> int(50000) [1]=> int(2017) } [1]=> array(2) { [0]=> int(40000) [1]=> int(2015) } [2]=> array(2) { [0]=> int(75000) [1]=> int(2014) } [3]=> array(2) { [0]=> int(30000) [1]=> int(2016) } }


array(4) { [0]=> int(15000) [1]=> int(13000) [2]=> int(14500) [3]=> int(14000) }

So that would be what I am trying to achieve with my SQL output这就是我想用我的 SQL output 实现的目标

You just need to push the values you are reading from the database into the $samples and $targets arrays in your loop.您只需要将从数据库中读取的值推送到循环中的$samples$targets arrays 中。 It may also be necessary to convert them to integers, I've included that code too.可能还需要将它们转换为整数,我也包含了该代码。

$sql = "SELECT price, mileage, year From table";
$result = $conn->query($sql);
$samples = array();
$targets = array();
if ($result->num_rows > 0) {
    while($row = $result->fetch_assoc()) {
        $mileage = (int)$row['mileage'];
        $year = (int)$row['year'];
        $samples[] = array($mileage, $year);
        $price = (int)$row['price'];
        $targets[] = array($price);
    }
}

Note that without an else clause, the if ($result->num_rows > 0) { clause serves no useful purpose in this code as the while loop will not do anything if $result->num_rows == 0 (ie there are no rows in the result set).请注意,如果没有else子句, if ($result->num_rows > 0) {子句在此代码中没有用处,因为如果$result->num_rows == 0 (即没有行), while循环将不会做任何事情在结果集中)。

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

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