Completed
Push — master ( f0a798...cf222b )
by Arkadiusz
02:58
created

DecisionStump   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 3
dl 0
loc 45
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A train() 0 13 4
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Phpml\Classification\Linear;
6
7
use Phpml\Helper\Predictable;
8
use Phpml\Helper\Trainable;
9
use Phpml\Classification\Classifier;
10
use Phpml\Classification\DecisionTree;
11
12
class DecisionStump extends DecisionTree
13
{
14
    use Trainable, Predictable;
15
16
    /**
17
     * @var int
18
     */
19
    protected $columnIndex;
20
21
22
    /**
23
     * A DecisionStump classifier is a one-level deep DecisionTree. It is generally
24
     * used with ensemble algorithms as in the weak classifier role. <br>
25
     *
26
     * If columnIndex is given, then the stump tries to produce a decision node
27
     * on this column, otherwise in cases given the value of -1, the stump itself
28
     * decides which column to take for the decision (Default DecisionTree behaviour)
29
     *
30
     * @param int $columnIndex
31
     */
32
    public function __construct(int $columnIndex = -1)
33
    {
34
        $this->columnIndex = $columnIndex;
35
36
        parent::__construct(1);
37
    }
38
39
    /**
40
     * @param array $samples
41
     * @param array $targets
42
     */
43
    public function train(array $samples, array $targets)
44
    {
45
        // Check if a column index was given
46
        if ($this->columnIndex >= 0 && $this->columnIndex > count($samples[0]) - 1) {
47
            $this->columnIndex = -1;
48
        }
49
50
        if ($this->columnIndex >= 0) {
51
            $this->setSelectedFeatures([$this->columnIndex]);
52
        }
53
54
        parent::train($samples, $targets);
55
    }
56
}
57