1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace devfym\IntelliPHP\Regression; |
4
|
|
|
|
5
|
|
|
use devfym\IntelliPHP\Data\DataFrame; |
6
|
|
|
use devfym\IntelliPHP\Math\Validation; |
7
|
|
|
|
8
|
|
|
class LinearRegression |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* @var array |
12
|
|
|
*/ |
13
|
|
|
private $xColumn; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @var array |
17
|
|
|
*/ |
18
|
|
|
private $yColumn; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var int |
22
|
|
|
*/ |
23
|
|
|
private $df; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var float |
27
|
|
|
*/ |
28
|
|
|
private $slope; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var float |
32
|
|
|
*/ |
33
|
|
|
private $intercept; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var String |
37
|
|
|
*/ |
38
|
|
|
private $metric; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* LinearRegression constructor. |
42
|
|
|
*/ |
43
|
|
|
public function __construct() |
44
|
|
|
{ |
45
|
|
|
$this->xColumn = ''; |
|
|
|
|
46
|
|
|
$this->yColumn = ''; |
|
|
|
|
47
|
|
|
$this->df = new DataFrame(); |
|
|
|
|
48
|
|
|
$this->slope = 0; |
49
|
|
|
$this->intercept = 0; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param DataFrame $df |
54
|
|
|
*/ |
55
|
|
|
public function setTrain(DataFrame $df) : void |
56
|
|
|
{ |
57
|
|
|
$this->df = $df; |
|
|
|
|
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @param string $xColumn |
62
|
|
|
* @param string $yColumn |
63
|
|
|
* @param string $metric |
64
|
|
|
*/ |
65
|
|
|
public function model($xColumn = '', $yColumn = '', $metric = '') : void |
66
|
|
|
{ |
67
|
|
|
$this->xColumn = $xColumn; |
|
|
|
|
68
|
|
|
$this->yColumn = $yColumn; |
|
|
|
|
69
|
|
|
|
70
|
|
|
$mx = $this->df->{$xColumn}->mean(); |
71
|
|
|
$my = $this->df->{$yColumn}->mean(); |
72
|
|
|
|
73
|
|
|
$this->slope = $my / $mx; |
74
|
|
|
|
75
|
|
|
$this->intercept = $my - ($mx * $this->slope); |
76
|
|
|
|
77
|
|
|
$this->metric = $metric; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @param array $x |
82
|
|
|
* @return array |
83
|
|
|
*/ |
84
|
|
|
public function predict($x = []) : array |
85
|
|
|
{ |
86
|
|
|
$y = []; |
87
|
|
|
|
88
|
|
|
foreach ($x as $p) { |
89
|
|
|
array_push($y, round(($p * $this->slope) + $this->intercept, 2)); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
return $y; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @param $expected |
97
|
|
|
* @param $actual |
98
|
|
|
* @return mixed |
99
|
|
|
*/ |
100
|
|
|
public function validate($expected, $actual) |
101
|
|
|
{ |
102
|
|
|
switch ($this->metric) { |
103
|
|
|
|
104
|
|
|
case 'mean_squared_error': |
105
|
|
|
case 'mse': |
106
|
|
|
return Validation::MeanSquaredError($expected, $actual); |
107
|
|
|
|
108
|
|
|
case 'root_mean_squared_error': |
109
|
|
|
case 'rmse': |
110
|
|
|
return Validation::RootMeanSquaredError($expected, $actual); |
111
|
|
|
|
112
|
|
|
default: |
113
|
|
|
return NULL; |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
public function saveModel() : string |
118
|
|
|
{ |
119
|
|
|
$model = [ |
120
|
|
|
'type' => 'LinearRegression', |
121
|
|
|
'xColumn' => $this->xColumn, |
122
|
|
|
'yColumn' => $this->yColumn, |
123
|
|
|
'slope' => $this->slope, |
124
|
|
|
'intercept' => $this->intercept, |
125
|
|
|
'metric' => $this->metric |
126
|
|
|
]; |
127
|
|
|
|
128
|
|
|
return json_encode($model); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
public function loadModel($model = '') : void |
132
|
|
|
{ |
133
|
|
|
$model = json_decode($model); |
134
|
|
|
|
135
|
|
|
$this->xColumn = $model->xColumn; |
136
|
|
|
$this->yColumn = $model->yColumn; |
137
|
|
|
$this->slope = $model->slope; |
138
|
|
|
$this->intercept = $model->intercept; |
139
|
|
|
$this->metric = $model->metric; |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..