Issue
I am using LinearRegression
from sklearn.linear_model
. Can I force the coefficients between 0 and 1? Also, can I give priority to solutions involving only binary coefficients? (Assume such a solution exists!)
From https://scikit-learn.org/stable/modules/generated/sklearn.linear_model.LinearRegression.html, I only know how to force positive coefficients using the positive=True
parameter, but coefficients reach values above 1:
from sklearn.linear_model import LinearRegression
reg = LinearRegression(positive=True, fit_intercept=False).fit(X, y)
Alternatively, can you suggest a different model for this?
EDIT:
As I understand, the command reg.coef_
shows the coefficients that were found to fit the data best. Can I force the algorithm to only look for solutions with coefficients in the range of 0-1 (or if possible binary)? E.g., scipy.optimize.curve_fit
allows to set bounds
(possible ranges) for each variable.
Solution
Sklearn LinearRegression()
is a wrapper for scipy.linalg.lstsq()
. It does not implement a constrained version as far as I know, but you can try scipy.optimize.lsq_linear()
:
from scipy.optimize import lsq_linear
res = lsq_linear(X, y, bounds=(0, 1))
# Get coefficients:
print(res.x)
Answered By - dx2-66
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.