-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathtrain.php
More file actions
33 lines (19 loc) · 733 Bytes
/
train.php
File metadata and controls
33 lines (19 loc) · 733 Bytes
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
<?php
include __DIR__ . '/vendor/autoload.php';
use Rubix\ML\Loggers\Screen;
use Rubix\ML\Datasets\Labeled;
use Rubix\ML\Extractors\NDJSON;
use Rubix\ML\Classifiers\KNearestNeighbors;
use Rubix\ML\CrossValidation\Metrics\Accuracy;
$logger = new Screen();
$logger->info('Loading data into memory');
$dataset = Labeled::fromIterator(new NDJSON('dataset.ndjson'));
[$training, $testing] = $dataset->stratifiedSplit(0.8);
$estimator = new KNearestNeighbors(3);
$logger->info('Training');
$estimator->train($training);
$logger->info('Making predictions');
$predictions = $estimator->predict($testing);
$metric = new Accuracy();
$score = $metric->score($predictions, $testing->labels());
$logger->info("Accuracy is $score");