Passed
Push — master ( 120e65...4f9e14 )
by Ruben
01:52
created

src/CategoricalDataset.php (2 issues)

1
<?php
2
/**
3
 *
4
 * (c) Ruben Dorado <[email protected]>
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
namespace SiteAnalyzer;
10
/**
11
 * class CategoricalDataset
12
 *
13
 * @package   SiteAnalyzer
14
 * @author    Ruben Dorado <[email protected]>
15
 * @copyright 2018 Ruben Dorado
16
 * @license   http://www.opensource.org/licenses/MIT The MIT License
17
 */
18
class CategoricalDataset
19
{
20
 
21
    /**
22
     * @var array
23
     */
24
    protected $data;    
25
    
26
    /**
27
     * @var array
28
     */
29
    protected $sortedEncodedFeatures;        
30
31
    /**
32
     * @var array
33
     */
34
    protected $encodedValues;      
35
   
36
    /**
37
     * @var array
38
     */
39
    protected $featEncode;
40
    
41
    /**
42
     * @var array
43
     */
44
    protected $featIndexMap;
45
    
46
    
47
    /*
48
     * @param
49
     */        
50
    public function __construct($data) 
51
    {
52
        $this->data = $data;
53
    }
54
    
55
    /*
56
     * @param
57
     */    
58
    public function setEncodedFeatures($array) 
59
    {
60
        $array = sort($array);
61
        $this->encodedValues = [];
62
        $this->sortedEncodedFeatures = $array;
0 ignored issues
show
Documentation Bug introduced by Ruben Dorado
It seems like $array of type boolean is incompatible with the declared type array of property $sortedEncodedFeatures.

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...
63
        foreach($this->sortedEncodedFeatures as $col){
0 ignored issues
show
The expression $this->sortedEncodedFeatures of type boolean is not traversable.
Loading history...
64
            $vals = $this->getUniqueValues($col);
65
            $this->encodedValues[] = $vals;
66
            $this->featIndexMap[$col] = count($vals);
67
            $this->featEncode[$col] = $this->encodeFeature(count($vals));
68
            //$this->featDecode[$col] = function($val, $arr){ return $this->getDecodedFeature($val, $arr, ); }
69
            //$this->newEncodedSize += count($vals)-1;
70
        }
71
        
72
        /*for ($i=0;$i<$this->newEncodedSize:$i++) {
73
            
74
        }*/
75
    }   
76
    
77
    /*
78
     * @param
79
     */
80
    private function getUniqueValues($col) 
81
    {
82
        $resp = [];
83
        $resp = Matrix::getColumn($this->data, $col);
84
        $resp = array_unique($resp);
85
        return $resp;
86
    }
87
    
88
    
89
    /*
90
     * @param
91
     */
92
    private function encodeFeature($size) 
93
    {
94
        $resp = [];
95
        for ($i=0;$i<$size;$i++) {
96
            $tmp = array_fill(0, $size, 0);
97
            $tmp[$i] = 1;  
98
            $resp[] = $tmp;
99
        }
100
        return $resp;
101
    }
102
    
103
    /*
104
     * @param
105
     */  
106
    public function encode(){
107
        $transformer  = [];
108
        $n = count($this->data);
109
        $ndim = count($this->data[0]);
110
        for ($j=0; $j<$ndim; $j++) {
111
            $transformer[] = function($val){ return [$val]; };
112
        }
113
        foreach($this->sortedEncodedFeatures as $col) {
114
            $transformer[$col] = function($val) { return $this->featEncode[$col][$val]; };
115
        }
116
        $ndata = [];
117
        for ($i=0; $i<$n; $i++) {
118
            $npoint = [];
119
            for ($j=0; $j<$ndim; $j++) {
120
                $npoint += $transformer[$j]($data[$i][$j]);
121
            }
122
            $ndata[] = $npoint;
123
        }
124
        return $ndata;
125
    }
126
    
127
    /*
128
     * @param
129
     *
130
    function decode($ndata){
131
        $resp = [];
132
        foreach ($ndata as $row) {             
133
            $resp[] = $this->decodeRow($row);
134
        }
135
        return $resp;
136
    }
137
138
    /*
139
     * @param
140
     *     
141
    function decodeRow($row){
142
        $resp = [];
143
        $n = count($row);
144
        for ($i=0; $i<$n; $i++) {
145
            $resp[] = $this->decodeFeature($i, $row);
146
        }
147
        return $resp;
148
    }*/
149
    
150
}
151