PHP-ML – Machine Learning library for PHP

PHP-ML is a library developed to handle Machine learning tasks using PHP, and this library includes ML algorithms as well as data processing APIs that can handle data cleanups and feature extractions. Machine learning focuses on the use of data and algorithms to imitate the way that humans learn, gradually improving its accuracy.

PHP-ML requires PHP >= 7.1.

Fresh approach to Machine Learning in PHP.Algorithms, Cross Validation, Neural Network, Preprocessing, Feature Extraction and much more in one library.

  • Applications that are not able to afford cost associated with complex hardware and software development platforms, and only require simple predictions and data analytics php-ml solves the purpose.
  • It allows developers to perform complex machine learning tasks with a simple and intuitive API.
  • This library helps web developers, who are beginners of AI and just wish to learn AI concepts, without overhead of learning new languages such as R, Python or other.
  • Association rule learning
  • Classification
    • SVC
    • k-Nearest Neighbors
    • Naive Bayes
    • Decision Tree (CART)
    • Ensemble Algorithms
      • Bagging (Bootstrap Aggregating)
      • Random Forest
      • AdaBoost
    • Linear
      • Adaline
      • Decision Stump
      • Perceptron
      • LogisticRegression
  • Regression
  • Clustering
  • Metric
  • Workflow
  • Neural Network
  • Cross Validation
  • Feature Selection
  • Preprocessing
  • Feature Extraction
  • Dimensionality Reduction
    • PCA (Principal Component Analysis)
    • Kernel PCA
    • LDA (Linear Discriminant Analysis)
  • Datasets
  • Models management
  • Math

  • Install php-ai/php-ml with Composer:     
composer require php-ai/php-ml
  • Install php-ai/php-ml without Composer

you can download php-ml directly from github repo

Simple example of classification:

Classifier implementing the k-nearest neighbors algorithm.

require_once __DIR__ . '/vendor/autoload.php';
//it will return an instance of the Composer Autoloader

use PhpmlClassificationKNearestNeighbors;
// Import library

$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'
  • $k – number of nearest neighbors to scan (default: 3)
  • $distanceMetric – Distance object, default Euclidean
$classifier = new KNearestNeighbors($k=4);
$classifier = new KNearestNeighbors($k=3, new Minkowski($lambda=4));

To train a classifier simply provide train samples and labels (as array). Example:

You can train the classifier using multiple data sets, predictions will be based on all the training data.

$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);

To predict sample label use predict method. You can provide one sample or array of samples:

$classifier->predict([3, 2]);
// return 'b'

$classifier->predict([[3, 2], [1, 5]]);
// return ['b', 'a']

Source link