Reputation: 196
I'm fitting a set of data with numpy.lstsq()
:
numpy.linalg.lstsq(a,b)[0]
returns something like:
array([ -0.02179386, 0.08898451, -0.17298247, 0.89314904])
Note the fitting solution is a mix of positive and negative float.
Unfortunately, in my physical model, the fitting solutions represent a mass: consequently I'd like to force lstsq()
to return a set of positive values as a solution of the fitting. Is it possible to do this?
i.e.
solution = {a_1, ... a_i, ... a_N} with a_i > 0 for i = {1, ..., N}
Upvotes: 9
Views: 4259
Reputation: 13913
Non-negative least squares is implemented in scipy.optimize.nnls
.
from scipy.optimize import nnls
solution = nnls(a, b)[0]
Upvotes: 18