Machine Learning in PHP: Reality or Compromise?
Traditionally, machine learning (ML) is associated with Python, R, or Julia. However, PHP, being one of the most popular languages for web development, also has an ecosystem of libraries for ML. Using PHP for machine learning is a compromise: you lose in performance and the number of ready-made models compared to Python, but you gain seamless integration with web applications, a single technology stack, and the ability to quickly implement simple ML algorithms without launching microservices.
In this article, we will review the main libraries for machine learning in PHP, their capabilities, and usage examples.
1. PHP-ML (PHP Machine Learning)
PHP-ML is the most popular library for ML in PHP. It requires no external dependencies and is written entirely in PHP. It implements classic algorithms: classification, regression, clustering, dimensionality reduction, and evaluation metrics.
Capabilities:
- Classification: Naive Bayes, SVM, k-Nearest Neighbors, Decision Tree
- Regression: Linear Regression, SVR
- Clustering: k-Means, DBSCAN
- Data preprocessing: normalization, standardization
- Metrics: accuracy, precision, recall, F1-score
Example: Fisher's Iris Classification
require_once __DIR__ . '/vendor/autoload.php';
use Phpml\\Classification\\KNearestNeighbors;use Phpml\\Dataset\\Demo\\IrisDataset;
$dataset = new IrisDataset();$samples = $dataset->getSamples();$labels = $dataset->getTargets();
$classifier = new KNearestNeighbors(3);$classifier->train($samples, $labels);
// Prediction for a new instance (sepal length, sepal width, petal length, petal width)$newSample = [5.1, 3.5, 1.4, 0.2];$predicted = $classifier->predict($newSample);
echo "Predicted class: " . $predicted; // 'Iris-setosa'2. Rubix ML
Rubix ML is a more modern and powerful library. Unlike PHP-ML, it supports neural networks, gradient boosting, ensemble methods, and working with large datasets through generators (lazy evaluation).
Features:
- Neural networks (Multilayer Perceptron, CNN via conversion)
- Ensembles: Random Forest, Gradient Boosted Trees
- Data processing pipelines
- Support for categorical features and One-Hot Encoding
- Model export in PHP-serialized format
Example: Linear Regression with a Pipeline
require_once __DIR__ . '/vendor/autoload.php';
use Rubix\\ML\\Datasets\\Labeled;use Rubix\\ML\\Regressors\\Ridge;use Rubix\\ML\\Transformers\\NumericStringConverter;use Rubix\\ML\\Transformers\\MinMaxNormalizer;use Rubix\\ML\\Pipeline;
// Create a dataset$samples = [[1], [2], [3], [4], [5]];$labels = [1.5, 2.8, 4.1, 5.9, 7.2];
$dataset = new Labeled($samples, $labels);
// Create a pipeline$estimator = new Pipeline([ new NumericStringConverter(), new MinMaxNormalizer(),], new Ridge(1.0));
$estimator->train($dataset);
// Prediction$prediction = $estimator->predict([[6]]);echo "Predicted value: " . $prediction[0]; // ~8.33. Keras PHP (TensorFlow PHP Binding)
For working with deep neural networks in PHP, you can use Keras PHP — a wrapper over the Python library Keras/TensorFlow. It is essentially a bridge that runs Python scripts from PHP. It is not a pure PHP solution, but it allows you to leverage the full power of TensorFlow.
Example: Loading a Pre-trained Model
use KerasPHP\\Keras;
$keras = new Keras();
// Load a model from a .h5 file$model = $keras->load_model('/path/to/model.h5');
// Prepare data (array)$input = [[0.5, 0.2, 0.1]];
// Prediction$result =