Skip to content

Commit e003e82

Browse files
committed
BUG: np.cov transpose control
* Fixes numpy#27658 * Use a more sensible filter for controlling the decision to transpose the design matrix received by `np.cov`. * Add a release note.
1 parent 70fde29 commit e003e82

File tree

3 files changed

+12
-1
lines changed

3 files changed

+12
-1
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
* `numpy.cov` now properly transposes single-row (2d array) design matrices
2+
when ``rowvar=False``. Previously, single-row design matrices would
3+
return a scalar in this scenario, which is not correct, so this
4+
is a behavior change and an array of the appropriate shape will
5+
now be returned.

numpy/lib/_function_base_impl.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2736,7 +2736,7 @@ def cov(m, y=None, rowvar=True, bias=False, ddof=None, fweights=None,
27362736
dtype = np.result_type(m, y, np.float64)
27372737

27382738
X = array(m, ndmin=2, dtype=dtype)
2739-
if not rowvar and X.shape[0] != 1:
2739+
if not rowvar and m.ndim != 1:
27402740
X = X.T
27412741
if X.shape[0] == 0:
27422742
return np.array([]).reshape(0, 0)

numpy/lib/tests/test_function_base.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2509,6 +2509,12 @@ def test_cov_dtype(self, test_type):
25092509
res = cov(cast_x1, dtype=test_type)
25102510
assert test_type == res.dtype
25112511

2512+
def test_gh_27658(self):
2513+
x = np.ones((3, 1))
2514+
expected = np.cov(x, ddof=0, rowvar=True)
2515+
actual = np.cov(x.T, ddof=0, rowvar=False)
2516+
assert_allclose(actual, expected, strict=True)
2517+
25122518

25132519
class Test_I0:
25142520

0 commit comments

Comments
 (0)