From 221903c0f1d3457fff731583a6ec4015a29abfce Mon Sep 17 00:00:00 2001 From: Arkadiusz Kondas Date: Thu, 21 Mar 2019 22:12:00 +0100 Subject: [PATCH] Text classification examples --- .gitignore | 2 ++ README.md | 9 ++++++++ classification/bbc.php | 41 +++++++++++++++++++++++++++++++++ classification/bbcPipeline.php | 42 ++++++++++++++++++++++++++++++++++ classification/bbcRestored.php | 36 +++++++++++++++++++++++++++++ composer.json | 2 +- composer.lock | 12 +++++----- model/.gitkeep | 0 regression/wineQuality.php | 4 +++- 9 files changed, 140 insertions(+), 8 deletions(-) create mode 100644 classification/bbc.php create mode 100644 classification/bbcPipeline.php create mode 100644 classification/bbcRestored.php create mode 100644 model/.gitkeep diff --git a/.gitignore b/.gitignore index c2ebe75..3a37dfe 100644 --- a/.gitignore +++ b/.gitignore @@ -10,3 +10,5 @@ humbuglog.* /data/stackoverflow_*.csv /data/crimes*.csv /data/bbc +/model/* +!/model/.gitkeep diff --git a/README.md b/README.md index 55dd66e..33395d7 100644 --- a/README.md +++ b/README.md @@ -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: diff --git a/classification/bbc.php b/classification/bbc.php new file mode 100644 index 0000000..14e7952 --- /dev/null +++ b/classification/bbc.php @@ -0,0 +1,41 @@ +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); diff --git a/classification/bbcPipeline.php b/classification/bbcPipeline.php new file mode 100644 index 0000000..47611a0 --- /dev/null +++ b/classification/bbcPipeline.php @@ -0,0 +1,42 @@ +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'); diff --git a/classification/bbcRestored.php b/classification/bbcRestored.php new file mode 100644 index 0000000..c26dffc --- /dev/null +++ b/classification/bbcRestored.php @@ -0,0 +1,36 @@ +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; diff --git a/composer.json b/composer.json index 52370af..b28daa9 100644 --- a/composer.json +++ b/composer.json @@ -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" diff --git a/composer.lock b/composer.lock index 4bc7d3d..3f29b98 100644 --- a/composer.lock +++ b/composer.lock @@ -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": [ diff --git a/model/.gitkeep b/model/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/regression/wineQuality.php b/regression/wineQuality.php index 295908a..9e60597 100644 --- a/regression/wineQuality.php +++ b/regression/wineQuality.php @@ -1,4 +1,6 @@ -