1
|
|
|
<?php |
2
|
|
|
namespace MachineLearning\Domain\Model; |
3
|
|
|
|
4
|
|
|
use MachineLearning\Application\Algorithm\AlgorithmInterface; |
5
|
|
|
use MachineLearning\Application\Normalization\NormalizationInterface; |
6
|
|
|
use MachineLearning\Domain\Hypothesis\HypothesisInterface; |
7
|
|
|
|
8
|
|
|
class Training |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* @var array |
12
|
|
|
*/ |
13
|
|
|
private $data; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @var ValueInterface |
17
|
|
|
*/ |
18
|
|
|
private $algorithmCoefficient; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var AlgorithmInterface |
22
|
|
|
*/ |
23
|
|
|
private $algorithm; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var ValueInterface |
27
|
|
|
*/ |
28
|
|
|
private $normalizationCoefficient; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var NormalizationInterface |
32
|
|
|
*/ |
33
|
|
|
private $normalization; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var HypothesisInterface |
37
|
|
|
*/ |
38
|
|
|
private $hypothesis; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Training constructor. |
42
|
|
|
* @param array $data |
43
|
|
|
* @param ValueInterface $algorithmCoefficient |
44
|
|
|
* @param AlgorithmInterface $algorithm |
45
|
|
|
* @param ValueInterface $normalizationCoefficient |
46
|
|
|
* @param NormalizationInterface $normalization |
47
|
|
|
* @param HypothesisInterface $hypothesis |
48
|
|
|
*/ |
49
|
|
|
public function __construct( |
50
|
|
|
array $data, |
51
|
|
|
AlgorithmInterface $algorithm, |
52
|
|
|
ValueInterface $algorithmCoefficient, |
53
|
|
|
NormalizationInterface $normalization, |
54
|
|
|
ValueInterface $normalizationCoefficient, |
55
|
|
|
HypothesisInterface $hypothesis |
56
|
|
|
) { |
57
|
|
|
$this->data = $data; |
58
|
|
|
$this->algorithmCoefficient = $algorithmCoefficient; |
59
|
|
|
$this->algorithm = $algorithm; |
60
|
|
|
$this->normalizationCoefficient = $normalizationCoefficient; |
61
|
|
|
$this->normalization = $normalization; |
62
|
|
|
$this->hypothesis = $hypothesis; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @return array |
67
|
|
|
*/ |
68
|
|
|
public function getData() |
69
|
|
|
{ |
70
|
|
|
return $this->data; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @return ValueInterface |
75
|
|
|
*/ |
76
|
|
|
public function getAlgorithmCoefficient() |
77
|
|
|
{ |
78
|
|
|
return $this->algorithmCoefficient; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @return AlgorithmInterface |
83
|
|
|
*/ |
84
|
|
|
public function getAlgorithm() |
85
|
|
|
{ |
86
|
|
|
return $this->algorithm; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @return ValueInterface |
91
|
|
|
*/ |
92
|
|
|
public function getNormalizationCoefficient() |
93
|
|
|
{ |
94
|
|
|
return $this->normalizationCoefficient; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @return NormalizationInterface |
99
|
|
|
*/ |
100
|
|
|
public function getNormalization() |
101
|
|
|
{ |
102
|
|
|
return $this->normalization; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @return HypothesisInterface |
107
|
|
|
*/ |
108
|
|
|
public function getHypothesis() |
109
|
|
|
{ |
110
|
|
|
return $this->hypothesis; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @param ValueInterface |
115
|
|
|
* @return ValueInterface |
116
|
|
|
*/ |
117
|
|
|
public function predict(ValueInterface $value) |
118
|
|
|
{ |
119
|
|
|
$normalizedValue = $this->normalization->normalizeValue($value, $this->normalizationCoefficient); |
120
|
|
|
return $this->hypothesis->calculate($this->algorithmCoefficient, $normalizedValue); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
} |
124
|
|
|
|