How can I turn
def square_error(f1,f2, u1,v1,sigu1,sigv1,rho1, u2,v2,sigu2,sigv2,rho2, u3,v3,sigu3,sigv3,rho3):
into:
def square_error(x):
# x = [0.2,0.5,
# 1,1,1,1,0.3,
# 1,1,1,1,0.3,
# 1,1,1,1,0.3],
f1,f2, u1,v1,sigu1,sigv1,rho1, u2,v2,sigu2,sigv2,rho2, u3,v3,sigu3,sigv3,rho3 = x
The scipy.minimize
function only allow 1 argument for the target function, so I need to turn the variables into a single argument.
Cœur
38.9k25 gold badges206 silver badges281 bronze badges
asked Sep 14, 2015 at 2:38
1 Answer 1
Yes you can do that, but you will have to the *
syntax which will unpack the array into the parameters of the function.
For example:
def square_error( x,y,z ):
print x,y,z
arr = [ 1, 2, 3 ]
square_error( *arr )
will unpack the values 1,2,3
into the parameters x,y,z
Or if you wanted to unpck the values into variables within the function, use sequence unpacking:
def square_error( arr ):
x,y,z = arr
print x,y,z
arr = [ 1, 2, 3 ]
square_error( arr )
answered Sep 14, 2015 at 2:44
-
I'm sorry, I may not be cleaer enough. I need to turn
def square_error(x,y,z):
intodef square_error(x):
, and within the function, unpack thex
intox,y,z
. How can I do that? Thescipy.minimize
function seems only allow for1 argument
.ZK Zhao– ZK Zhao2015年09月14日 02:47:34 +00:00Commented Sep 14, 2015 at 2:47 -
Ah in that case, you don't need to do anything special, I've edited my answer.Serdalis– Serdalis2015年09月14日 02:50:22 +00:00Commented Sep 14, 2015 at 2:50
lang-py
np.array(x)
? I'm not clear what you're asking.