Skip to content

Commit 52e556d

Browse files
committed
Add air pollution examples
1 parent 02294eb commit 52e556d

File tree

11 files changed

+6275
-7
lines changed

11 files changed

+6275
-7
lines changed

classification/airPollution.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PhpmlExamples;
6+
7+
use Phpml\Classification\KNearestNeighbors;
8+
use Phpml\Dataset\CsvDataset;
9+
10+
include 'vendor/autoload.php';
11+
12+
$dataset = new CsvDataset(__DIR__.'/../data/air.csv', 2, false, ';');
13+
14+
foreach (range(1, 10) as $k) {
15+
$correct = 0;
16+
foreach ($dataset->getSamples() as $index => $sample) {
17+
$estimator = new KNearestNeighbors($k);
18+
$estimator->train($other = removeIndex($index, $dataset->getSamples()), removeIndex($index, $dataset->getTargets()));
19+
20+
$predicted = $estimator->predict([$sample]);
21+
22+
if ($predicted[0] === $dataset->getTargets()[$index]) {
23+
$correct++;
24+
}
25+
}
26+
27+
echo sprintf('Accuracy (k=%s): %.02f%% correct: %s', $k, ($correct / count($dataset->getSamples())) * 100, $correct) . PHP_EOL;
28+
}
29+
30+
function removeIndex($index, $array): array
31+
{
32+
unset($array[$index]);
33+
return $array;
34+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PhpmlExamples;
6+
7+
use Phpml\Classification\KNearestNeighbors;
8+
use Phpml\Dataset\CsvDataset;
9+
use Phpml\Math\Distance\Minkowski;
10+
11+
include 'vendor/autoload.php';
12+
13+
$minLat = 41.34343606848294;
14+
$maxLat = 57.844750992891;
15+
$minLng = -16.040039062500004;
16+
$maxLng = 29.311523437500004;
17+
18+
$step = 0.1;
19+
$k = 3;
20+
21+
$dataset = new CsvDataset(__DIR__.'/../data/air.csv', 2, false, ';');
22+
$estimator = new KNearestNeighbors($k, new Minkowski());
23+
$estimator->train($dataset->getSamples(), $dataset->getTargets());
24+
25+
$lines = [];
26+
for ($lat=$minLat; $lat<$maxLat; $lat+=$step) {
27+
for ($lng=$minLng; $lng<$maxLng; $lng+=$step) {
28+
$lines[] = sprintf('%s;%s;%s', $lat, $lng, $estimator->predict([[$lat, $lng]])[0]);
29+
}
30+
}
31+
32+
file_put_contents(__DIR__.'/../data/airVis.csv', implode(PHP_EOL, $lines));

classification/languageDetection.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
<?php declare(strict_types=1);
1+
<?php
2+
3+
declare(strict_types=1);
24

35
namespace PhpmlExamples;
46

classification/mnist.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
namespace PhpmlExamples;
46

57
use Phpml\Classification\SVC;
@@ -11,8 +13,8 @@
1113

1214
function convert($size)
1315
{
14-
$unit=array('b','kb','mb','gb','tb','pb');
15-
return @round($size/pow(1024,($i=floor(log($size,1024)))),2).' '.$unit[$i];
16+
$unit=['b','kb','mb','gb','tb','pb'];
17+
return @round($size/pow(1024, ($i=floor(log($size, 1024)))), 2).' '.$unit[$i];
1618
}
1719

1820
$start = $loadStart = microtime(true);

classification/spamFilter.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
<?php declare(strict_types=1);
1+
<?php
2+
3+
declare(strict_types=1);
24

35
namespace PhpmlExamples;
46

composer.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,8 @@
1818
],
1919
"require": {
2020
"php-ai/php-ml": "0.7.0"
21+
},
22+
"require-dev": {
23+
"friendsofphp/php-cs-fixer": "^2.14"
2124
}
2225
}

0 commit comments

Comments
 (0)