ML Ecosystem in PHP

Learning examples

These examples will help you understand how you can use ML in PHP. They are not full-fledged applications, but they will help you understand the basics of working with ML in PHP.

Example with RubixML

Rubix supports classification, regression, clustering, and working with datasets as first-class objects. Let's see how this looks in practice. Let's say we have data for binary classification.
The code below also trains a k-nearest neighbors (k-NN) classifier, but this time on height and weight data with gender labels, and then predicts the label $M$ for a new person. The model returns $[172, 68]$ for the parameters, since most of the 3 nearest neighbors have this label.

 
<?php

use Rubix\ML\Classifiers\KNearestNeighbors;
use 
Rubix\ML\Datasets\Labeled;
use 
Rubix\ML\Datasets\Unlabeled;

$samples = [
    [
170, 65],
    [
160, 50],
    [
180, 80],
    [
175, 70],
];

$labels = ['M', 'F', 'M', 'M'];

$dataset = new Labeled($samples, $labels);

$model = new KNearestNeighbors(3);
$model->train($dataset);

$testSamples = new Unlabeled([[172, 68]]);
$prediction = $model->predict($testSamples);

echo 
$prediction[0];
Result: Memory: 0.358 Mb Time running: 0.011 sec.
M