diff options
-rw-r--r-- | numpy/lib/function_base.py | 8 | ||||
-rw-r--r-- | numpy/lib/tests/test_function_base.py | 14 |
2 files changed, 21 insertions, 1 deletions
diff --git a/numpy/lib/function_base.py b/numpy/lib/function_base.py index 34c136064..02c9b5349 100644 --- a/numpy/lib/function_base.py +++ b/numpy/lib/function_base.py @@ -1970,6 +1970,9 @@ def cov(m, y=None, rowvar=1, bias=0, ddof=None): raise ValueError("ddof must be integer") X = array(m, ndmin=2, dtype=float) + if X.size == 0: + # handle empty arrays + return np.array(m) if X.shape[0] == 1: rowvar = 1 if rowvar: @@ -2017,7 +2020,7 @@ def corrcoef(x, y=None, rowvar=1, bias=0, ddof=None): Parameters ---------- - m : array_like + x : array_like A 1-D or 2-D array containing multiple variables and observations. Each row of `m` represents a variable, and each column a single observation of all those variables. Also see `rowvar` below. @@ -2051,6 +2054,9 @@ def corrcoef(x, y=None, rowvar=1, bias=0, ddof=None): """ c = cov(x, y, rowvar, bias, ddof) + if c.size == 0: + # handle empty arrays + return c try: d = diag(c) except ValueError: # scalar covariance diff --git a/numpy/lib/tests/test_function_base.py b/numpy/lib/tests/test_function_base.py index 353bf23e9..e65c84158 100644 --- a/numpy/lib/tests/test_function_base.py +++ b/numpy/lib/tests/test_function_base.py @@ -944,6 +944,20 @@ class TestCorrCoef(TestCase): assert_almost_equal(corrcoef(self.A, ddof=-1), self.res1) assert_almost_equal(corrcoef(self.A, self.B, ddof=-1), self.res2) + def test_empty(self): + assert_equal(corrcoef(np.array([])).size, 0) + assert_equal(corrcoef(np.array([]).reshape(0, 2)).shape, (0, 2)) + + +class TestCov(TestCase): + def test_basic(self): + x = np.array([[0, 2], [1, 1], [2, 0]]).T + assert_allclose(np.cov(x), np.array([[ 1.,-1.], [-1.,1.]])) + + def test_empty(self): + assert_equal(cov(np.array([])).size, 0) + assert_equal(cov(np.array([]).reshape(0, 2)).shape, (0, 2)) + class Test_i0(TestCase): def test_simple(self): |