Skip to content
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
6 changes: 6 additions & 0 deletions doc/release/1.14.0-notes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ This translation now gives a warning explaining what translation is occuring.
In future, the translation will be disabled, and the first example will be made
equivalent to the second.

``np.linalg.lstsq`` default for ``rcond`` will be changed
---------------------------------------------------------

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't need this blank line.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But doesn't hurt, I will edit the notes later in any case.

The ``rcond`` parameter to ``np.linalg.lstsq`` will change its default to the
better value of machine precision times the maximum of the input matrix
dimensions. A FutureWarning is given if the parameter is not passed explicitly.

Build System Changes
====================
Expand Down
23 changes: 22 additions & 1 deletion numpy/linalg/linalg.py
Original file line number Diff line number Diff line change
Expand Up @@ -1810,7 +1810,7 @@ def det(a):

# Linear Least Squares

def lstsq(a, b, rcond=-1):
def lstsq(a, b, rcond="warn"):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would be better as np.NoValue_<, I think

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At least, that's consistent with how I implemented this kind of thing for the axis argument to MaskedArray.argmin

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thought about that at some point, but then decided that the NoValue thingy actually has a very specific purpose for functions dispatching to methods, and "warn" gives a much clearer idea when looking at the signature.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could argue, NoValue means the same in the sense of "not passed in", but I just don't think we need a singleton like this here when a meaningful string will do. Just my opinion though.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think @eric-wieser has a point. The original motivation for _NoValue was

The idea is to make it an implementation detail and minimize the API
surface area - people can't intentionally pass _NotGiven, so we don't find
ourselves down the line having to specially support people explicitly
passing None.

So the argument here is that if someone passes 'warn' on purpose we will break their code if we drop it a some point.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

True, I don't feel much either way. Thought the main point of NoValue was for thing like keepdims where there is no other option, but its a fair point that it hinders someone from thinking that it is permanent API. Do we use this consistently now also for this use case?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was invented mostly for "keepdims", so I don't know that we really have a tradition yet. I don't feel strongly either way about using it in this case. I thought "warn" was a bit unusual, but not particularly problematic. I confess to a certain weakness for things that are just a bit different, and this was one of them.

"""
Return the least-squares solution to a linear matrix equation.

Expand All @@ -1836,6 +1836,13 @@ def lstsq(a, b, rcond=-1):
as zero if they are smaller than `rcond` times the largest singular
value of `a`.

.. versionchanged:: 1.14.0
If not set, a FutureWarning is given. The previous default
of ``-1`` will use the machine precision as `rcond` parameter,
the new default will use the machine precision times `max(M, N)`.
To silence the warning and use the new default, use ``rcond=None``,
to keep using the old behavior, use ``rcond=-1``.

Returns
-------
x : {(N,), (N, K)} ndarray
Expand Down Expand Up @@ -1909,6 +1916,20 @@ def lstsq(a, b, rcond=-1):
if m != b.shape[0]:
raise LinAlgError('Incompatible dimensions')
t, result_t = _commonType(a, b)
# Determine default rcond value
if rcond == "warn":
# 2017-08-19, 1.14.0
warnings.warn("`rcond` parameter will change to the default of "
"machine precision times ``max(M, N)`` where M and N "
"are the input matrix dimensions.\n"
"To use the future default and silence this warning "
"we advise to pass `rcond=None`, to keep using the old, "
"explicitly pass `rcond=-1`.",
FutureWarning, stacklevel=2)
rcond = -1
if rcond is None:
rcond = finfo(t).eps * ldb

result_real_t = _realType(result_t)
real_t = _linalgRealType(t)
bstar = zeros((ldb, n_rhs), t)
Expand Down
19 changes: 18 additions & 1 deletion numpy/linalg/tests/test_linalg.py
Original file line number Diff line number Diff line change
Expand Up @@ -793,7 +793,7 @@ def do(self, a, b, tags):
arr = np.asarray(a)
m, n = arr.shape
u, s, vt = linalg.svd(a, 0)
x, residuals, rank, sv = linalg.lstsq(a, b)
x, residuals, rank, sv = linalg.lstsq(a, b, rcond=-1)
if m <= n:
assert_almost_equal(b, dot(a, x))
assert_equal(rank, m)
Expand All @@ -814,6 +814,23 @@ def do(self, a, b, tags):
assert_(imply(isinstance(b, matrix), isinstance(x, matrix)))
assert_(imply(isinstance(b, matrix), isinstance(residuals, matrix)))

def test_future_rcond(self):
a = np.array([[0., 1., 0., 1., 2., 0.],
[0., 2., 0., 0., 1., 0.],
[1., 0., 1., 0., 0., 4.],
[0., 0., 0., 2., 3., 0.]]).T

b = np.array([1, 0, 0, 0, 0, 0])
with suppress_warnings() as sup:
w = sup.record(FutureWarning, "`rcond` parameter will change")
x, residuals, rank, s = linalg.lstsq(a, b)
assert_(rank == 4)
x, residuals, rank, s = linalg.lstsq(a, b, rcond=-1)
assert_(rank == 4)
x, residuals, rank, s = linalg.lstsq(a, b, rcond=None)
assert_(rank == 3)
# Warning should be raised exactly once (first command)
assert_(len(w) == 1)

class TestMatrixPower(object):
R90 = array([[0, 1], [-1, 0]])
Expand Down