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.
Features
- 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
- Accuracy
- Confusion Matrix
- Classification Report
- Regression
- Workflow
- Pipeline
- FeatureUnion
- Neural Network
- Cross Validation
- Feature Selection
- Preprocessing
- Normalization
- Imputation missing values
- LabelEncoder
- LambdaTransformer
- NumberConverter
- ColumnFilter
- OneHotEncoder
- Feature Extraction
- Token Count Vectorizer
- NGramTokenizer
- WhitespaceTokenizer
- WordTokenizer
- Tf-idf Transformer
- Token Count Vectorizer
- Dimensionality Reduction
- PCA (Principal Component Analysis)
- Kernel PCA
- LDA (Linear Discriminant Analysis)
- Datasets
- Models management
- Math
Installation
- 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 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));
Train
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);
Predict
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.
References
https://php-ml.readthedocs.io/en/latest/
Have a Good Day ahead!!