我是python的新手,我有以下问题:我试图最小化一个python函数,它有一个numpy数组作为其参数之一.当我使用scipy.optimize.fmin时,它会将我的数组转换为一个列表(导致函数无法评估).是否有一个优化函数可以接受numpy数组作为函数参数?
提前致谢!
-MB
编辑:这是我正在谈论的一个例子,由@EOL提供:
import scipy.optimize as optimize
import numpy as np
def rosen(x):
print x
x=x[0]
"""The Rosenbrock function"""
return sum(100.0*(x[1:]-x[:-1]**2.0)**2.0 + (1-x[:-1])**2.0)
x0 = np.array([[1.3,0.7,0.8,1.9,1.2]])
xopt = optimize.fmin(rosen,x0,xtol=1e-8,disp=True)
#[ 1.3 0.7 0.8 1.9 1.2]
#(note that this used to be a numpy array of length 0,#now it's "lost" a set of brackets")
最佳答案
以下是使用来自scipy tutorial的optimize.fmin的示例:
原文链接:/python/439205.htmlimport scipy.optimize as optimize
def rosen(x):
"""The Rosenbrock function"""
return sum(100.0*(x[1:]-x[:-1]**2.0)**2.0 + (1-x[:-1])**2.0)
x0 = [1.3,1.2]
xopt = optimize.fmin(rosen,disp=True)
# Optimization terminated successfully.
# Current function value: 0.000000
# Iterations: 339
# Function evaluations: 571
print(xopt)
# [ 1. 1. 1. 1. 1.]