Completed
Push — master ( 0178b9...6acaef )
by Yasunori
27s queued 10s
created

LinearRegression::saveModel()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 8
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 12
rs 10
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 = '';
0 ignored issues
show
Documentation Bug introduced by
It seems like '' of type string is incompatible with the declared type array of property $xColumn.

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..

Loading history...
45
        $this->yColumn = '';
0 ignored issues
show
Documentation Bug introduced by
It seems like '' of type string is incompatible with the declared type array of property $yColumn.

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..

Loading history...
46
        $this->df = new DataFrame();
0 ignored issues
show
Documentation Bug introduced by
It seems like new devfym\IntelliPHP\Data\DataFrame() of type devfym\IntelliPHP\Data\DataFrame is incompatible with the declared type integer of property $df.

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..

Loading history...
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;
0 ignored issues
show
Documentation Bug introduced by
It seems like $df of type devfym\IntelliPHP\Data\DataFrame is incompatible with the declared type integer of property $df.

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..

Loading history...
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;
0 ignored issues
show
Documentation Bug introduced by
It seems like $xColumn of type string is incompatible with the declared type array of property $xColumn.

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..

Loading history...
67
        $this->yColumn = $yColumn;
0 ignored issues
show
Documentation Bug introduced by
It seems like $yColumn of type string is incompatible with the declared type array of property $yColumn.

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..

Loading history...
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