Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

BUG: np.cov transpose control #27661

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 1 commit into from
Oct 29, 2024
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
BUG: np.cov transpose control
* Fixes #27658

* Use a more sensible filter for controlling
the decision to transpose the design matrix
received by `np.cov`.

* Add a release note.
  • Loading branch information
tylerjereddy committed Oct 29, 2024
commit e003e823e4c9add852a854b10cfa1fc382e3aa7f
5 changes: 5 additions & 0 deletions 5 doc/release/upcoming_changes/27661.compatibility.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
* `numpy.cov` now properly transposes single-row (2d array) design matrices
when ``rowvar=False``. Previously, single-row design matrices would
return a scalar in this scenario, which is not correct, so this
is a behavior change and an array of the appropriate shape will
now be returned.
2 changes: 1 addition & 1 deletion 2 numpy/lib/_function_base_impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -2736,7 +2736,7 @@ def cov(m, y=None, rowvar=True, bias=False, ddof=None, fweights=None,
dtype = np.result_type(m, y, np.float64)

X = array(m, ndmin=2, dtype=dtype)
if not rowvar and X.shape[0] != 1:
if not rowvar and m.ndim != 1:
X = X.T
if X.shape[0] == 0:
return np.array([]).reshape(0, 0)
Expand Down
6 changes: 6 additions & 0 deletions 6 numpy/lib/tests/test_function_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -2509,6 +2509,12 @@ def test_cov_dtype(self, test_type):
res = cov(cast_x1, dtype=test_type)
assert test_type == res.dtype

def test_gh_27658(self):
x = np.ones((3, 1))
expected = np.cov(x, ddof=0, rowvar=True)
actual = np.cov(x.T, ddof=0, rowvar=False)
assert_allclose(actual, expected, strict=True)


class Test_I0:

Expand Down
Loading
Morty Proxy This is a proxified and sanitized view of the page, visit original site.