forked from RubixML/Credit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
train.php
62 lines (40 loc) · 1.66 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
61
62
<?php
include __DIR__ . '/vendor/autoload.php';
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\Other\Loggers\Screen;
use Rubix\ML\CrossValidation\Reports\AggregateReport;
use Rubix\ML\CrossValidation\Reports\ConfusionMatrix;
use Rubix\ML\CrossValidation\Reports\MulticlassBreakdown;
use League\Csv\Writer;
use function Rubix\ML\array_transpose;
ini_set('memory_limit', '-1');
echo 'Loading data into memory ...' . PHP_EOL;
$dataset = Labeled::fromIterator(new CSV('dataset.csv', true));
$dataset->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(new Screen('credit'));
echo 'Training ...' . PHP_EOL;
$estimator->train($training);
$losses = $estimator->steps();
$writer = Writer::createFromPath('progress.csv', 'w+');
$writer->insertOne(['loss']);
$writer->insertAll(array_transpose([$losses]));
echo 'Progress saved to progress.csv' . PHP_EOL;
$report = new AggregateReport([
new MulticlassBreakdown(),
new ConfusionMatrix(),
]);
echo 'Making predictions ...' . PHP_EOL;
$predictions = $estimator->predict($testing);
$results = $report->generate($predictions, $testing->labels());
file_put_contents('report.json', json_encode($results, JSON_PRETTY_PRINT));
echo 'Report saved to report.json' . PHP_EOL;