-
Notifications
You must be signed in to change notification settings - Fork 12
/
train.php
60 lines (39 loc) · 1.54 KB
/
train.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
<?php
include __DIR__ . '/vendor/autoload.php';
use Rubix\ML\Loggers\Screen;
use Rubix\ML\Datasets\Labeled;
use Rubix\ML\Extractors\CSV;
use Rubix\ML\Transformers\NumericStringConverter;
use Rubix\ML\Transformers\OneHotEncoder;
use Rubix\ML\Transformers\ZScaleStandardizer;
use Rubix\ML\Classifiers\LogisticRegression;
use Rubix\ML\NeuralNet\Optimizers\StepDecay;
use Rubix\ML\CrossValidation\Reports\AggregateReport;
use Rubix\ML\CrossValidation\Reports\ConfusionMatrix;
use Rubix\ML\CrossValidation\Reports\MulticlassBreakdown;
use Rubix\ML\Persisters\Filesystem;
use function Rubix\ML\array_transpose;
ini_set('memory_limit', '-1');
$logger = new Screen();
$logger->info('Loading data into memory');
$dataset = Labeled::fromIterator(new CSV('dataset.csv', true))
->apply(new NumericStringConverter())
->apply(new OneHotEncoder())
->apply(new ZScaleStandardizer());
[$training, $testing] = $dataset->stratifiedSplit(0.8);
$estimator = new LogisticRegression(128, new StepDecay(0.01, 100));
$estimator->setLogger($logger);
$estimator->train($training);
$extractor = new CSV('progress.csv', true);
$extractor->export($estimator->steps());
$logger->info('Progress saved to progress.csv');
$report = new AggregateReport([
new MulticlassBreakdown(),
new ConfusionMatrix(),
]);
$logger->info('Making predictions');
$predictions = $estimator->predict($testing);
$results = $report->generate($predictions, $testing->labels());
echo $results;
$results->toJSON()->saveTo(new Filesystem('report.json'));
$logger->info('Report saved to report.json');