FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
arrayfire-python/arrayfire/statistics.py at master · arrayfire/arrayfire-python · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
arrayfire
/
arrayfire-python
Public
Notifications
You must be signed in to change notification settings
Fork
64
Star
422
Code
Issues
57
Pull requests
3
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
arrayfire-python
/
arrayfire
/
statistics.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
303 lines (240 loc) · 8.38 KB
Breadcrumbs
arrayfire-python
/
arrayfire
/
statistics.py
Copy path
File metadata and controls
303 lines (240 loc) · 8.38 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
#######################################################
# Copyright (c) 2015, ArrayFire
# All rights reserved.
#
# This file is distributed under 3-clause BSD license.
# The complete license agreement can be obtained at:
# http://arrayfire.com/licenses/BSD-3-Clause
########################################################
"""
Statistical algorithms (mean, var, stdev, etc).
"""
from
.
library
import
*
from
.
array
import
*
def
mean
(
a
,
weights
=
None
,
dim
=
None
):
"""
Calculate mean along a given dimension.
Parameters
----------
a: af.Array
The input array.
weights: optional: af.Array. default: None.
Array to calculate the weighted mean. Must match size of the
input array.
dim: optional: int. default: None.
The dimension for which to obtain the mean from input data.
Returns
-------
output: af.Array
Array containing the mean of the input array along a given
dimension.
"""
if
dim
is
not
None
:
out
=
Array
()
if
weights
is
None
:
safe_call
(
backend
.
get
().
af_mean
(
c_pointer
(
out
.
arr
),
a
.
arr
,
c_int_t
(
dim
)))
else
:
safe_call
(
backend
.
get
().
af_mean_weighted
(
c_pointer
(
out
.
arr
),
a
.
arr
,
weights
.
arr
,
c_int_t
(
dim
)))
return
out
else
:
real
=
c_double_t
(
0
)
imag
=
c_double_t
(
0
)
if
weights
is
None
:
safe_call
(
backend
.
get
().
af_mean_all
(
c_pointer
(
real
),
c_pointer
(
imag
),
a
.
arr
))
else
:
safe_call
(
backend
.
get
().
af_mean_all_weighted
(
c_pointer
(
real
),
c_pointer
(
imag
),
a
.
arr
,
weights
.
arr
))
real
=
real
.
value
imag
=
imag
.
value
return
real
if
imag
==
0
else
real
+
imag
*
1j
def
var
(
a
,
bias
=
VARIANCE
.
DEFAULT
,
weights
=
None
,
dim
=
None
):
"""
Calculate variance along a given dimension.
Parameters
----------
a: af.Array
The input array.
bias: optional: af.VARIANCE. default: DEFAULT.
population variance(VARIANCE.POPULATION) or sample variance(VARIANCE.SAMPLE).
This is ignored if weights are provided.
weights: optional: af.Array. default: None.
Array to calculate for the weighted mean. Must match size of
the input array.
dim: optional: int. default: None.
The dimension for which to obtain the variance from input data.
Returns
-------
output: af.Array
Array containing the variance of the input array along a given
dimension.
"""
if
dim
is
not
None
:
out
=
Array
()
if
weights
is
None
:
safe_call
(
backend
.
get
().
af_var_v2
(
c_pointer
(
out
.
arr
),
a
.
arr
,
bias
.
value
,
c_int_t
(
dim
)))
else
:
safe_call
(
backend
.
get
().
af_var_weighted
(
c_pointer
(
out
.
arr
),
a
.
arr
,
weights
.
arr
,
c_int_t
(
dim
)))
return
out
else
:
real
=
c_double_t
(
0
)
imag
=
c_double_t
(
0
)
if
weights
is
None
:
safe_call
(
backend
.
get
().
af_var_all_v2
(
c_pointer
(
real
),
c_pointer
(
imag
),
a
.
arr
,
bias
.
value
))
else
:
safe_call
(
backend
.
get
().
af_var_all_weighted
(
c_pointer
(
real
),
c_pointer
(
imag
),
a
.
arr
,
weights
.
arr
))
real
=
real
.
value
imag
=
imag
.
value
return
real
if
imag
==
0
else
real
+
imag
*
1j
def
meanvar
(
a
,
weights
=
None
,
bias
=
VARIANCE
.
DEFAULT
,
dim
=
-
1
):
"""
Calculate mean and variance along a given dimension.
Parameters
----------
a: af.Array
The input array.
weights: optional: af.Array. default: None.
Array to calculate for the weighted mean. Must match size of
the input array.
bias: optional: af.VARIANCE. default: DEFAULT.
population variance(VARIANCE.POPULATION) or
sample variance(VARIANCE.SAMPLE).
dim: optional: int. default: -1.
The dimension for which to obtain the variance from input data.
Returns
-------
mean: af.Array
Array containing the mean of the input array along a given
dimension.
variance: af.Array
Array containing the variance of the input array along a given
dimension.
"""
mean_out
=
Array
()
var_out
=
Array
()
if
weights
is
None
:
weights
=
Array
()
safe_call
(
backend
.
get
().
af_meanvar
(
c_pointer
(
mean_out
.
arr
),
c_pointer
(
var_out
.
arr
),
a
.
arr
,
weights
.
arr
,
bias
.
value
,
c_int_t
(
dim
)))
return
mean_out
,
var_out
def
stdev
(
a
,
bias
=
VARIANCE
.
DEFAULT
,
dim
=
None
):
"""
Calculate standard deviation along a given dimension.
Parameters
----------
a: af.Array
The input array.
bias: optional: af.VARIANCE. default: DEFAULT.
population variance(VARIANCE.POPULATION) or sample variance(VARIANCE.SAMPLE).
This is ignored if weights are provided.
dim: optional: int. default: None.
The dimension for which to obtain the standard deviation from
input data.
Returns
-------
output: af.Array
Array containing the standard deviation of the input array
along a given dimension.
"""
if
dim
is
not
None
:
out
=
Array
()
safe_call
(
backend
.
get
().
af_stdev_v2
(
c_pointer
(
out
.
arr
),
a
.
arr
,
bias
.
value
,
c_int_t
(
dim
)))
return
out
else
:
real
=
c_double_t
(
0
)
imag
=
c_double_t
(
0
)
safe_call
(
backend
.
get
().
af_stdev_all_v2
(
c_pointer
(
real
),
c_pointer
(
imag
),
a
.
arr
,
bias
.
value
))
real
=
real
.
value
imag
=
imag
.
value
return
real
if
imag
==
0
else
real
+
imag
*
1j
def
cov
(
a
,
b
,
bias
=
VARIANCE
.
DEFAULT
):
"""
Calculate covariance along a given dimension.
Parameters
----------
a: af.Array
Input array.
b: af.Array
Input array.
bias: optional: af.VARIANCE. default: DEFAULT.
population variance(VARIANCE.POPULATION) or sample variance(VARIANCE.SAMPLE).
Returns
-------
output: af.Array
Array containing the covariance of the input array along a given dimension.
"""
out
=
Array
()
safe_call
(
backend
.
get
().
af_cov_v2
(
c_pointer
(
out
.
arr
),
a
.
arr
,
b
.
arr
,
bias
.
value
))
return
out
def
median
(
a
,
dim
=
None
):
"""
Calculate median along a given dimension.
Parameters
----------
a: af.Array
The input array.
dim: optional: int. default: None.
The dimension for which to obtain the median from input data.
Returns
-------
output: af.Array
Array containing the median of the input array along a
given dimension.
"""
if
dim
is
not
None
:
out
=
Array
()
safe_call
(
backend
.
get
().
af_median
(
c_pointer
(
out
.
arr
),
a
.
arr
,
c_int_t
(
dim
)))
return
out
else
:
real
=
c_double_t
(
0
)
imag
=
c_double_t
(
0
)
safe_call
(
backend
.
get
().
af_median_all
(
c_pointer
(
real
),
c_pointer
(
imag
),
a
.
arr
))
real
=
real
.
value
imag
=
imag
.
value
return
real
if
imag
==
0
else
real
+
imag
*
1j
def
corrcoef
(
x
,
y
):
"""
Calculate the correlation coefficient of the input arrays.
Parameters
----------
x: af.Array
The first input array.
y: af.Array
The second input array.
Returns
-------
output: af.Array
Array containing the correlation coefficient of the input arrays.
"""
real
=
c_double_t
(
0
)
imag
=
c_double_t
(
0
)
safe_call
(
backend
.
get
().
af_corrcoef
(
c_pointer
(
real
),
c_pointer
(
imag
),
x
.
arr
,
y
.
arr
))
real
=
real
.
value
imag
=
imag
.
value
return
real
if
imag
==
0
else
real
+
imag
*
1j
def
topk
(
data
,
k
,
dim
=
0
,
order
=
TOPK
.
DEFAULT
):
"""
Return top k elements along a single dimension.
Parameters
----------
data: af.Array
Input array to return k elements from.
k: scalar. default: 0
The number of elements to return from input array.
dim: optional: scalar. default: 0
The dimension along which the top k elements are
extracted. Note: at the moment, topk() only supports the
extraction of values along the first dimension.
order: optional: af.TOPK. default: af.TOPK.DEFAULT
The ordering of k extracted elements. Defaults to top k max values.
Returns
-------
values: af.Array
Top k elements from input array.
indices: af.Array
Corresponding index array to top k elements.
"""
values
=
Array
()
indices
=
Array
()
safe_call
(
backend
.
get
().
af_topk
(
c_pointer
(
values
.
arr
),
c_pointer
(
indices
.
arr
),
data
.
arr
,
k
,
c_int_t
(
dim
),
order
.
value
))
return
values
,
indices
Back
|
FazBrowse Home
|
New Git URL