Photo by Markus Winkler on Unsplash
PHP is a very popular language, and when something gets very popular it usually happens that someone come out and just says “that’s not cool”, and it’s ok, it happens. I read lots of articles saying “PHP is not cool for machine learning” or saying “just move to language X to perform this, there will work better” (I’m not gonna flame another language, just talking about random stuff on the internet).
I’m a PHP lover, I won’t negate that, I tried different languages, frameworks, developed native applications, I love to try new things but I also love PHP. But sometimes I’m held back by the fact that the PHP ecosystem is missing some extremely interesting features like powerful Machine Learning libraries.
Last time this happened was during my PhD thesis writing. My thesis was about “Indoor Localization”: I had to study the various techniques, delineate a possible approach to perform the localization, and finally, I had to build an experimental system composed of a web interface to handle the localization and a client to use it.
To study Indoor Localization, I got many papers from IEEE Explore thanks to my university access, studied different approaches, and I finally picked a combination of techniques to approach my localization, but that’s not the purpose of this article.
And what does this have to do with PHP and Machine Learning? Well, it’s kinda curious, all tests I performed to learn how SVM works were in python because the engineer helping me with my development was skilled with Python, but I had to write a web application and that engineer wasn’t practical writing backends, so I got an idea, I google for “PHP svm” and the first result was “SVM — Manual — PHP” from PHP.net documentation.
Ext-svm: what and why
And here will come the critics, PHP just approached Machine Learning, other languages has it from years and, why should I pick it? It’s easy because ext-svm is just a wrapper for the libSVM library (check libsvm on Github) that’s written in C++, this means that every time you want to perform an SVM operation using PHP, this will be executed at a lower level and the results will be propagated to PHP thanks to the ext-svm interface.
LibSVM is an efficient solver for SVM classification and regression problems. The svm extension wraps this in a PHP interface for easy use in PHP scripts. As of version 0.2.0 the extension requires PHP 7.0 or above.
For me, that was awesome! That was everything I needed to move from Python (python devs please don’t hate me) that I didn’t know so much, to PHP on which I‘m more skilled.
Installing ext-svm
First of all, the extension is available on Pecl, this means that if you have access to a command-line with administrator permissions you can just
pecl install svm
Please note that ext-svm is currently in Beta, so if you got problems with pecl, to install it any way you can just:
pecl install svm-beta
The output will be something like:
downloading svm-0.2.3.tgz ...
Starting to download svm-0.2.3.tgz (130,776 bytes)
.............................done: 130,776 bytes
7 source files, building
[... skipping the whole output ...]
Build process completed successfully
Installing '/usr/lib/php/20190902/svm.so'
install ok: channel://pecl.php.net/svm-0.2.3
Extension svm enabled in php.ini
If you want to check if the extension has correctly been installed just launch:
$ php -m | grep svm
svm
The extension is now installed and you’re ready to go!
Build your first classifier
To build our first classifier we’ll just stick to the documentation example and check if it works. Our classifier will be trained from an array, and we’ll check if it can predict a value from the training dataset.
<?php
$data = array(
array(-1, 1 => 0.43, 3 => 0.12, 9284 => 0.2),
array(1, 1 => 0.22, 5 => 0.01, 94 => 0.11),
);
$svm = new SVM();
$model = $svm->train($data);
$data = array(1 => 0.43, 3 => 0.12, 9284 => 0.2);
$result = $model->predict($data);
echo $result;
If you save this file and then run it via console, or put it in a web server and access to it, the result should be -1. Why?
Let’s decompose our code to understand how it works. On the first two lines, we found our array of array representing the data
$data = array(
array(-1, 1 => 0.43, 3 => 0.12, 9284 => 0.2),
array(1, 1 => 0.22, 5 => 0.01, 94 => 0.11),
);
If you focus on the inner array, every row can be split into two parts: the result and the features for training. The first value of the array (at index 0) is the classification result, followed by features, so for array(-1, 1 => 0.43, 3 => 0.12, 9284 => 0.2) the classification result will be -1 and the following values grouped as key/value couples are features.
Later we instance the SVM:
$svm = new SVM();
The SVM instance can be tweaked by calling setOptions, for example, you can change your kernel before training:
$svm->setOptions([ SVM::OPT_KERNEL_TYPE => SVM::KERNEL_SIGMOID ]);
Constant values to tweak options can be found in the SVM Class Documentation on php.net.
Now that we have our SVM ready, we can train a model:
$model = $svm->train($data);
This line outputs an SVMModel which we can use for predictions. If you have a very big model, you can just save it to a file with save(string $filename) and reload from the filesystem with load(string $filename) this way:
$model->save('model.svm');
// later... somewhere in the code
$model->load('model.svm');
Please note: save and load will return a boolean value, you can check it or drop it, it’s up to you!
Latest three lines are the testing code:
$data = array(1 => 0.43, 3 => 0.12, 9284 => 0.2);
$result = $model->predict($data);
echo $result;
You take the features you measured and wanna check against the model and put them into an array, in this code the array is called $data. That features will be processed from SVM by using the training set.
Once our data are ready, we can call predict to classify the data passed as input and get a float output corresponding to the prediction. Finally, we can echo it, or use it somewhere else.
Built, trained, predicted
Photo by Stephen Dawson on Unsplash
Now your system has the PHP SVM extension installed, you can train a model, save it, reload it and make predictions, everything from now, it’s up to you!