Python L-BFGS-B不满足给定约束

Python L-BFGS-B不满足给定约束,python,optimization,scipy,minimize,Python,Optimization,Scipy,Minimize,我试图通过使用scipy中的最小化函数来找到模型的优化权重值。如下面代码所示,我定义了返回1减去模型f1分数的错误函数 def err_func(weights,x,y): undetected=0 correct=0 incorrect=0 results=fun(weights,x) for i in range(0,len(results)): if(results[i]==y[i])

我试图通过使用scipy中的最小化函数来找到模型的优化权重值。如下面代码所示,我定义了返回1减去模型f1分数的错误函数

 def err_func(weights,x,y):
        undetected=0
        correct=0
        incorrect=0
        results=fun(weights,x)
        for i in range(0,len(results)):
            if(results[i]==y[i]):
                correct+=1
            elif(not (results[i]==y[i])):
                incorrect+=1
        undetected=len(y)-(correct+incorrect)
        precision=float(correct) / float(correct + incorrect)
        recall=float(correct) / float(correct + incorrect + undetected)
        f1=2 * precision * recall / (precision + recall)
        return 1.0-f1  
我使用的约束条件是,权重中的每个值介于0和1之间,权重之和等于1。这些定义如下:

cons = ({'type': 'eq', 'fun': lambda x: 1 - sum(x)})
bnds = tuple((0.0, 1.0) for x in weights)
eps=1e-2
但在运行minimize方法时,我的函数不满足约束

from scipy.optimize import minimize
res = minimize(err_func, weights,method='L-BFGS-B', args=(x,y),constraints=cons,bounds=bnds,options = {'eps':eps,'maxiter':100}) 
print res
test_weights=res.x
print sum(test_weights) 
我得到这样一个输出,权重之和大于1。我错过了什么

>    fun: 0.4955555555555555  hess_inv: <11x11 LbfgsInvHessProduct with
> dtype=float64>
>       jac: array([ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.])   message: 'CONVERGENCE: NORM_OF_PROJECTED_GRADIENT_<=_PGTOL'
>      nfev: 24
>       nit: 1    status: 0   success: True
>         x: array([ 0.        ,  0.22222222,  0.        ,  1.        ,  1.        ,
>         0.11111111,  1.        ,  1.        ,  1.        ,  0.        ,  1.        ])
> 6.33333333333
>乐趣:0.495555赫斯_投资:dtype=float64>

>jac:array([0,0,0,0,0,0,0,0,0,0,0.])消息:“收敛:投影梯度的NORM\u
L-BFGS-B
仅支持绑定约束(这就是第二个“B”的意思)。此方法不支持常规约束

摘录自:

Parameters:
    ...
    constraints : dict or sequence of dict, optional
        ...
        Constraints definition (only for COBYLA and SLSQP)