DataFrame::std()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 5
c 0
b 0
f 0
nc 3
nop 1
dl 0
loc 12
rs 10
1
<?php
2
3
namespace devfym\IntelliPHP\Data;
4
5
use devfym\IntelliPHP\Math\StatisticInterface;
6
7
class DataFrame implements StatisticInterface
8
{
9
    /**
10
     * @var array
11
     * List of Column names.
12
     */
13
    private $columns = [];
14
15
    /**
16
     * @var int
17
     * Number of Sample.
18
     */
19
    private $index = 0;
20
21
    /**
22
     * DataFrame constructor.
23
     */
24
    public function __construct()
25
    {
26
        //
27
    }
28
29
    /**
30
     * @param array $arr
31
     * @return array
32
     * Transpose array.
33
     */
34
    public function transpose($arr = []) : array
35
    {
36
        $columns = array_keys($arr[0]);
37
38
        $arr = array_map(null, ...$arr);
39
40
        foreach ($columns as $index => $column) {
41
            $arr[$column] = $arr[$index];
42
            unset($arr[$index]);
43
        }
44
45
        return $arr;
46
    }
47
48
    /**
49
     * @param array $arr
50
     * @param bool $transpose
51
     * Store given array-formatted data into series.
52
     */
53
    public function readArray($arr = [], $transpose = false) : void
54
    {
55
56
        if ($transpose === true) {
57
            $arr = $this->transpose($arr);
58
        }
59
60
        // Set Columns
61
        $this->columns = array_keys($arr);
62
63
        // Set Index
64
        $this->index = count($arr[$this->columns[0]]);
65
66
        // Create Series instance for each column.
67
        foreach ($this->columns as $column) {
68
            $this->{$column} = new Series();
69
            $this->{$column}->setSample($arr[$column]);
70
        }
71
    }
72
73
    /**
74
     * @return array
75
     * Get list of column names.
76
     */
77
    public function getColumns() : array
78
    {
79
        return $this->columns;
80
    }
81
82
    public function getNumericColumns() : array {
83
84
        $columns = $this->columns;
85
86
        $numeric = [];
87
88
        foreach ($columns as $col) {
89
            if ($this->{$col}->dataType() == 'Numeric') {
90
                array_push($numeric, $col);
91
            }
92
        }
93
94
        return $numeric;
95
    }
96
97
    /**
98
     * @return int
99
     * Get Number of Sample.
100
     */
101
    public function getIndex() : int
102
    {
103
        return $this->index;
104
    }
105
106
    /**
107
     * @param int $floatPoint
108
     * @return array
109
     * Get list of mean value in DataFrame.
110
     */
111
    public function mean($floatPoint = 1) : array
112
    {
113
        // Initialize mean.
114
        $mean = [];
115
116
        foreach ($this->columns as $column) {
117
            if ($this->{$column}->dataType() == 'Numeric') {
118
                $mean[$column] = $this->{$column}->mean($floatPoint);
119
            }
120
        }
121
122
        return $mean;
123
    }
124
125
    /**
126
     * @param int $floatPoint
127
     * @return array
128
     * Get list of max value in DataFrame.
129
     */
130
    public function max($floatPoint = 2) : array
131
    {
132
        // Initialize max.
133
        $max = [];
134
135
        foreach ($this->columns as $column) {
136
            if ($this->{$column}->dataType() == 'Numeric') {
137
                $max[$column] = $this->{$column}->max($floatPoint);
138
            }
139
        }
140
141
        return $max;
142
    }
143
144
    /**
145
     * @param int $floatPoint
146
     * @return array
147
     * Get list of min value in DataFrame.
148
     */
149
    public function min($floatPoint = 2) : array
150
    {
151
        // Initialize min.
152
        $min = [];
153
154
        foreach ($this->columns as $column) {
155
            if ($this->{$column}->dataType() == 'Numeric') {
156
                $min[$column] = $this->{$column}->min($floatPoint);
157
            }
158
        }
159
160
        return $min;
161
    }
162
163
    /**
164
     * @param int $Q
165
     * @param int $floatPoint
166
     * @return array
167
     * Get list of Q-th quartile value in DataFrame.
168
     */
169
    public function quartile($Q = 1, $floatPoint = 2) : array
170
    {
171
        // Initialize quartile.
172
        $quartile = [];
173
174
        foreach ($this->columns as $column) {
175
            if ($this->{$column}->dataType() == 'Numeric') {
176
                $quartile[$column] = $this->{$column}->quartile($Q, $floatPoint);
177
            }
178
        }
179
180
        return $quartile;
181
    }
182
183
    /**
184
     * @param int $floatPoint
185
     * @return array
186
     * Get list of 2nd Quartile value in DataFrame.
187
     */
188
    public function median($floatPoint = 2) : array
189
    {
190
        return $this->quartile(2, $floatPoint);
191
    }
192
193
    /**
194
     * @param int $floatPoint
195
     * @return array
196
     * Get list of Variance in DataFrame.
197
     */
198
    public function variance($floatPoint = 4) : array
199
    {
200
        // Initialize quartile.
201
        $variance = [];
202
203
        foreach ($this->columns as $column) {
204
            if ($this->{$column}->dataType() == 'Numeric') {
205
                $variance[$column] = $this->{$column}->variance($floatPoint);
206
            }
207
        }
208
209
        return $variance;
210
    }
211
212
    /**
213
     * @param int $floatPoint
214
     * @return array
215
     * Get list of Standard Deviation in DataFrame.
216
     */
217
    public function std($floatPoint = 2) : array
218
    {
219
        // Initialize quartile.
220
        $std = [];
221
222
        foreach ($this->columns as $column) {
223
            if ($this->{$column}->dataType() == 'Numeric') {
224
                $std[$column] = $this->{$column}->std($floatPoint);
225
            }
226
        }
227
228
        return $std;
229
    }
230
}
231