Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
naive numpy.cov implementation
  • Loading branch information
Alexander-Makaryev committed Sep 7, 2020
commit 9af254efb855a8e6098b711ee41f3824fab6617c
24 changes: 24 additions & 0 deletions dpnp/backend_statistics.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,34 @@ from dpnp.dpnp_utils cimport checker_throw_type_error, normalize_axis


__all__ += [
"dpnp_cov",
"dpnp_mean"
]


cpdef dparray dpnp_cov(dparray array1):
cdef dparray mean = dparray(array1.shape[0], dtype=array1.dtype)
cdef dparray X = dparray(array1.shape, dtype=array1.dtype)

# mean(array1, axis=1) #################################
for i in range(array1.shape[0]):
sum = 0.0
for j in range(array1.shape[1]):
sum += array1[i,j]
mean[i] = sum/array1.shape[1]
########################################################
#X = array1 - mean[:, None]
#X = array1 - mean[:, numpy.newaxis]
#X = array1 - mean.reshape((array1.shape[0], 1))
for i in range(array1.shape[0]):
for j in range(array1.shape[1]):
X[i,j] = array1[i,j] - mean[i]
########################################################
Y = X.transpose()
res = dpnp_matmul(X,Y)
return res/(array1.shape[1]-1)


cpdef dparray dpnp_mean(dparray a, axis):
cdef dparray_shape_type shape_a = a.shape
cdef long size_a = a.size
Expand Down
14 changes: 14 additions & 0 deletions dpnp/dpnp_iface_statistics.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,24 @@
from dpnp.dpnp_utils import checker_throw_value_error, use_origin_backend

__all__ = [
'cov',
'mean'
]


def cov(in_array1):
"""
Estimate a covariance matrix, given data and weights.
"""

is_dparray1 = isinstance(in_array1, dparray)

if (not use_origin_backend(in_array1) and is_dparray1):

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

return dpnp_cov(in_array1)

return numpy.cov(in_array1)


def mean(a, axis=None):
"""
Compute the arithmetic mean along the specified axis.
Expand Down