二维插值和最大与interp2d和fmin/从scipy

时间:2022-01-14 18:14:52

I have some data from the lab to analyze. Until now it was a problem which was dependend on one variable. Therefore I found a solution (http://kitchingroup.cheme.cmu.edu/blog/category/interpolation/):

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

# 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)

[...]

It figured out that my problem is dependend on at least two parameters. So I tried to apply "interp2d" for a multidimensional problem. But in fact I did not understand how do that exactly.

它指出我的问题取决于至少两个参数。所以我尝试用"interp2d"来解决一个多维问题。但实际上我不明白这是怎么回事。

I have a matrix of data for testing which is like:

我有一个测试数据矩阵,它是这样的:

     | 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
...

I read this post "Scipy interpolation on a numpy array" which was quite interesting for me. It's now possible for me to interpolate data like in the example. But I don't know how to apply a function like "minimize" from scipy. There is an example (http://docs.scipy.org/doc/scipy-0.16.0/reference/generated/scipy.optimize.minimize.html#scipy.optimize.minimize) but the use a analytical function instead of an interpolated dataset like in the 1D example above.

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

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

When I tried to use interp2d to do it the same way I got into trouble because of the shape of the array. Maybe it is the key to solve the problem to use it?

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

Maybe someone can explain to me how to use such a 2D interpolation and how to find the maximum.

也许有人可以向我解释如何使用这样的二维插值和如何找到最大值。

Regards, Alex

认为,亚历克斯

1 个解决方案

#1


0  

Based on my experience with the 1D problem I changed to code to the following:

根据我对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)

This works and I can get datapoints from the interpolated matrix. But I cannot apply the minimize function in the next step:

这是可行的,我可以从插值矩阵中得到数据池。但是我不能在下一个步骤中应用最小化函数:

# 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)

This throws the following exception:

这会抛出以下异常:

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'

So what is the missing positional argument? Does this mean that I cannot get minimum of two paramters without defining a specific x?

那么,缺失的位置参数是什么?这是否意味着如果不定义一个特定的x,我就不能得到至少两个参数?

#1


0  

Based on my experience with the 1D problem I changed to code to the following:

根据我对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)

This works and I can get datapoints from the interpolated matrix. But I cannot apply the minimize function in the next step:

这是可行的,我可以从插值矩阵中得到数据池。但是我不能在下一个步骤中应用最小化函数:

# 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)

This throws the following exception:

这会抛出以下异常:

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'

So what is the missing positional argument? Does this mean that I cannot get minimum of two paramters without defining a specific x?

那么,缺失的位置参数是什么?这是否意味着如果不定义一个特定的x,我就不能得到至少两个参数?