Skip to content

Instantly share code, notes, and snippets.

@bitsnaps
Last active March 28, 2022 16:36
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bitsnaps/73b061dca230eb7e134c7171ebda698f to your computer and use it in GitHub Desktop.
Save bitsnaps/73b061dca230eb7e134c7171ebda698f to your computer and use it in GitHub Desktop.
PHP-ML machine learning php library examples
<?php
/*
You should first install phpml library with composer:
```
composer require php-ai/php-ml
```
then create in the same directory of vendor a file (e.g. `index.php`) with this content.
*/
require_once __DIR__ . '/vendor/autoload.php';
use Phpml\Classification\KNearestNeighbors;
use Phpml\Classification\NaiveBayes;
use Phpml\Regression\LeastSquares;
use Phpml\Regression\SVR;
use Phpml\SupportVectorMachine\Kernel;
use Phpml\Clustering\KMeans;
// KNearestNeighbors Classification
$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);
$result_knn = $classifier->predict([3, 2]);
// echo $classifier->predict([3, 2]);
// return 'b'
assert($result_knn == 'b');
// NaiveBayes Classification
$samples = [[5, 1, 1], [1, 5, 1], [1, 1, 5]];
$labels = ['a', 'b', 'c'];
$classifier = new NaiveBayes();
$classifier->train($samples, $labels);
$result_naive_bayes = $classifier->predict([3, 1, 1]);
assert($result_naive_bayes == 'a');
$result_naive_bayes = $classifier->predict([[3, 1, 1], [1, 4, 1]]);
assert($result_naive_bayes == ['a','b']);
// LeastSquares Linear Regression
$samples = [[60], [61], [62], [63], [65]];
$targets = [3.1, 3.6, 3.8, 4, 4.1];
$regression = new LeastSquares();
$regression->train($samples, $targets);
$result_regression = $regression->predict([64]);
// $result_regression ~ 4.0581081081082
assert($result_regression >= 4.058 && $result_regression < 4.06);
// Multiple Linear Regression
$samples = [[73676, 1996], [77006, 1998], [10565, 2000], [146088, 1995], [15000, 2001], [65940, 2000], [9300, 2000], [93739, 1996], [153260, 1994], [17764, 2002], [57000, 1998], [15000, 2000]];
$targets = [2000, 2750, 15500, 960, 4400, 8800, 7100, 2550, 1025, 5900, 4600, 4400];
$regression = new LeastSquares();
$regression->train($samples, $targets);
$regression_linear = $regression->predict([60000, 1996]);
// return 4094.829932965
assert($regression_linear >= 4094.82 && $regression_linear < 4094.83);
assert($regression->getIntercept() <= -800614.95 && $regression->getIntercept() > -800614.96);
// return -800614.95740509
assert(count($regression->getCoefficients()) == 2 );
// return array(2) {[0]=> float(-0.032710644137865) [1]=> float(404.14450199716)}
// Support Vector Machines
$samples = [[60], [61], [62], [63], [65]];
$targets = [3.1, 3.6, 3.8, 4, 4.1];
$regression = new SVR(Kernel::LINEAR);
$regression->train($samples, $targets);
$regression_svm = $regression->predict([64]);
assert($regression_svm == 4.03334);
// KMeans Clustering
$kmeans = new KMeans(2);
$kmeans = new KMeans(4, KMeans::INIT_RANDOM);
$samples = [[1, 1], [8, 7], [1, 2], [7, 8], [2, 1], [8, 9]];
// Or if you need to keep your indentifiers along with yours samples you can use array keys as labels.
// $samples = [ 'Label1' => [1, 1], 'Label2' => [8, 7], 'Label3' => [1, 2]];
$clusters = $kmeans->cluster($samples);
// echo "Detected Clusters:\n";
assert(count($clusters) == 4);
// checkout for more examples: https://php-ml.readthedocs.io/en/latest/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment