Reading list Switch to dark mode

    PHP-ML – Machine Learning library for PHP

    Updated 9 May 2023

    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 by using PHP-ML Algorithms like Cross Validation, Neural Network, Preprocessing, Feature Extraction and much more in one library.

    Advantages of php-ml

    • 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.



    • 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.

    Start your headless eCommerce
    Find out More
    require_once __DIR__ . '/vendor/autoload.php';
    //it will return an instance of the Composer Autoloader
    use Phpml\Classification\KNearestNeighbors;
    // 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'

    Constructor Parameters

    • $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']

    If you need Machine Learning and AI Development Services then feel free to reach us.


    Have a Good Day ahead!!

    . . .

    Leave a Comment

    Your email address will not be published. Required fields are marked*

    Be the first to comment.

    Back to Top

    Message Sent!

    If you have more details or questions, you can reply to the received confirmation email.

    Back to Home

    Table of Content