ImputerTest.php 4.83 KB
Newer Older
1
2
<?php

Arkadiusz Kondas's avatar
Arkadiusz Kondas committed
3
declare(strict_types=1);
4

5
namespace Phpml\Tests\Preprocessing;
6

7
use Phpml\Exception\InvalidOperationException;
8
9
use Phpml\Preprocessing\Imputer;
use Phpml\Preprocessing\Imputer\Strategy\MeanStrategy;
10
11
use Phpml\Preprocessing\Imputer\Strategy\MedianStrategy;
use Phpml\Preprocessing\Imputer\Strategy\MostFrequentStrategy;
Arkadiusz Kondas's avatar
Arkadiusz Kondas committed
12
use PHPUnit\Framework\TestCase;
13

Arkadiusz Kondas's avatar
Arkadiusz Kondas committed
14
class ImputerTest extends TestCase
15
{
Tomáš Votruba's avatar
Tomáš Votruba committed
16
    public function testComplementsMissingValuesWithMeanStrategyOnColumnAxis(): void
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
    {
        $data = [
            [1, null, 3, 4],
            [4, 3, 2, 1],
            [null, 6, 7, 8],
            [8, 7, null, 5],
        ];

        $imputeData = [
            [1, 5.33, 3, 4],
            [4, 3, 2, 1],
            [4.33, 6, 7, 8],
            [8, 7, 4, 5],
        ];

Arkadiusz Kondas's avatar
Arkadiusz Kondas committed
32
        $imputer = new Imputer(null, new MeanStrategy(), Imputer::AXIS_COLUMN, $data);
33
        $imputer->transform($data);
34
35
36
37

        $this->assertEquals($imputeData, $data, '', $delta = 0.01);
    }

Tomáš Votruba's avatar
Tomáš Votruba committed
38
    public function testComplementsMissingValuesWithMeanStrategyOnRowAxis(): void
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
    {
        $data = [
            [1, null, 3, 4],
            [4, 3, 2, 1],
            [null, 6, 7, 8],
            [8, 7, null, 5],
        ];

        $imputeData = [
            [1, 2.66, 3, 4],
            [4, 3, 2, 1],
            [7, 6, 7, 8],
            [8, 7, 6.66, 5],
        ];

Arkadiusz Kondas's avatar
Arkadiusz Kondas committed
54
        $imputer = new Imputer(null, new MeanStrategy(), Imputer::AXIS_ROW, $data);
55
        $imputer->transform($data);
56
57
58

        $this->assertEquals($imputeData, $data, '', $delta = 0.01);
    }
59

Tomáš Votruba's avatar
Tomáš Votruba committed
60
    public function testComplementsMissingValuesWithMediaStrategyOnColumnAxis(): void
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
    {
        $data = [
            [1, null, 3, 4],
            [4, 3, 2, 1],
            [null, 6, 7, 8],
            [8, 7, null, 5],
        ];

        $imputeData = [
            [1, 6, 3, 4],
            [4, 3, 2, 1],
            [4, 6, 7, 8],
            [8, 7, 3, 5],
        ];

Arkadiusz Kondas's avatar
Arkadiusz Kondas committed
76
        $imputer = new Imputer(null, new MedianStrategy(), Imputer::AXIS_COLUMN, $data);
77
        $imputer->transform($data);
78
79
80
81

        $this->assertEquals($imputeData, $data, '', $delta = 0.01);
    }

Tomáš Votruba's avatar
Tomáš Votruba committed
82
    public function testComplementsMissingValuesWithMediaStrategyOnRowAxis(): void
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
    {
        $data = [
            [1, null, 3, 4],
            [4, 3, 2, 1],
            [null, 6, 7, 8],
            [8, 7, null, 5],
        ];

        $imputeData = [
            [1, 3, 3, 4],
            [4, 3, 2, 1],
            [7, 6, 7, 8],
            [8, 7, 7, 5],
        ];

Arkadiusz Kondas's avatar
Arkadiusz Kondas committed
98
        $imputer = new Imputer(null, new MedianStrategy(), Imputer::AXIS_ROW, $data);
99
        $imputer->transform($data);
100
101
102
103

        $this->assertEquals($imputeData, $data, '', $delta = 0.01);
    }

Tomáš Votruba's avatar
Tomáš Votruba committed
104
    public function testComplementsMissingValuesWithMostFrequentStrategyOnColumnAxis(): void
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
    {
        $data = [
            [1, null, 3, 4],
            [4, 3, 2, 1],
            [null, 6, 7, 8],
            [8, 7, null, 5],
            [8, 3, 2, 5],
        ];

        $imputeData = [
            [1, 3, 3, 4],
            [4, 3, 2, 1],
            [8, 6, 7, 8],
            [8, 7, 2, 5],
            [8, 3, 2, 5],
        ];

Arkadiusz Kondas's avatar
Arkadiusz Kondas committed
122
        $imputer = new Imputer(null, new MostFrequentStrategy(), Imputer::AXIS_COLUMN, $data);
123
        $imputer->transform($data);
124
125
126
127

        $this->assertEquals($imputeData, $data);
    }

Tomáš Votruba's avatar
Tomáš Votruba committed
128
    public function testComplementsMissingValuesWithMostFrequentStrategyOnRowAxis(): void
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
    {
        $data = [
            [1, null, 3, 4, 3],
            [4, 3, 2, 1, 7],
            [null, 6, 7, 8, 6],
            [8, 7, null, 5, 5],
            [8, 3, 2, 5, 4],
        ];

        $imputeData = [
            [1, 3, 3, 4, 3],
            [4, 3, 2, 1, 7],
            [6, 6, 7, 8, 6],
            [8, 7, 5, 5, 5],
            [8, 3, 2, 5, 4],
        ];

Arkadiusz Kondas's avatar
Arkadiusz Kondas committed
146
        $imputer = new Imputer(null, new MostFrequentStrategy(), Imputer::AXIS_ROW, $data);
147
        $imputer->transform($data);
148
149
150

        $this->assertEquals($imputeData, $data);
    }
151

Tomáš Votruba's avatar
Tomáš Votruba committed
152
    public function testImputerWorksOnFitSamples(): void
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
    {
        $trainData = [
            [1, 3, 4],
            [6, 7, 8],
            [8, 7, 5],
        ];

        $data = [
            [1, 3, null],
            [6, null, 8],
            [null, 7, 5],
        ];

        $imputeData = [
            [1, 3, 5.66],
            [6, 5.66, 8],
            [5, 7, 5],
        ];

        $imputer = new Imputer(null, new MeanStrategy(), Imputer::AXIS_COLUMN, $trainData);
        $imputer->transform($data);

        $this->assertEquals($imputeData, $data, '', $delta = 0.01);
    }
177
178
179
180
181
182
183
184
185
186
187
188
189
190

    public function testThrowExceptionWhenTryingToTransformWithoutTrainSamples(): void
    {
        $this->expectException(InvalidOperationException::class);

        $data = [
            [1, 3, null],
            [6, null, 8],
            [null, 7, 5],
        ];

        $imputer = new Imputer(null, new MeanStrategy(), Imputer::AXIS_COLUMN);
        $imputer->transform($data);
    }
191
}