diff options
Diffstat (limited to 'numpy')
-rw-r--r-- | numpy/lib/function_base.py | 46 | ||||
-rw-r--r-- | numpy/lib/tests/test_function_base.py | 52 |
2 files changed, 71 insertions, 27 deletions
diff --git a/numpy/lib/function_base.py b/numpy/lib/function_base.py index ae3d3d84c..a70f74f60 100644 --- a/numpy/lib/function_base.py +++ b/numpy/lib/function_base.py @@ -23,7 +23,7 @@ from numpy.core.numeric import ( newaxis, intp, integer, isscalar ) from numpy.core.umath import ( - pi, multiply, add, arctan2, frompyfunc, isnan, cos, less_equal, sqrt, sin, + pi, multiply, add, arctan2, frompyfunc, cos, less_equal, sqrt, sin, mod, exp, log10 ) from numpy.core.fromnumeric import ( @@ -350,8 +350,8 @@ def histogramdd(sample, bins=10, range=None, normed=False, weights=None): dedges[i] = diff(edges[i]) if np.any(np.asarray(dedges[i]) <= 0): raise ValueError( - "Found bin edge of size <= 0. Did you specify `bins` with" - "non-monotonic sequence?") + "Found bin edge of size <= 0. Did you specify `bins` with" + "non-monotonic sequence?") nbin = asarray(nbin) @@ -1809,36 +1809,40 @@ 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) + # Handles complex arrays too + 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 X.shape[0] == 1: rowvar = 1 if rowvar: + N = X.shape[1] axis = 0 tup = (slice(None), newaxis) else: + N = X.shape[0] axis = 1 tup = (newaxis, slice(None)) - if y is not None: - y = array(y, copy=False, ndmin=2, dtype=float) - X = concatenate((X, y), axis) - - X -= X.mean(axis=1-axis)[tup] - if rowvar: - N = X.shape[1] - else: - N = X.shape[0] - + # check ddof if ddof is None: if bias == 0: ddof = 1 else: ddof = 0 fact = float(N - ddof) + if fact <= 0: + warnings.warn("Degrees of freedom <= 0 for slice", RuntimeWarning) + fact = 0.0 + if y is not None: + y = array(y, copy=False, ndmin=2, dtype=dtype) + X = concatenate((X, y), axis) + + X -= X.mean(axis=1-axis)[tup] if not rowvar: return (dot(X.T, X.conj()) / fact).squeeze() else: @@ -1893,14 +1897,12 @@ 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 - return 1 - return c/sqrt(multiply.outer(d, d)) + # nan if incorrect value (nan, inf, 0), 1 otherwise + return c / c + return c / sqrt(multiply.outer(d, d)) def blackman(M): diff --git a/numpy/lib/tests/test_function_base.py b/numpy/lib/tests/test_function_base.py index 8b239d7c0..f91ab8aa1 100644 --- a/numpy/lib/tests/test_function_base.py +++ b/numpy/lib/tests/test_function_base.py @@ -1189,19 +1189,61 @@ 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_complex(self): + x = np.array([[1, 2, 3], [1j, 2j, 3j]]) + assert_allclose(corrcoef(x), np.array([[1., -1.j], [1.j, 1.]])) + + def test_xy(self): + x = np.array([[1, 2, 3]]) + y = np.array([[1j, 2j, 3j]]) + assert_allclose(np.corrcoef(x, y), np.array([[1., -1.j], [1.j, 1.]])) + def test_empty(self): - assert_equal(corrcoef(np.array([])).size, 0) - assert_equal(corrcoef(np.array([]).reshape(0, 2)).shape, (0, 2)) + with warnings.catch_warnings(): + warnings.simplefilter('ignore', RuntimeWarning) + assert_array_equal(corrcoef(np.array([])), np.nan) + assert_array_equal(corrcoef(np.array([]).reshape(0, 2)), + np.array([]).reshape(0, 0)) + assert_array_equal(corrcoef(np.array([]).reshape(2, 0)), + np.array([[np.nan, np.nan], [np.nan, np.nan]])) + + def test_wrong_ddof(self): + x = np.array([[0, 2], [1, 1], [2, 0]]).T + with warnings.catch_warnings(): + warnings.simplefilter('ignore', RuntimeWarning) + assert_array_equal(corrcoef(x, ddof=5), + np.array([[np.nan, np.nan], [np.nan, np.nan]])) 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.]])) + assert_allclose(cov(x), np.array([[1., -1.], [-1., 1.]])) + + def test_complex(self): + x = np.array([[1, 2, 3], [1j, 2j, 3j]]) + assert_allclose(cov(x), np.array([[1., -1.j], [1.j, 1.]])) + + def test_xy(self): + x = np.array([[1, 2, 3]]) + y = np.array([[1j, 2j, 3j]]) + assert_allclose(cov(x, y), np.array([[1., -1.j], [1.j, 1.]])) def test_empty(self): - assert_equal(cov(np.array([])).size, 0) - assert_equal(cov(np.array([]).reshape(0, 2)).shape, (0, 2)) + with warnings.catch_warnings(): + warnings.simplefilter('ignore', RuntimeWarning) + assert_array_equal(cov(np.array([])), np.nan) + assert_array_equal(cov(np.array([]).reshape(0, 2)), + np.array([]).reshape(0, 0)) + assert_array_equal(cov(np.array([]).reshape(2, 0)), + np.array([[np.nan, np.nan], [np.nan, np.nan]])) + + def test_wrong_ddof(self): + x = np.array([[0, 2], [1, 1], [2, 0]]).T + with warnings.catch_warnings(): + warnings.simplefilter('ignore', RuntimeWarning) + assert_array_equal(cov(x, ddof=5), + np.array([[np.inf, -np.inf], [-np.inf, np.inf]])) class Test_I0(TestCase): |