Find optimal weights for a regression model with some restrictions I have created a hybrid recommendation system which contains 4 recommendation models.
In my case i am trying to predict the ratings of the products and after that recommend the high rated (predictions) products to the user.
Here I have the 4 predictions and the true values. I am trying with those data to train a regression model with 4 weights that the sum of the weights will be equal to 1.

So the restriction is that the sum of weights should be equal to 1. I tried many libraries such as sklearn linear regression or some other polyonimal regression but the weights didn't make much sense. Any suggestions how to accomplish that?
 A: You can use optuna for this: https://optuna.org
Its a bayesian optimization framework generally used to optimize model parameters. But actually you can use Bayesian Opt. to optimize any function. Below code should fix your problem. Note that I am assuming you are trying to minimize mean squared error here.
import optuna
import numpy as np
def objective(trial):
    w_1 = trial.suggest_uniform('w_1', -1, 1)
    w_2 = trial.suggest_uniform('w_2', -1, 1)
    w_3 = trial.suggest_uniform('w_3', -1, 1)
    w_4 = trial.suggest_uniform('w_4', -1, 1)
    
    convex_comb = w_1*pred_0+w_2*pred_1+w_3*pred_2+w_4*pred_3
    if convex_comb==1:
        return np.mean((y_true - convex_comb)**2)
    return float('inf')

study = optuna.create_study()
study.optimize(objective, n_trials=2000, n_jobs=-1)

Alternatively:
import optuna
import numpy as np
def objective(trial):
    w_1 = trial.suggest_uniform('w_1', -1, 1)
    w_2 = trial.suggest_uniform('w_2', -1, 1)
    w_3 = trial.suggest_uniform('w_3', -1, 1)
    w_4 = 1-(w_1+w_2+w_3)
    
    convex_comb = w_1*pred_0+w_2*pred_1+w_3*pred_2+w_4*pred_3
    if convex_comb==1:
        return np.mean((y_true - convex_comb)**2)
    return float('inf')

study = optuna.create_study()
study.optimize(objective, n_trials=2000, n_jobs=-1)

Later one should be faster.
