0

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/):

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

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.

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?

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

Regards, Alex

Community
  • 1
  • 1
schlenzmeister
  • 149
  • 1
  • 3
  • 12

1 Answers1

0

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

[...]

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?

schlenzmeister
  • 149
  • 1
  • 3
  • 12