Skip to content
Advertisement

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. 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:

JavaScript

Here is myScript

JavaScript

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. 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.

When I do

JavaScript

I get

JavaScript

So that would be what I am trying to achieve with my SQL output

Advertisement

Answer

You just need to push the values you are reading from the database into the $samples and $targets arrays in your loop. It may also be necessary to convert them to integers, I’ve included that code too.

JavaScript

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 (i.e. there are no rows in the result set).

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement