-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpredict.php
62 lines (50 loc) · 1.62 KB
/
predict.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
<?php
if (!($loader = @include __DIR__ . '/vendor/autoload.php')) {
die(<<<EOT
You need to install the project dependencies using Composer:
$ wget http://getcomposer.org/composer.phar
OR
$ curl -s https://getcomposer.org/installer | php
$ php composer.phar install --dev
$ phpunit
EOT
);
}
use Rubix\ML\Datasets\Unlabeled;
use Rubix\ML\Extractors\CSV;
use Rubix\ML\Extractors\ColumnPicker;
use Rubix\ML\PersistentModel;
use Rubix\ML\Persisters\Filesystem;
use Rubix\ML\Transformers\NumericStringConverter;
// We use the same dataset (it's a test, not a real ML app)
$dataset = Unlabeled::fromIterator(
(new ColumnPicker(
new CSV('./data/customers.csv', true), [
'recency',
'frequency',
'monetary',
])
)
)->apply(new NumericStringConverter())
;
$variables = [
'id_customer',
'recency',
'monetary',
'frequency',
];
$kmeans = PersistentModel::load(new Filesystem('./models/customers_clustering.rbx'));
$predictions = $kmeans->predict($dataset);
array_unshift($predictions, 'cluster');
// We add the predictions variable to the former dataset
$selection = new ColumnPicker(new CSV('./data/customers.csv', true), $variables);
$valuesPerVariable = [];
foreach ($variables as $variable) {
$variableValues = array_column(iterator_to_array($selection), $variable);
array_unshift($variableValues, $variable);
$valuesPerVariable[] = $variableValues;
}
$valuesPerVariable[] = $predictions;
// For further analysis : plot, statistics, testing...
$predictionsFile = new CSV('./output/predictions.csv');
$predictionsFile->export(array_map(null, ...$valuesPerVariable));