gpt4 book ai didi

python - Scipy 或 bayesian 优化函数与 Python 中的约束、边界和数据框

转载 作者:行者123 更新时间:2023-12-04 04:27:35 24 4
gpt4 key购买 nike

使用下面的数据框,我想优化总返回,同时满足某些界限。

d = {'Win':[0,0,1, 0, 0, 1, 0],'Men':[0,1,0, 1, 1, 0, 0], 'Women':[1,0,1, 0, 0, 1,1],'Matches' :[0,5,4, 7, 4, 10,13],
'Odds':[1.58,3.8,1.95, 1.95, 1.62, 1.8, 2.1], 'investment':[0,0,6, 10, 5, 25,0],}

data = pd.DataFrame(d)
我想最大化以下等式:
totalreturn = np.sum(data['Odds'] * data['investment'] * (data['Win'] == 1))
函数应该最大化满足以下边界:
for i in range(len(data)):

investment = data['investment'][i]

C = alpha0 + alpha1*data['Men'] + alpha2 * data['Women'] + alpha3 * data['Matches']

if (lb < investment ) & (investment < ub) & (investment > C) == False:
data['investment'][i] = 0
特此 lbub数据帧中的每一行都是常量。阈值 C但是,每一行都不同。因此有6个参数需要优化: lb, ub, alph0, alpha1, alpha2, alpha3 .
谁能告诉我如何在python中做到这一点?到目前为止,我的程序一直在使用 scipy(方法 1)和贝叶斯(方法 2)优化,并且只有 lbub正在尝试优化。
方法一:
import pandas as pd
from scipy.optimize import minimize

def objective(val, data):

# Approach 1
# Lowerbound and upperbound
lb, ub = val

# investments
# These matches/bets are selected to put wager on
tf1 = (data['investment'] > lb) & (data['investment'] < ub)
data.loc[~tf1, 'investment'] = 0


# Total investment
totalinvestment = sum(data['investment'])

# Good placed bets
data['reward'] = data['Odds'] * data['investment'] * (data['Win'] == 1)
totalreward = sum(data['reward'])

# Return and cumalative return
data['return'] = data['reward'] - data['investment']
totalreturn = sum(data['return'])
data['Cum return'] = data['return'].cumsum()

# Return on investment
print('\n',)
print('lb, ub:', lb, ub)
print('TotalReturn: ',totalreturn)
print('TotalInvestment: ', totalinvestment)
print('TotalReward: ', totalreward)
print('# of bets', (data['investment'] != 0).sum())

return totalreturn


# Bounds and contraints
b = (0,100)
bnds = (b,b,)
x0 = [0,100]

sol = minimize(objective, x0, args = (data,), method = 'Nelder-Mead', bounds = bnds)
和方法2:
import pandas as pd
import time
import pickle
from hyperopt import fmin, tpe, Trials
from hyperopt import STATUS_OK
from hyperopt import hp

def objective(args):
# Approach2

# Lowerbound and upperbound
lb, ub = args

# investments
# These matches/bets are selected to put wager on
tf1 = (data['investment'] > lb) & (data['investment'] < ub)
data.loc[~tf1, 'investment'] = 0


# Total investment
totalinvestment = sum(data['investment'])

# Good placed bets
data['reward'] = data['Odds'] * data['investment'] * (data['Win'] == 1)
totalreward = sum(data['reward'])

# Return and cumalative return
data['return'] = data['reward'] - data['investment']
totalreturn = sum(data['return'])
data['Cum return'] = data['return'].cumsum()

# store results
d = {'loss': - totalreturn, 'status': STATUS_OK, 'eval time': time.time(),
'other stuff': {'type': None, 'value': [0, 1, 2]},
'attachments': {'time_module': pickle.dumps(time.time)}}

return d



trials = Trials()

parameter_space = [hp.uniform('lb', 0, 100), hp.uniform('ub', 0, 100)]

best = fmin(objective,
space= parameter_space,
algo=tpe.suggest,
max_evals=500,
trials = trials)


print('\n', trials.best_trial)
有谁知道我应该如何进行? Scipy 不会生成所需的结果。 Hyperopt 优化确实会产生所需的结果。在任何一种方法中,我都不知道如何合并依赖于行的边界( C(i) )。
什么都会有帮助!
(任何有关优化类型的相关文章、练习或有用的解释也非常受欢迎)

最佳答案

我在这里假设您无法遍历整个数据集,或者它不完整,或者您想要外推,因此您无法计算所有组合。
如果您没有先验,并且不确定平滑度,或者评估可能很昂贵,我会使用贝叶斯优化。您可以控制探索/开发并防止陷入最低限度。
我会用 scikit-optimize它更好地实现了贝叶斯优化 IMO。他们有更好的初始化技术,如 Sobol'此处正确实现的方法。这确保您的搜索空间将被正确采样。

from skopt import gp_minimize

res = gp_minimize(objective, bnds, initial_point_generator='sobol')

关于python - Scipy 或 bayesian 优化函数与 Python 中的约束、边界和数据框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66985605/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com