Artificial intelligence and Machine learning is a very hot topic for most developers, but not a very common topic for web developers. PHP-ML is a machine learning library available for PHP which provides Algorithms, Cross Validation, Neural Network, Preprocessing, Feature Extraction and much more in one library.
Simple example of classification using the library:
require_once __DIR__ . '/vendor/autoload.php';
use Phpml\Classification\KNearestNeighbors;
$samples = [[1, 3], [1, 4], [2, 4], [3, 1], [4, 1], [4, 2]];
$labels = ['a', 'a', 'a', 'b', 'b', 'b'];
$classifier = new KNearestNeighbors();
$classifier->train($samples, $labels);
echo $classifier->predict([3, 2]);
// return 'b'
Code review code estimator
A more interesting application of this library is a code review cost estimation app. You should first create a csv file under data/code-reviews.csv which include the columns : “commits; additions; deletions; changed_files; comments; review_comments; price”
Then train your model using :
> bin/console train
R2: 0.90124676557598
New model trained! :rocket:
You can use this model to estimate a PR :
> bin/console estimate:github symfony/symfony/pull/27647
Fetching symfony/symfony/pull/27647 pull request data
Price for symfony/symfony/pull/27647 is: $51.36
You can also use the estimate wizard, which will ask you questions to provide you with a code review cost estimation :
> bin/console estimate:wizard
Commits: 1
Additions: 100
Deletions: 20
Changed files: 2
Comments: 0
Review comments: 0
Price for PR is: $94.4
PHP-ML Stock Forecast
A second application using the PHP-ML library is a PHP-ML stock forecast which provides a simple stock and cryptocurrency price forecasting console application.
Actually the application is more proof of concept, as it can only use some linear algorithms, SquareLevels, Support Vector Regression and a basic linear regression based on Cumulative Moving Averages. More interesting features are in the TODO to make it more interesting.
You can try it with bin/console forecast:stock <base_currency> <stock/crypto code> <date_interval_magnitude (minutes/hours/days)>
$ bin/console forecast:stock USD BTC days
This will generate a table with the historical price information and a prevision for the next period based in the historical data, giving you three estimations, based on short-term, medium-term and long-term.
You can then get insights and signals using :
bin/console forecast:signals EUR ETC
This will output some signals and score built based on last month / day / hour for given currency / crypto pair, or the default pairs if none are given.
===== SOME SIGNALS FOR EUR - ETC ON Jan 14th, 15:21h =====
Signals based on last hour (Score: 0):
- Stable in this period.
Signals based on last day (Score: -1):
- Loosing value in short term.
Signals based on last month (Score: 3):
- Improving exponentially.
- Recovering value in short term.
- Having a noticeable improvement recently (6.62%).
More ML examples
Examples of artificial intelligence using PHP-ML library are available for langage detection and hands digits recognition for classification algorithm. A regression example is also available to assess the quality of the wine.
More information at PHP-AI, code review estimator, and stock forecast. Released under a MIT license.