-
-
Notifications
You must be signed in to change notification settings - Fork 189
/
Copy pathGridSearchTest.php
150 lines (122 loc) · 3.63 KB
/
GridSearchTest.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
<?php
namespace Rubix\ML\Tests;
use Rubix\ML\Learner;
use Rubix\ML\Verbose;
use Rubix\ML\DataType;
use Rubix\ML\Estimator;
use Rubix\ML\GridSearch;
use Rubix\ML\Persistable;
use Rubix\ML\EstimatorType;
use Rubix\ML\Loggers\BlackHole;
use Rubix\ML\CrossValidation\HoldOut;
use Rubix\ML\Kernels\Distance\Euclidean;
use Rubix\ML\Kernels\Distance\Manhattan;
use Rubix\ML\Datasets\Generators\Circle;
use Rubix\ML\Classifiers\KNearestNeighbors;
use Rubix\ML\CrossValidation\Metrics\FBeta;
use Rubix\ML\Datasets\Generators\Agglomerate;
use Rubix\ML\CrossValidation\Metrics\Accuracy;
use PHPUnit\Framework\TestCase;
/**
* @group MetaEstimators
* @covers \Rubix\ML\GridSearch
*/
class GridSearchTest extends TestCase
{
protected const TRAIN_SIZE = 512;
protected const TEST_SIZE = 256;
protected const MIN_SCORE = 0.9;
protected const RANDOM_SEED = 0;
/**
* @var Agglomerate
*/
protected $generator;
/**
* @var GridSearch
*/
protected $estimator;
/**
* @var Accuracy
*/
protected $metric;
/**
* @before
*/
protected function setUp() : void
{
$this->generator = new Agglomerate([
'inner' => new Circle(0.0, 0.0, 1.0, 0.5),
'middle' => new Circle(0.0, 0.0, 5.0, 1.0),
'outer' => new Circle(0.0, 0.0, 10.0, 2.0),
]);
$this->estimator = new GridSearch(KNearestNeighbors::class, [
[1, 5, 10], [true], [new Euclidean(), new Manhattan()],
], new FBeta(), new HoldOut(0.2));
$this->metric = new Accuracy();
srand(self::RANDOM_SEED);
}
protected function assertPreConditions() : void
{
$this->assertFalse($this->estimator->trained());
}
/**
* @test
*/
public function build() : void
{
$this->assertInstanceOf(GridSearch::class, $this->estimator);
$this->assertInstanceOf(Learner::class, $this->estimator);
$this->assertInstanceOf(Verbose::class, $this->estimator);
$this->assertInstanceOf(Persistable::class, $this->estimator);
$this->assertInstanceOf(Estimator::class, $this->estimator);
}
/**
* @test
*/
public function type() : void
{
$this->assertEquals(EstimatorType::classifier(), $this->estimator->type());
}
/**
* @test
*/
public function compatibility() : void
{
$this->assertEquals(DataType::all(), $this->estimator->compatibility());
}
/**
* @test
*/
public function params() : void
{
$expected = [
'class' => KNearestNeighbors::class,
'params' => [
[1, 5, 10], [true], [new Euclidean(), new Manhattan()],
],
'metric' => new FBeta(),
'validator' => new HoldOut(0.2),
];
$this->assertEquals($expected, $this->estimator->params());
}
/**
* @test
*/
public function trainPredictBest() : void
{
$this->estimator->setLogger(new BlackHole());
$training = $this->generator->generate(self::TRAIN_SIZE);
$testing = $this->generator->generate(self::TEST_SIZE);
$this->estimator->train($training);
$this->assertTrue($this->estimator->trained());
$predictions = $this->estimator->predict($testing);
$score = $this->metric->score($predictions, $testing->labels());
$this->assertGreaterThanOrEqual(self::MIN_SCORE, $score);
$expectedBest = [
'k' => 10,
'weighted' => true,
'kernel' => new Manhattan(),
];
$this->assertEquals($expectedBest, $this->estimator->base()->params());
}
}