Passed
Push — master ( 7df88e...37e99a )
by Ruben
01:59
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
        sort($array);
61
        $this->encodedValues = [];
62
        $this->sortedEncodedFeatures = $array;
63
        foreach($this->sortedEncodedFeatures as $col){
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 = [];
0 ignored issues
show
The assignment to $resp is dead and can be removed.
Loading history...
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]);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $data does not exist. Did you maybe mean $ndata?
Loading history...
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