简体   繁体   English

scipy fmin操作数不能与形状一起广播

[英]scipy fmin operands could not be broadcast together with shapes

i'm trying to learn about optimization in Python so i've written some code to test out the fmin function. 我试图学习有关Python优化的知识,所以我写了一些代码来测试fmin函数。

However i keep receiving the following error: 但是我一直收到以下错误:

ValueError: operands could not be broadcast together with shapes (1,2) (100,)

I can tell the issue is to do with the dimensions of my arguments but I'm not sure how to rectify it. 我可以说问题出在我的论点范围之内,但我不确定如何纠正它。 Rather than a lambda function I also tried to def a function but I still get the same error. 我也没有尝试使用lambda函数来定义一个函数,但是我仍然遇到相同的错误。

I'm sure it's something pretty basic but I can't seem to understand it. 我敢肯定这是一个非常基本的东西,但我似乎无法理解。 Any help wold be greatly appreciated! 任何帮助将不胜感激!

import numpy as np
import pandas as pd
from scipy.stats.distributions import norm
from scipy.optimize import fmin

x = np.random.normal(size=100)

norm_1 = lambda theta,x: -(np.log(norm.pdf(x,theta[0],theta[1]))).sum()

def norm_2(theta,x):
    mu = theta[0]
    sigma = theta[1]
    ll = np.log(norm.pdf(x,mu,sigma)).sum()
    return -ll

fmin(norm_1,np.array([0,1]),x)

fmin(norm_2,np.array([0,1]),x)

The docs for fmin say: fmin的文档说:

Definition: fmin(func, x0, args=(), xtol=0.0001, ftol=0.0001, maxiter=None, maxfun=None, full_output=0, disp=1, retall=0, callback=None)
...
    args : tuple, optional
        Extra arguments passed to func, i.e. ``f(x,*args)``.

Therefore, the third argument, args , should be a tuple: 因此,第三个参数args应该是一个元组:

In [45]: fmin(norm_1,np.array([0,1]),(x,))
Warning: Maximum number of function evaluations has been exceeded.
Out[45]: array([-0.02405078,  1.0203125 ])

(x, ) is a tuple containing one element, x . (x, )是一个包含一个元素x的元组。 The docs say f(x, *args) gets called. 文档说f(x, *args)被调用。 Which mean in your case 在你的情况下

norm_1(np.array([0,1]), *(x,))

will get called, which is equivalent to 将被调用, 相当于

norm_1(np.array([0,1]), x)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Scipy pythonoptimization error:ValueError: 操作数无法与形状一起广播 (9,) (6,) (6,) - Scipy pythonoptimization error:ValueError: operands could not be broadcast together with shapes (9,) (6,) (6,) Numpy错误:操作数无法与形状1一起广播 - Numpy error: operands could not be broadcast together with shapes 1 操作数不能与形状(128,)(0,)错误一起广播 - Operands could not be broadcast together with shapes (128,) (0,) error Python - ValueError:操作数无法与形状一起广播 - Python - ValueError: operands could not be broadcast together with shapes 操作数不能与形状 (100,) (8,8) 一起广播 - Operands could not be broadcast together with shapes (100,) (8,8) ValueError: 操作数无法与形状 (3,) (3000,) 一起广播 - ValueError: operands could not be broadcast together with shapes (3,) (3000,) Pandas:ValueError - 操作数无法与形状一起广播 - Pandas: ValueError - operands could not be broadcast together with shapes Python ValueError:操作数无法与形状一起广播 - Python ValueError: operands could not be broadcast together with shapes ValueError: 操作数无法与形状 (7,) (6,) (7,) 一起广播 - ValueError: operands could not be broadcast together with shapes (7,) (6,) (7,) ValueError: 操作数无法与形状一起广播 (7410,) (3,) - ValueError: operands could not be broadcast together with shapes (7410,) (3,)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM