Skip to content

Commit

Permalink
force linear fit to have positive coefficients
Browse files Browse the repository at this point in the history
  • Loading branch information
timmh committed Jul 15, 2024
1 parent bd49e10 commit b799e76
Showing 1 changed file with 4 additions and 1 deletion.
5 changes: 4 additions & 1 deletion utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,10 @@ def calibrate(x, y, method, n=2, poly_deg=5):
x, y = x.reshape(-1), y.reshape(-1)

if method == RegressionMethod.RANSAC:
ransac = linear_model.RANSACRegressor(random_state=random_seed)
def is_model_valid(model, X_, y_):
return (model.coef_ > 0).all()
estimator = linear_model.LinearRegression(positive=True)
ransac = linear_model.RANSACRegressor(estimator=estimator, is_model_valid=is_model_valid, random_state=random_seed)
ransac.fit(np.array(x).reshape(-1, 1), np.array(y).reshape(-1, 1))
c = ransac.predict(np.array([0]).reshape(-1, 1)) if n == 2 else 0
m = ransac.predict(np.array([1]).reshape(-1, 1)) - c
Expand Down

0 comments on commit b799e76

Please sign in to comment.