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