2D插值和最大值与interp2d和fmin /最小化scipy

时间:2016-04-05 13:50:41

标签: python optimization scipy

我从实验室获得了一些数据进行分析。到目前为止,这是一个依赖于一个变量的问题。因此我找到了一个解决方案(http://kitchingroup.cheme.cmu.edu/blog/category/interpolation/):

# use splines to fit and interpolate data
from scipy.interpolate import interp1d
from scipy.optimize import fmin
import numpy as np

x = np.array([ 0,      1,      2,      3,      4    ])
y = np.array([ 0.,     0.308,  0.55,   0.546,  0.44 ])

# create the interpolating function
f = interp1d(x, y, kind='cubic', bounds_error=False)

# to find the maximum, we minimize the negative of the function. We
# cannot just multiply f by -1, so we create a new function here.
f2 = interp1d(x, -y, kind='cubic')
xmax = fmin(f2, 2.5)

[...]

它发现我的问题至少取决于两个参数。所以我试图将“interp2d”应用于多维问题。但实际上我并不明白这是怎么做的。

我有一个用于测试的数据矩阵,如:

     | 2.00 | 2.50 | 3.00 | 3.50 | ...
--------------------------------------
5.00 | 0.0  | 60.0 | 10.0 | 0.00
10.0 | 0.0  | 100.0| 70.0 | 30.0
25.0 | 10.0 | 40.0 | 50.0 | 10.0
50.0 | 15.0 | 30.0 | 10.0 | 0.0
...

我读过这篇文章“Scipy interpolation on a numpy array”,这对我来说非常有趣。现在我可以像示例中那样插入数据。但我不知道如何应用像scipy这样的“最小化”功能。有一个例子(http://docs.scipy.org/doc/scipy-0.16.0/reference/generated/scipy.optimize.minimize.html#scipy.optimize.minimize),但是使用分析函数而不是上面的一维例子中的插值数据集。

res = minimize(func, x0, method='Nelder-Mead')

当我尝试使用interp2d时,由于数组的形状,我遇到了麻烦。也许这是解决问题的关键?

也许有人可以向我解释如何使用这种2D插值以及如何找到最大值。

此致 亚历

1 个答案:

答案 0 :(得分:0)

根据我对1D问题的经验,我将代码改为以下代码:

[...]

import scipy as sp
import numpy as np

[...]

x, y, z = self.Data

# make matrix for both parameters
xx, yy = np.meshgrid(x, y)        

# change shape of values in z
s = xx.shape      
z = z.reshape(s)

# create the interpolating function and inverse function        
kind = 'cubic'         
self.F2 = interp2d(xx, -yy, z, kind, bounds_error=False)

这可行,我可以从插值矩阵中获取数据点。但我不能在下一步中应用最小化功能:

# i used ranges for the x value from 2000 to 3500 and for y from 10 to 50. Maybe this is wrong?
rranges = (slice(2000, 3500, 1), slice(10, 50, 1))

resbrute = sp.optimize.brute(self.F2, rranges, finish=sp.optimize.fmin)

这会引发以下异常:

File "optimizer.py", line 199, in getIntpMax
resbrute = sp.optimize.brute(self.F2, rranges, finish=sp.optimize.fmin)

File "c:\program files\Anaconda3\lib\site-packages\scipy\optimize\optimize.py", line 2551, in brute
Jout = vecfunc(*grid)
File "c:\program files\Anaconda3\lib\site-packages\numpy\lib\function_base.py", line 1700, in __call__
return self._vectorize_call(func=func, args=vargs)
File "c:\program files\Anaconda3\lib\site-packages\numpy\lib\function_base.py", line 1763, in _vectorize_call
ufunc, otypes = self._get_ufunc_and_otypes(func=func, args=args)
File "c:\program files\Anaconda3\lib\site-packages\numpy\lib\function_base.py", line 1725, in _get_ufunc_and_otypes
outputs = func(*inputs)
File "c:\program files\Anaconda3\lib\site-packages\scipy\optimize\optimize.py", line 2545, in _scalarfunc
return func(params, *args)

TypeError: __call__() missing 1 required positional argument: 'y'

那么缺少位置论证的是什么?这是否意味着在没有定义特定x的情况下我无法获得最少两个参数?