LinearRegressionService   A
last analyzed

Complexity

Total Complexity 1

Size/Duplication

Total Lines 32
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 5

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 32
wmc 1
lcom 0
cbo 5
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A train() 0 23 1
1
<?php
2
3
namespace MachineLearning\Application\Service;
4
5
use MachineLearning\Application\Algorithm\GradientDescendent;
6
use MachineLearning\Application\Normalization\MeanScaleNormalization;
7
use MachineLearning\Domain\Hypothesis\Linear;
8
use MachineLearning\Domain\Model\Dataset;
9
use MachineLearning\Domain\Model\Training;
10
11
class LinearRegressionService
12
{
13
14
    /**
15
     * @param array $data
16
     * @return Training
17
     * @throws \MachineLearning\Domain\Exception\DivergenceException
18
     */
19
    public function train($data)
20
    {
21
22
        $hypothesis = new Linear();
23
        $normalization = new MeanScaleNormalization();
24
        $algorithm = new GradientDescendent($hypothesis);
25
        $dataset = new Dataset($data, $hypothesis);
26
27
        $normalizationCoefficient = $normalization->calculateCoefficient($dataset);
28
        $dataset = $normalization->normalizeDataset($dataset, $normalizationCoefficient);
29
30
        $algorithmCoefficient = $algorithm->train($dataset);
31
32
        return new Training(
33
            $data,
34
            $algorithm,
35
            $algorithmCoefficient,
36
            $normalization,
37
            $normalizationCoefficient,
38
            $hypothesis
39
        );
40
41
    }
42
}