Machine Learning in PHP: Overview of Libraries for ML

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Machine Learning in PHP: Overview of Libraries for Implementing ML Algorithms



Although Python is traditionally considered the main language for machine learning, PHP also has several powerful libraries that allow you to integrate ML models directly into web applications. In this article, we will review the main tools for machine learning in PHP, their capabilities, and usage examples.



Why PHP for Machine Learning?

PHP remains one of the most popular languages for web development. Using ML libraries in PHP allows you to:

  • Integrate models directly into existing Laravel/Symfony projects
  • Avoid switching between languages within a single project
  • Use the familiar Composer ecosystem and autoloading
  • Quickly deploy prototypes on cheap hosting


1. PHP-ML — The Standard for Machine Learning in PHP

PHP-ML (Machine Learning library for PHP) is the most popular library for ML in PHP. It provides a wide range of algorithms without external dependencies.



Installation

composer require php-ai/php-ml


Example: Text Classification

<?phprequire_once __DIR__ . '/vendor/autoload.php';

use Phpml\\Classification\\KNearestNeighbors;use Phpml\\FeatureExtraction\\TfIdfTransformer;use Phpml\\Tokenization\\WhitespaceTokenizer;use Phpml\\FeatureExtraction\\TokenCountVectorizer;

// Training data$samples = [ 'great product fast delivery', 'terrible quality defective', 'recommend to all friends', 'does not work broke after a day'];$labels = ['positive', 'negative', 'positive', 'negative'];

// Converting text to vectors$vectorizer = new TokenCountVectorizer(new WhitespaceTokenizer());$vectorizer->fit($samples);$vectorizer->transform($samples);

$transformer = new TfIdfTransformer();$transformer->fit($samples);$transformer->transform($samples);

// Training the model$classifier = new KNearestNeighbors();$classifier->train($samples, $labels);

// Prediction$testSample = ['excellent product quality'];$vectorizer->transform($testSample);$transformer->transform($testSample);

$predicted = $classifier->predict($testSample[0]);echo "Sentiment: $predicted"; // positive?>


PHP-ML Features

  • Classification: KNearestNeighbors, NaiveBayes, SVC, DecisionTree
  • Regression: LeastSquares, Ridge, LogisticRegression
  • Clustering: KMeans, DBSCAN, FuzzyCMeans
  • Text Processing: TF-IDF, stemming, tokenization
  • Metrics: accuracy, precision, recall, confusion matrix


2. Rubix ML — Modern Framework for Machine Learning

Rubix ML is a newer and more performant library with support for neural networks, GPU acceleration, and data processing pipelines.



Installation

composer require rubix/ml


Example: Neural Network for Classification

<?phprequire_once __DIR__ . '/vendor/autoload.php';

use Rubix\\ML\\Datasets\\Labeled;use Rubix\\ML\\NeuralNet\\Layers\\Dense;use Rubix\\ML\\NeuralNet\\Layers\\Activation;use Rubix\\ML\\NeuralNet\\ActivationFunctions\\ReLU;use Rubix\\ML\\NeuralNet\\ActivationFunctions\\Softmax;use Rubix\\ML\\NeuralNet\\Optimizers\\Adam;use Rubix\\ML\\Classifiers\\MultilayerPerceptron;use Rubix\\ML\\CrossValidation\\Reports\\MulticlassBreakdown;

// Creating a dataset$samples = [[2.5, 1.2], [1.0, 0.8], [3.5, 4.2], [2.0, 0.5]];$labels = ['cat', 'dog', 'cat', 'dog'];$dataset = new Labeled($samples, $labels);

// Creating an MLP (Multilayer Perceptron)$mlp = new MultilayerPerceptron([ new Dense(10), new Activation(new ReLU()), new Dense(5), new Activa

Recommendations