phpml - 基于 PHP-ML 库实现机器学习


MIT
跨平台
PHP

软件简介

phpml

基于 PHP-ML 库实现机器学习

基于语言学习

基于语言学习,根据语言编码实现学习

实例

require_once 'vendor/autoload.php';
use Phpml\Classification\KNearestNeighbors; 
use Phpml\Dataset\CsvDataset;
use Phpml\Dataset\ArrayDataset;
use Phpml\FeatureExtraction\TokenCountVectorizer;
use Phpml\Tokenization\WordTokenizer;
use Phpml\CrossValidation\StratifiedRandomSplit;
use Phpml\FeatureExtraction\TfIdfTransformer;
use Phpml\Metric\Accuracy;
use Phpml\Classification\SVC;
use Phpml\Regression\SVR;
use Phpml\SupportVectorMachine\Kernel;

$dataset = new CsvDataset('languages.csv', 1);
$vectorizer = new TokenCountVectorizer(new WordTokenizer());
$tfIdfTransformer = new TfIdfTransformer();

$testample=['我是中国人'];


$samples = [];
foreach ($dataset->getSamples() as $sample) {
   $samples[] = $sample[0];
}


$vectorizer->fit($samples);
$vectorizer->transform($samples);

$vectorizer->fit($testample);
$vectorizer->transform($testample);

$tfIdfTransformer->fit($samples);
$tfIdfTransformer->transform($samples);



$dataset = new ArrayDataset($samples, $dataset->getTargets());

$randomSplit = new StratifiedRandomSplit($dataset, 0.1);


$classifier = new SVC(Kernel::RBF, 10000);
$classifier->train($randomSplit->getTrainSamples(), $randomSplit->getTrainLabels());
$testpredictedLabels = $classifier->predict($testample);

print_r($testpredictedLabels);// return  Array ( [0] => zh )
exit;