r/PHPhelp • • Sep 08 '25

PHP ML

How can I implement ML in my PHP webapp

0 Upvotes

7 comments sorted by

View all comments

3

u/Prestigiouspite Sep 08 '25

composer require rubix/ml

<?php
require 'vendor/autoload.php';

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

// Training data
$dataset = Labeled::build([
    [65, 220], [72, 250], [78, 265], [69, 210], [75, 240],
    [60, 155], [67, 160], [70, 170], [62, 120], [66, 110],
], [
    'male', 'male', 'male', 'male', 'male',
    'female', 'female', 'female', 'female', 'female',
]);

// Model: K-Nearest Neighbors
$estimator = new KNearestNeighbors(3);
$estimator->train($dataset);

// Prediction
$sample = [68, 200];
$prediction = $estimator->predict($sample);

echo "Prediction: $prediction\n";
?>