8000 ENH: add dtype option to numpy.lib.function_base.cov and corrcoef by lschwetlick · Pull Request #17456 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

ENH: add dtype option to numpy.lib.function_base.cov and corrcoef #17456

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 15 commits into from
Oct 9, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
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
5 changes: 5 additions & 0 deletions doc/release/upcoming_changes/17456.new_feature.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
``dtype`` option for `cov` and `corrcoef`
----------------------------------------------------
The ``dtype`` option is now available for `numpy.cov` and `numpy.corrcoef`.
It specifies which data-type the returned result should have.
By default the functions still return a `numpy.float64` result.
33 changes: 24 additions & 9 deletions numpy/lib/function_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -2268,13 +2268,13 @@ def _vectorize_call_with_signature(self, func, args):


def _cov_dispatcher(m, y=None, rowvar=None, bias=None, ddof=None,
fweights=None, aweights=None):
fweights=None, aweights=None, *, dtype=None):
return (m, y, fweights, aweights)


@array_function_dispatch(_cov_dispatcher)
def cov(m, y=None, rowvar=True, bias=False, ddof=None, fweights=None,
aweights=None):
aweights=None, *, dtype=None):
"""
Estimate a covariance matrix, given data and weights.

Expand Down Expand Up @@ -2325,6 +2325,11 @@ def cov(m, y=None, rowvar=True, bias=False, ddof=None, fweights=None,
weights can be used to assign probabilities to observation vectors.

.. versionadded:: 1.10
dtype : data-type, optional
Data-type of the result. By default, the return data-type will have
at least `numpy.float64` precision.

.. versionadded:: 1.20

Returns
-------
Expand Down Expand Up @@ -2400,13 +2405,16 @@ def cov(m, y=None, rowvar=True, bias=False, ddof=None, fweights=None,
if m.ndim > 2:
raise ValueError("m has more than 2 dimensions")

if y is None:
dtype = np.result_type(m, np.float64)
else:
if y is not None:
y = np.asarray(y)
if y.ndim > 2:
raise ValueError("y has more than 2 dimensions")
dtype = np.result_type(m, y, np.float64)

if dtype is None:
if y is None:
dtype = np.result_type(m, np.float64)
else:
dtype = np.result_type(m, y, np.float64)

X = array(m, ndmin=2, dtype=dtype)
if not rowvar and X.shape[0] != 1:
Expand Down Expand Up @@ -2486,12 +2494,14 @@ def cov(m, y=None, rowvar=True, bias=False, ddof=None, fweights=None,
return c.squeeze()


def _corrcoef_dispatcher(x, y=None, rowvar=None, bias=None, ddof=None):
def _corrcoef_dispatcher(x, y=None, rowvar=None, bias=None, ddof=None, *,
dtype=None):
return (x, y)


@array_function_dispatch(_corrcoef_dispatcher)
def corrcoef(x, y=None, rowvar=True, bias=np._NoValue, ddof=np._NoValue):
def corrcoef(x, y=None, rowvar=True, bias=np._NoValue, ddof=np._NoValue, *,
dtype=None):
"""
Return Pearson product-moment correlation coefficients.

Expand Down Expand Up @@ -2525,6 +2535,11 @@ def corrcoef(x, y=None, rowvar=True, bias=np._NoValue, ddof=np._NoValue):
Has no effect, do not use.

.. deprecated:: 1.10.0
dtype : data-type, optional
Data-type of the result. By default, the return data-type will have
at least `numpy.float64` precision.

.. versionadded:: 1.20

Returns
-------
Expand Down Expand Up @@ -2616,7 +2631,7 @@ def corrcoef(x, y=None, rowvar=True, bias=np._NoValue, ddof=np._NoValue):
# 2015-03-15, 1.10
warnings.warn('bias and ddof have no effect and are deprecated',
DeprecationWarning, stacklevel=3)
c = cov(x, y, rowvar)
c = cov(x, y, rowvar, dtype=dtype)
try:
d = diag(c)
except ValueError:
Expand Down
12 changes: 12 additions & 0 deletions numpy/lib/tests/test_function_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -2023,6 +2023,12 @@ def test_extreme(self):
assert_array_almost_equal(c, np.array([[1., -1.], [-1., 1.]]))
assert_(np.all(np.abs(c) <= 1.0))

@pytest.mark.parametrize("test_type", [np.half, np.single, np.double, np.longdouble])
def test_corrcoef_dtype(self, test_type):
cast_A = self.A.astype(test_type)
res = corrcoef(cast_A, dtype=test_type)
assert test_type == res.dtype


class TestCov:
x1 = np.array([[0, 2], [1, 1], [2, 0]]).T
Expand Down Expand Up @@ -2123,6 +2129,12 @@ def test_unit_fweights_and_aweights(self):
aweights=self.unit_weights),
self.res1)

@pytest.mark.parametrize("test_type", [np.half, np.single, np.double, np.longdouble])
def test_cov_dtype(self, test_type):
cast_x1 = self.x1.astype(test_type)
res = cov(cast_x1, dtype=test_type)
assert test_type == res.dtype


class Test_I0:

Expand Down
0