[Machine learning] Array datasets in PHP

Goodmorning, welcome back to the backend developers zone, machine learning has found its way into the world of php which enables software engineers and machine learning enthusiasts do more with it, for now in my recent posts concerning Php for machine learning, we would be using the PHPML library for machine learning to implement stuffs.

Machine learning has to do with a lot of math and datasets and implementation of those datasets, if you have looked into machine learning with python, you sure must have studied about the numpy (Numerical python) library which tends to hold more information about array datasets to be used in data analysis for machine learning, most of those concepts also apply here in using PHP for Machine learning, and i would try as much as possible to explain in its simplest term.

NB: This post is for newbies and beginners, please if you are a professional or senior developer this post is not for you, unless you have feedback on improvements which should be done, and which i will really appreciate.

In array datasets for machine learning, the arrays can be said to be one dimensional or two dimensional

//example of a one dimensional array dataset
$one_dimensional_array_ds = array([1,2,3,4]);

//example of a two dimensional array dataset
$two_dimensional_array_ds = array([[1,2],[3,4],[5,6],[7,8]]);

Now we would be using these forms of array in our examples as we proceed, now from the PhpMl documentation, we are meant to know that Helper class that holds data as PHP array type. Implements the Dataset interface which is used heavily in other classes. in simple terms if we have a class that would certainly use the array method or function, it must implement a php interface known as the dataset interface, because in machine learning data is key.

Constructors Parameters

  • $samples – (array) of samples
  • $labels – (array) of labels
use Phpml\Dataset\ArrayDataset;

$dataset = new ArrayDataset([[1, 1], [2, 1], [3, 2], [4, 1]], ['a', 'a', 'b', 'b']);

Remember what we said about the two dimensional array

Samples and labels

To get samples or labels you can use getters:


Remove columns

You can remove columns by index numbers, for example:

use Phpml\Dataset\ArrayDataset;

$dataset = new ArrayDataset(
    [[1,2,3,4], [2,3,4,5], [3,4,5,6], [4,5,6,7]],
    ['a', 'a', 'b', 'b']


// now from each sample column 0 and 2 are removed
// [[2,4], [3,5], [4,6], [5,7]]

This serves as a beginners guide in using arraydatasets for machine learning in PHP.


Add comment

Your Header Sidebar area is currently empty. Hurry up and add some widgets.