TypeError:函数()只接受2个参数(1给定)(python)

时间:2021-08-29 04:04:09
import numpy as np
import scipy.optimize as spo

def function(x,y):
    return (np.sin(x*y+y)*np.exp(-1*(x**2+y**2)))**-1


xi=[0,0]    
answer=spo.fmin(function,xi)
print 'the answer is', answer

I am trying to minimise this function. However running it brings up

我试图将这个函数最小化。然而,运行它会带来。

TypeError: function() takes exactly 2 arguments (1 given)

2 个解决方案

#1


1  

scipy.optimize.fmin(func, x0, args=(), xtol=0.0001, ftol=0.0001, maxiter=None, maxfun=None, full_output=0, disp=1, retall=0, callback=None)

parameter func is callable func(x,*args)

参数func是可调用的func(x,*args)

In this case fmin call function with one parameter - x (which is xi). The second parameter must be passed as args parameter.

在这种情况下,fmin调用函数具有一个参数x(即xi),第二个参数必须作为args参数传递。

xi = 0
args = (0,)
answer = spo.fmin(function, x0=xi, args=args)

http://docs.scipy.org/doc/scipy-0.16.0/reference/generated/scipy.optimize.fmin.html

http://docs.scipy.org/doc/scipy-0.16.0/reference/generated/scipy.optimize.fmin.html

#2


1  

Is your intent to minimize over 2 variables ('x','y'), or over just one (with 'y' as an extra parameter)?

你的意图是最小化两个变量(x, y),或者仅仅是一个(带有“y”作为额外参数)?

def fn1(x, y):
    # x is minimization variable
    # y is extra argument
    return (np.sin(x*y+y)*np.exp(-1*(x**2+y**2)))**-1

def fn2(xy):
    # xy is minimization variable; assumed to be 2 elements
    x,y = xy                                             
    return (np.sin(x*y+y)*np.exp(-1*(x**2+y**2)))**-1

fmin with 1 variable; fails

fmin 1变量;失败

In [35]: optimize.fmin(fn1, x0=0, args=(0,))
Warning: Maximum number of function evaluations has been exceeded.
Out[35]: array([ 0.])

fmin with 2 element array (x0 and function); returns 2 element array.

fmin有2个元素阵列(x0和函数);返回2元素数组。

In [38]: optimize.fmin(fn2, x0=np.array([0,0]))
Optimization terminated successfully.
         Current function value: 2.227274
         Iterations: 64
         Function evaluations: 121
Out[38]: array([ 0.29782369,  0.62167083])

#1


1  

scipy.optimize.fmin(func, x0, args=(), xtol=0.0001, ftol=0.0001, maxiter=None, maxfun=None, full_output=0, disp=1, retall=0, callback=None)

parameter func is callable func(x,*args)

参数func是可调用的func(x,*args)

In this case fmin call function with one parameter - x (which is xi). The second parameter must be passed as args parameter.

在这种情况下,fmin调用函数具有一个参数x(即xi),第二个参数必须作为args参数传递。

xi = 0
args = (0,)
answer = spo.fmin(function, x0=xi, args=args)

http://docs.scipy.org/doc/scipy-0.16.0/reference/generated/scipy.optimize.fmin.html

http://docs.scipy.org/doc/scipy-0.16.0/reference/generated/scipy.optimize.fmin.html

#2


1  

Is your intent to minimize over 2 variables ('x','y'), or over just one (with 'y' as an extra parameter)?

你的意图是最小化两个变量(x, y),或者仅仅是一个(带有“y”作为额外参数)?

def fn1(x, y):
    # x is minimization variable
    # y is extra argument
    return (np.sin(x*y+y)*np.exp(-1*(x**2+y**2)))**-1

def fn2(xy):
    # xy is minimization variable; assumed to be 2 elements
    x,y = xy                                             
    return (np.sin(x*y+y)*np.exp(-1*(x**2+y**2)))**-1

fmin with 1 variable; fails

fmin 1变量;失败

In [35]: optimize.fmin(fn1, x0=0, args=(0,))
Warning: Maximum number of function evaluations has been exceeded.
Out[35]: array([ 0.])

fmin with 2 element array (x0 and function); returns 2 element array.

fmin有2个元素阵列(x0和函数);返回2元素数组。

In [38]: optimize.fmin(fn2, x0=np.array([0,0]))
Optimization terminated successfully.
         Current function value: 2.227274
         Iterations: 64
         Function evaluations: 121
Out[38]: array([ 0.29782369,  0.62167083])