Text classification examples

This commit is contained in:
Arkadiusz Kondas committed 2019-03-21 22:12:00 +01:00
1 parent 50af45491b
commit 221903c0f1
9 files changed
+140 -8

No files matched your search

+2
View File
@@ -10,3 +10,5 @@ humbuglog.*
/data/stackoverflow_*.csv
/data/crimes*.csv
/data/bbc
/model/*
!/model/.gitkeep
+9
View File
@@ -8,6 +8,14 @@ Interesting demo/examples projects using `php-ml`:
* [Code Review Estimator](https://github.com/akondas/code-review-estimator) - Simple showcase of machine learning for code review cost estimation.
## Articles
Many of samples from this repository was used in my articles
* Text data classification with BBC news article dataset
* [Clustering Chicago robberies locations with k-means algorithm](https://arkadiuszkondas.com/clustering-chicago-robberies-locations-with-k-means-algorithm/)
* [Predict air pollution with k-Nearest Neighbors and PHP](https://arkadiuszkondas.com/predict-air-pollution-with-k-nearest-neighbors-and-php/)
## Examples
To test example, select one of the following and run it from main folder (each category has its own folder).
@@ -21,6 +29,7 @@ Classification:
* `languageDetection.php` - classifier build for language detection
* `minst.php` - recognize handwritten digits from MNIST dataset (to download dataset use `bin/download-mnist.sh`)
* `spamFilter.php` - simple spam filter with example dataset
* `bbc.php` - example of text classification
Regression:
+41
View File
@@ -0,0 +1,41 @@
<?php
declare(strict_types=1);
namespace PhpmlExamples;
use Phpml\Classification\SVC;
use Phpml\CrossValidation\StratifiedRandomSplit;
use Phpml\Dataset\FilesDataset;
use Phpml\FeatureExtraction\StopWords\English;
use Phpml\FeatureExtraction\TfIdfTransformer;
use Phpml\FeatureExtraction\TokenCountVectorizer;
use Phpml\Metric\Accuracy;
use Phpml\Tokenization\NGramTokenizer;
include 'vendor/autoload.php';
$dataset = new FilesDataset(__DIR__.'/../data/bbc');
$split = new StratifiedRandomSplit($dataset, 0.3);
$samples = $split->getTrainSamples();
$vectorizer = new TokenCountVectorizer(new NGramTokenizer(1, 3), new English());
$vectorizer->fit($samples);
$vectorizer->transform($samples);
$transformer = new TfIdfTransformer();
$transformer->fit($samples);
$transformer->transform($samples);
$classifier = new SVC();
$classifier->train($samples, $split->getTrainLabels());
$testSamples = $split->getTestSamples();
$vectorizer->transform($testSamples);
$transformer->transform($testSamples);
$predicted = $classifier->predict($testSamples);
echo 'Accuracy: ' . Accuracy::score($split->getTestLabels(), $predicted);
+42
View File
@@ -0,0 +1,42 @@
<?php
declare(strict_types=1);
namespace PhpmlExamples;
use Phpml\Classification\SVC;
use Phpml\CrossValidation\StratifiedRandomSplit;
use Phpml\Dataset\FilesDataset;
use Phpml\FeatureExtraction\StopWords\English;
use Phpml\FeatureExtraction\TfIdfTransformer;
use Phpml\FeatureExtraction\TokenCountVectorizer;
use Phpml\Metric\Accuracy;
use Phpml\ModelManager;
use Phpml\Pipeline;
use Phpml\SupportVectorMachine\Kernel;
use Phpml\Tokenization\NGramTokenizer;
include 'vendor/autoload.php';
$dataset = new FilesDataset(__DIR__.'/../data/bbc');
$split = new StratifiedRandomSplit($dataset, 0.1);
$pipeline = new Pipeline([
new TokenCountVectorizer($tokenizer = new NGramTokenizer(1, 3), new English()),
new TfIdfTransformer()
], new SVC(Kernel::LINEAR));
$start = microtime(true);
$pipeline->train($split->getTrainSamples(), $split->getTrainLabels());
$stop = microtime(true);
$predicted = $pipeline->predict($split->getTestSamples());
echo 'Train: ' . round($stop - $start, 4) . 's'. PHP_EOL;
echo 'Estimator: ' . get_class($pipeline->getEstimator()) . PHP_EOL;
echo 'Tokenizer: ' . get_class($tokenizer) . PHP_EOL;
echo 'Accuracy: ' . Accuracy::score($split->getTestLabels(), $predicted);
$modelManager = new ModelManager();
$modelManager->saveToFile($pipeline, __DIR__.'/../model/bbc-nb.phpml');
+36
View File
@@ -0,0 +1,36 @@
<?php
declare(strict_types=1);
namespace PhpmlExamples;
use Phpml\ModelManager;
include 'vendor/autoload.php';
$start = microtime(true);
$modelManager = new ModelManager();
$model = $modelManager->restoreFromFile(__DIR__.'/../model/bbc.phpml');
$total = microtime(true) - $start;
echo sprintf('Model loaded in %ss', round($total, 4)) . PHP_EOL;
$text = 'The future of the games industry, at least as Google sees it, is in streaming.
It’s a trend that feels inevitable - just ask anyone in the music, TV or film business. Streaming is where it\'s at, and the possibility for what can be streamed has only ever been bound by the limitations of internet connectivity.
Google thinks its technology can make streaming games a plausible and possibly even pleasurable reality. One where gamers aren’t driven to insanity by stuttering gameplay and slow-reacting characters.
For the sake of argument, let’s assume it succeeds. Where might Google - with its track record for upending business models, often with unintended consequences - lead the industry?
Shifting costs
Games consoles are expensive. The games are (mostly) expensive.
Google’s Stadia could eliminate both costs, replacing them with a subscription fee. A ballpark figure might be $15-$30 a month - though some predict big name titles might have an additional fee on top, like buying a new movie on Amazon Prime Video.
Good news? It depends on where you’re coming from.
For gamers, there are a number of hurdles. Phil Harrison, Google’s man in charge of Stadia, told me his team\'s tests managed 4K gaming on download speeds of “around 25mbps”.
For context, Microsoft currently suggests a minimum of just 3mbps to play “traditional” games online. And the difference between getting 3mbps and 25mbps? Hundreds of dollars a year in payments to your internet service provider.
Or, the difference could be not being able to play at all - 25mbps is more than double the average connection speed across the US, according to research commissioned and part-funded by, er, Google.';
$start = microtime(true);
$predicted = $model->predict([$text])[0];
$total = microtime(true) - $start;
echo sprintf('Predicted category: %s in %ss', $predicted, round($total, 6)) . PHP_EOL;
+1 -1
View File
@@ -17,7 +17,7 @@
}
],
"require": {
"php-ai/php-ml": "0.7.0"
"php-ai/php-ml": "0.8.0"
},
"require-dev": {
"friendsofphp/php-cs-fixer": "^2.14"
Generated
+6 -6
View File
@@ -4,20 +4,20 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically"
],
"content-hash": "01792e300aa1a4d851ba3b7c9c2f6a09",
"content-hash": "8a52bfd08b32850b0b1439755dde2209",
"packages": [
{
"name": "php-ai/php-ml",
"version": "0.7.0",
"version": "0.8.0",
"source": {
"type": "git",
"url": "https://github.com/php-ai/php-ml.git",
"reference": "db95db3e57475df53d602b32c4451438c21e46c3"
"reference": "5e02b893e904761cd7e2415b11778cd421494c07"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/php-ai/php-ml/zipball/db95db3e57475df53d602b32c4451438c21e46c3",
"reference": "db95db3e57475df53d602b32c4451438c21e46c3",
"url": "https://api.github.com/repos/php-ai/php-ml/zipball/5e02b893e904761cd7e2415b11778cd421494c07",
"reference": "5e02b893e904761cd7e2415b11778cd421494c07",
"shasum": ""
},
"require": {
@@ -59,7 +59,7 @@
"machine learning",
"pattern recognition"
],
"time": "2018-11-07T18:40:31+00:00"
"time": "2019-03-20T22:22:45+00:00"
}
],
"packages-dev": [
View File
Whitespace-only changes.
+3 -1
View File
@@ -1,4 +1,6 @@
<?php declare(strict_types=1);
<?php
declare(strict_types=1);
namespace PhpmlExamples;