There is an answer to this very question here however the solution doesn't seem to work for me.
my function is the following:
def slopefunction_cf(xin,amp,phase,off,freq,intercept,t):
fitted =np.zeros((4,12))
for im in t:
fitted[:,im-1] = intercept[im-1]+[amp*np.sin(freq*(im)+phase)+off]*xin
return fitted.ravel()
where intercept and t are additional arguments, that in leastsq I would pass as args=
I tried:
popt, pcov = curve_fit(slopefunction_cf, np.arange(0,4), datain, p0=paramin, args=(intercept,t) )
and get the error:
TypeError: func_wrapped() takes exactly 1 argument (3 given)
Then I tried what suggested in the answer I linked and transformed it to:
def define_var_args(interceptin,tin):
def slopefunction_cf(xin,amp,phase,off,freq):
intercept = interceptin
t = tin
fitted =np.zeros((4,12))
for im in t:
fitted[:,im-1] = intercept[im-1]+[amp*np.sin(freq*(im)+phase)+off]*xin
return fitted.ravel()
and then called curve_fit:
popt, pcov = curve_fit(define_var_args(intercept, t), np.arange(0,4), datain, p0=paramin )
but I get the following error (i copied everything)
--> 115 popt, pcov = curve_fit(define_var_args(interceptconus, t), np.arange(0,4), datain.ravel(), p0=paramin )
116
117 print popt
/software/centos6/x86_64/canopy-1.7.4/Canopy_64bit/User/lib/python2.7/site-packages/scipy/optimize/minpack.pyc in curve_fit(f, xdata, ydata, p0, sigma, absolute_sigma, check_finite, bounds, method, jac, **kwargs)
734 # Remove full_output from kwargs, otherwise we're passing it in twice.
735 return_full = kwargs.pop('full_output', False)
--> 736 res = leastsq(func, p0, Dfun=jac, full_output=1, **kwargs)
737 popt, pcov, infodict, errmsg, ier = res
738 cost = np.sum(infodict['fvec'] ** 2)
/software/centos6/x86_64/canopy-1.7.4/Canopy_64bit/User/lib/python2.7/site-packages/scipy/optimize/minpack.pyc in leastsq(func, x0, args, Dfun, full_output, col_deriv, ftol, xtol, gtol, maxfev, epsfcn, factor, diag)
375 if not isinstance(args, tuple):
376 args = (args,)
--> 377 shape, dtype = _check_func('leastsq', 'func', func, x0, args, n)
378 m = shape[0]
379 if n > m:
/software/centos6/x86_64/canopy-1.7.4/Canopy_64bit/User/lib/python2.7/site-packages/scipy/optimize/minpack.pyc in _check_func(checker, argname, thefunc, x0, args, numinputs, output_shape)
24 def _check_func(checker, argname, thefunc, x0, args, numinputs,
25 output_shape=None):
---> 26 res = atleast_1d(thefunc(*((x0[:numinputs],) + args)))
27 if (output_shape is not None) and (shape(res) != output_shape):
28 if (output_shape[0] != 1):
/software/centos6/x86_64/canopy-1.7.4/Canopy_64bit/User/lib/python2.7/site-packages/scipy/optimize/minpack.pyc in func_wrapped(params)
452 if transform is None:
453 def func_wrapped(params):
--> 454 return func(xdata, *params) - ydata
455 elif transform.ndim == 1:
456 def func_wrapped(params):
TypeError: 'NoneType' object is not callable
I am not even sure what the error is - if the code or something in my data? it's a bit obscure.
Is there another way to pass additional parameters?
EDIT: Adding some data and slight change to the above function
t = np.arange(1,13)
paramin = [0.40569370873086791, 1, -0.88134718450091365, 0.5]
datain = [-3.45083921, -3.405628 , -2.57401114, -1.7632551 , -1.29342567,
-1.44200248, -2.05366981, -2.51467815, -2.79597983, -2.95528074,
-2.90766603, -3.60850134, -3.90036107, -3.87760971, -3.18549774,
-2.43304533, -2.1734368 , -2.43953997, -3.1183866 , -3.57696154,
-3.72915335, -3.67344888, -3.29697388, -4.10385938, -4.88945667,
-4.82385939, -4.10568684, -3.50093464, -3.26766599, -3.78206157,
-4.51782818, -4.95149472, -4.93689182, -4.75850421, -4.22849458,
-4.91811193, -5.34080606, -5.40676402, -4.66050702, -4.29447163,
-4.16430688, -4.75008652, -5.57106708, -5.58801663, -5.96637981,
-5.51549722, -4.94026303, -5.42975354]
intercept = [-3.39651633, -3.33601661, -2.55447417, -1.69869584, -1.26867791,
-1.41340658, -2.02249291, -2.56860547, -2.74926044, -2.91082705,
-2.78895263, -3.57335517]
I already "raveled" datain, so I modified the function at the beginning of my question.
using these data I reproduce my error in a new notebook.
SOLUTION:
I had two issues in my code:
1) I was not returning the nested function
2) I did not put xin as an argument of the nested function
3) I had to put the star before the p
this now works
def define_extravar(interceptin,tin):
def slopefunction_cf(xin,*p):
intercept = interceptin
t = tin
fitted =np.zeros((4,12))
amp,phase,off,freq = p
for im in t:
fitted[:,im-1] = intercept[im-1]+[amp*np.sin(freq*(im)+phase)+off]*xin
return fitted.ravel()
return slopefunction_cf
Related
Dear Stackoverflow community,
I want fit over the following function with curve_fit:
def fct(A, Ravg,sigma,pos,omega, x):
K=integrate.quad(lambda R:np.exp( - (R-Ravg)**2 / (2 * sigma**2) ),Ravg-sigma,Ravg+sigma)
data=[]
for i in range(0,len(x)):
data.append(integrate.quad(lambda R: (3*(np.sin(x[i]*R)-x[i]*R*np.cos(x[i]*R))/(x[i]*R)**3)**2*1/K[0] *np.exp( - (R-Ravg)**2 / (2 * sigma**2) ),Ravg-sigma,Ravg+sigma))
gafs=array(data)
return A*gafs[:,0]
I get from curve_fit with this code:
fitPars, covMatrix = curve_fit(fct, xaxis, yaxis)
The following error message:
TypeError: object of type 'numpy.float64' has no len()
I understand that the error comes from the for loop. Do you have an idea how can I make my function suitable for curvefit?
The full error track is:
TypeError Traceback (most recent call last)
<ipython-input-5-3e25e14bdca2> in <module>()
1
----> 2 fitPars, covMatrix = curve_fit(fct, xaxis, yaxis)
C:\Programfiles\Anaconda3\lib\site-packages\scipy\optimize\minpack.py in curve_fit(f, xdata, ydata, p0, sigma, absolute_sigma, check_finite, bounds, method, jac, **kwargs)
734 # Remove full_output from kwargs, otherwise we're passing it in twice.
735 return_full = kwargs.pop('full_output', False)
--> 736 res = leastsq(func, p0, Dfun=jac, full_output=1, **kwargs)
737 popt, pcov, infodict, errmsg, ier = res
738 cost = np.sum(infodict['fvec'] ** 2)
C:\Programfiles\Anaconda3\lib\site-packages\scipy\optimize\minpack.py in leastsq(func, x0, args, Dfun, full_output, col_deriv, ftol, xtol, gtol, maxfev, epsfcn, factor, diag)
375 if not isinstance(args, tuple):
376 args = (args,)
--> 377 shape, dtype = _check_func('leastsq', 'func', func, x0, args, n)
378 m = shape[0]
379 if n > m:
C:\Programfiles\Anaconda3\lib\site-packages\scipy\optimize\minpack.py in _check_func(checker, argname, thefunc, x0, args, numinputs, output_shape)
24 def _check_func(checker, argname, thefunc, x0, args, numinputs,
25 output_shape=None):
---> 26 res = atleast_1d(thefunc(*((x0[:numinputs],) + args)))
27 if (output_shape is not None) and (shape(res) != output_shape):
28 if (output_shape[0] != 1):
C:\Programfiles\Anaconda3\lib\site-packages\scipy\optimize\minpack.py in func_wrapped(params)
452 if transform is None:
453 def func_wrapped(params):
--> 454 return func(xdata, *params) - ydata
455 elif transform.ndim == 1:
456 def func_wrapped(params):
<ipython-input-3-4ae12bf8a77b> in fct(A, Ravg, sigma, pos, omega, x)
52 K=integrate.quad(lambda R:np.exp( - (R-Ravg)**2 / (2 * sigma**2) ),Ravg-sigma,Ravg+sigma)
53 data=[]
---> 54 for i in range(0,len(x)):
55 data.append(integrate.quad(lambda R: (3*(np.sin(x[i]*R)-x[i]*R*np.cos(x[i]*R))/(x[i]*R)**3)**2*1/K[0] *np.exp( - (R-Ravg)**2 / (2 * sigma**2) ),Ravg-sigma,Ravg+sigma))
56 gafs=array(data)
TypeError: object of type 'numpy.float64' has no len()
I have data that comes from numerical simulations (around 7000 points, more in other simulations), this data is plotted in the image bellow. As you can see it has a small interval (between 0.35 and 0.45) where this data is linear (it is theoretically linear on that region, but also i measured it and indeed it is in that region).
I want to calculate the best interval where the slope/fit is maximal/best automatically.
For the moment i do it by hand, i chose by eye an interval, then calculate the fit. Then repeat until i find a slope/fit that looks good (This is what i did for this image).
I know this can be done by brute force by calculating all possible intervals in my data and comparing the slopes. However i want to doit a little more intelligently.
I tried to do it whit scipy minimize, since this is a function i have used multiple times and know how to use it. For this i take my data and pass it to miminize, with hoppes that it will search for 2 numbers the min and max of the interval.
I tried by searching for the values on x (ie 0.35 and 0.45), and also the index of the data (ie 3000 and 3700). As follows,
Mini = minimize(wrapper(MinFit, x_dat, y_dat), (0,nPoints), method='Powell')
def wrapper(func, x, y):
def newfunc(t):
return func(*t, x, y)
return(newfunc)
def MinFit(iMin, iMax, x_dat, y_dat):
iMin, iMax = np.int(np.ceil(np.abs(iMin))), np.int(np.ceil(np.abs(iMax)))
if(iMin == iMax):
print('i = ', iMin, iMax)
FitParam = [0., 0.]
else:
Min, Max = np.min([x_dat[iMin], x_dat[iMax]]), np.max([x_dat[iMin], x_dat[iMax]])
print('i = ', iMin, iMax)
print(x_dat[iMin], x_dat[iMax])
x_dat_short, y_dat_short = CutData(x_dat, y_dat, Max, Min)
FitParam, FitCovar = curve_fit(Linear, x_dat_short, y_dat_short, p0=(0,0), maxfev= 5000)
return(-FitParam[0])
Where MinFit is a function that takes 4 inputs (indexMin, indexMax, x_data, y_data), wrapper just an intermediary function to properly use minimize (reduce inputs to the once i want to search) and nPoints is the index of the last data point.
However the code properly calculates the first iteration and then it breaks (for both index and float search) and outputs the following error:
Mini = minimize(wrapper(MinFit, x_dat, y_dat), (0,nPoints), method='Powell')
i = 0 58333
0.0 0.699996
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-117-295df9b333b7> in <module>
----> 1 Mini = minimize(wrapper(MinFit, x_dat, y_dat), (0,nPoints), method='Powell')
/.../_minimize.py in minimize(fun, x0, args, method, jac, hess, hessp, bounds, constraints, tol, callback, options)
588 return _minimize_neldermead(fun, x0, args, callback, **options)
589 elif meth == 'powell':
--> 590 return _minimize_powell(fun, x0, args, callback, **options)
591 elif meth == 'cg':
592 return _minimize_cg(fun, x0, args, jac, callback, **options)
/.../optimize.py in _minimize_powell(func, x0, args, callback, xtol, ftol, maxiter, maxfev, disp, direc, return_all, **unknown_options)
2610 fx2 = fval
2611 fval, x, direc1 = _linesearch_powell(func, x, direc1,
-> 2612 tol=xtol * 100)
2613 if (fx2 - fval) > delta:
2614 delta = fx2 - fval
/.../optimize.py in _linesearch_powell(func, p, xi, tol)
2396 def myfunc(alpha):
2397 return func(p + alpha*xi)
-> 2398 alpha_min, fret, iter, num = brent(myfunc, full_output=1, tol=tol)
2399 xi = alpha_min*xi
2400 return squeeze(fret), p + xi, xi
/.../optimize.py in brent(func, args, brack, tol, full_output, maxiter)
2107 options = {'xtol': tol,
2108 'maxiter': maxiter}
-> 2109 res = _minimize_scalar_brent(func, brack, args, **options)
2110 if full_output:
2111 return res['x'], res['fun'], res['nit'], res['nfev']
/.../optimize.py in _minimize_scalar_brent(func, brack, args, xtol, maxiter, **unknown_options)
2139 full_output=True, maxiter=maxiter)
2140 brent.set_bracket(brack)
-> 2141 brent.optimize()
2142 x, fval, nit, nfev = brent.get_result(full_output=True)
2143 return OptimizeResult(fun=fval, x=x, nit=nit, nfev=nfev,
/.../optimize.py in optimize(self)
1923 # set up for optimization
1924 func = self.func
-> 1925 xa, xb, xc, fa, fb, fc, funcalls = self.get_bracket_info()
1926 _mintol = self._mintol
1927 _cg = self._cg
/.../optimize.py in get_bracket_info(self)
1897 ### carefully DOCUMENT any CHANGES in core ##
1898 if brack is None:
-> 1899 xa, xb, xc, fa, fb, fc, funcalls = bracket(func, args=args)
1900 elif len(brack) == 2:
1901 xa, xb, xc, fa, fb, fc, funcalls = bracket(func, xa=brack[0],
/.../optimize.py in bracket(func, xa, xb, args, grow_limit, maxiter)
2322 _gold = 1.618034 # golden ratio: (1.0+sqrt(5.0))/2.0
2323 _verysmall_num = 1e-21
-> 2324 fa = func(*(xa,) + args)
2325 fb = func(*(xb,) + args)
2326 if (fa < fb): # Switch so fa > fb
/.../optimize.py in myfunc(alpha)
2395 """
2396 def myfunc(alpha):
-> 2397 return func(p + alpha*xi)
2398 alpha_min, fret, iter, num = brent(myfunc, full_output=1, tol=tol)
2399 xi = alpha_min*xi
TypeError: can only concatenate tuple (not "float") to tuple
(I deleted the paths for privacy reasons and put ...)
Is there a way to do what i want? Can minimize do it? If so how?
Second question, is there a way to constrain scipy minimize? (ie. only search for values between a=1 and b=nPoints, or any other value and type)
Thanks.
i have a function like this
cos(x) - sin(y) + 0.2 = 0
i want to draw this function in plt.plot and find root (x,y) when function = 0
and this is my code
from scipy import optimize
import numpy as np
def func_2(x):
return np.cos(x[0])-np.sin(x[1])+0.2
result = optimize.root(func_2, [1])
and i got error
IndexError Traceback (most recent call last)
<ipython-input-366-6d13d246d69c> in <module>
14
15 result = optimize.root(func_1, [1])
---> 16 result1 = optimize.root(func_2, [1])
~\anaconda3\lib\site-packages\scipy\optimize\_root.py in root(fun, x0, args, method, jac, tol, callback, options)
185
186 if meth == 'hybr':
--> 187 sol = _root_hybr(fun, x0, args=args, jac=jac, **options)
188 elif meth == 'lm':
189 sol = _root_leastsq(fun, x0, args=args, jac=jac, **options)
~\anaconda3\lib\site-packages\scipy\optimize\minpack.py in _root_hybr(func, x0, args, jac, col_deriv, xtol, maxfev, band, eps, factor, diag, **unknown_options)
211 if not isinstance(args, tuple):
212 args = (args,)
--> 213 shape, dtype = _check_func('fsolve', 'func', func, x0, args, n, (n,))
214 if epsfcn is None:
215 epsfcn = finfo(dtype).eps
~\anaconda3\lib\site-packages\scipy\optimize\minpack.py in _check_func(checker, argname, thefunc, x0, args, numinputs, output_shape)
24 def _check_func(checker, argname, thefunc, x0, args, numinputs,
25 output_shape=None):
---> 26 res = atleast_1d(thefunc(*((x0[:numinputs],) + args)))
27 if (output_shape is not None) and (shape(res) != output_shape):
28 if (output_shape[0] != 1):
<ipython-input-366-6d13d246d69c> in func_2(x)
11
12 def func_2(x):
---> 13 return np.cos(x[0])-np.sin(x[1])+0.2
14
15 result = optimize.root(func_1, [1])
IndexError: index 1 is out of bounds for axis 0 with size 1
I have a function findMaxEval which I invoke in a following way:
eMax0,var0=findMaxEval(np.diag(eVal0),q,bWidth=.01)
where np.diag(eVal0) is an ndarray of shape (1000,), q is a number (10).
findMaxEval has the following definition:
def findMaxEval(eVal,q,bWidth):
out=minimize(lambda *x:errPDFs(*x),.5,args= (eVal,q,bWidth),bounds=((1E-5,1-1E-5),))
if out['success']:var=out['x'][0]
else:var=1
eMax=var*(1+(1./q)**.5)**2
return eMax,var
This funtion tries to minimize errPDFs which is defined as follows:
def errPDFs(var,eVal,q,bWidth,pts=1000):
pdf0=mpPDF(var,q,pts)
pdf1=fitKDE(eVal,bWidth,x=pdf0.index.values)
sse=np.sum((pdf1-pdf0)**2)
return sse
var is a number which I pass in the findMaxEval function in minimize, initial value is 0.5.
Further, mpPDF and fitKDE are defined:
def mpPDF(var,q,pts):
eMin,eMax=var*(1-(1./q)**.5)**2,var*(1+(1./q)**.5)**2
eVal=np.linspace(eMin,eMax,pts)
pdf=q/(2*np.pi*var*eVal)*((eMax-eVal)*(eVal-eMin))**.5
pdf=pd.Series(pdf,index=eVal)
return pdf
def fitKDE(obs,bWidth=.25,kernel='gaussian',x=None):
if len(obs.shape)==1:obs=obs.reshape(-1,1)
kde=KernelDensity(kernel=kernel,bandwidth=bWidth).fit(obs)
if x is None:x=np.unique(obs).reshape(-1,1)
if len(x.shape)==1:x=x.reshape(-1,1)
logProb=kde.score_samples(x) # log(density)
pdf=pd.Series(np.exp(logProb),index=x.flatten())
return pdf
When I invoke findMaxEval (first line in the description), I get the following error:
---------------------------------------------------------------------------
Exception Traceback (most recent call last)
<ipython-input-25-abd7cf64e843> in <module>
----> 1 eMax0,var0=findMaxEval(np.diag(eVal0),q,bWidth=.01)
2 nFacts0=eVal0.shape[0]-np.diag(eVal0)[::-1].searchsorted(eMax0)
<ipython-input-24-f44a1e9d84b1> in findMaxEval(eVal, q, bWidth)
1 def findMaxEval(eVal,q,bWidth):
2 # Find max random eVal by fitting Marcenko’s dist
----> 3 out=minimize(lambda *x:errPDFs(*x),.5,args= (eVal,q,bWidth),bounds=((1E-5,1-1E-5),))
4 if out['success']:var=out['x'][0]
5 else:var=1
/opt/anaconda3/lib/python3.7/site-packages/scipy/optimize/_minimize.py in minimize(fun, x0, args, method, jac, hess, hessp, bounds, constraints, tol, callback, options)
598 return _minimize_neldermead(fun, x0, args, callback, **options)
599 elif meth == 'powell':
--> 600 return _minimize_powell(fun, x0, args, callback, **options)
601 elif meth == 'cg':
602 return _minimize_cg(fun, x0, args, jac, callback, **options)
/opt/anaconda3/lib/python3.7/site-packages/scipy/optimize/lbfgsb.py in _minimize_lbfgsb(fun, x0, args, jac, bounds, disp, maxcor, ftol, gtol, eps, maxfun, maxiter, iprint, callback, maxls, **unknown_options)
333
334 while 1:
--> 335 # x, f, g, wa, iwa, task, csave, lsave, isave, dsave = \
336 _lbfgsb.setulb(m, x, low_bnd, upper_bnd, nbd, f, g, factr,
337 pgtol, wa, iwa, task, iprint, csave, lsave,
/opt/anaconda3/lib/python3.7/site-packages/scipy/optimize/lbfgsb.py in func_and_grad(x)
278 # unbounded variables must use None, not +-inf, for optimizer to work properly
279 bounds = [(None if l == -np.inf else l, None if u == np.inf else u) for l, u in bounds]
--> 280
281 if disp is not None:
282 if disp == 0:
/opt/anaconda3/lib/python3.7/site-packages/scipy/optimize/optimize.py in function_wrapper(*wrapper_args)
324
325 def function_wrapper(*wrapper_args):
--> 326 ncalls[0] += 1
327 return function(*(wrapper_args + args))
328
<ipython-input-24-f44a1e9d84b1> in <lambda>(*x)
1 def findMaxEval(eVal,q,bWidth):
2 # Find max random eVal by fitting Marcenko’s dist
----> 3 out=minimize(lambda *x:errPDFs(*x),.5,args= (eVal,q,bWidth),bounds=((1E-5,1-1E-5),))
4 if out['success']:var=out['x'][0]
5 else:var=1
<ipython-input-23-24070a331535> in errPDFs(var, eVal, q, bWidth, pts)
1 def errPDFs(var,eVal,q,bWidth,pts=1000):
2 # Fit error
----> 3 pdf0=mpPDF(var,q,pts) # theoretical pdf
4 pdf1=fitKDE(eVal,bWidth,x=pdf0.index.values) # empirical pdf
5 sse=np.sum((pdf1-pdf0)**2)
<ipython-input-17-565d70018af2> in mpPDF(var, q, pts)
10 eVal=np.linspace(eMin,eMax,pts)
11 pdf=q/(2*np.pi*var*eVal)*((eMax-eVal)*(eVal-eMin))**.5
---> 12 pdf=pd.Series(pdf,index=eVal)
13 return pdf
/opt/anaconda3/lib/python3.7/site-packages/pandas/core/series.py in __init__(self, data, index, dtype, name, copy, fastpath)
312
313 def _init_dict(self, data, index=None, dtype=None):
--> 314 """
315 Derive the "_data" and "index" attributes of a new Series from a
316 dictionary input.
/opt/anaconda3/lib/python3.7/site-packages/pandas/core/internals/construction.py in sanitize_array(data, index, dtype, copy, raise_cast_failure)
Exception: Data must be 1-dimensional
I don't understand what should be 1-Dimensional. np.diag(eVal0) is of shape (1000,).
I looked at all the other similar questions, but none seems to help me solve this.
Thanks.
The error is unrelated to bounds.
For some reason minimize() calls the custom function errPDFs() with the argument to be optimized - minimize() calls this for x0 - which is an array. So if you redefine the function errPDFs() to extract the first element of an array:
def errPDFs(var, eVal, q, bWidth, pts=1000):
print("var:"+var)
pdf0 = mpPDF(var[0], q, pts) #theoretical pdf
pdf1 = fitKDE(eVal, bWidth, x=pdf0.index.values) #empirical pdf
sse = np.sum((pdf1-pdf0)**2)
print("sse:"+str(sse))
return sse
It should work.
Sample output:
>>> out = minimize(lambda *x: errPDFs(*x), .5, args=(eVal, q, bWidth),bounds=
((1E-5, 1-1E-5),))
var:[0.5]
sse:743.6200749295413
var:[0.50000001]
sse:743.6199819531047
var:[0.99999]
sse:289.1462047531385
...
Updated 6/29 ... I got it to run this way, which is strange because it is the same thing, must be a bug in the library or casting explicitly like this gets it into the precise format desired:
import numpy as np
import pandas as pd
from scipy.optimize import minimize
from sklearn.neighbors import KernelDensity
def findMaxEval(eVal, q, bWidth):
bnds = ((float(1e5/10000000000), float(0.99999*-1)),)
print(bnds)
out = minimize(lambda *x: errPDFs(*x), .5, args=(eVal, q, bWidth), bounds=bnds)
if out['success']: var = out['x'][0]
else: var = 1
eMax = var*(1+(1./q)**.5)**2
return eMax, var
def errPDFs(var, eVal, q, bWidth, pts = 1000):
pdf0 = mpPDF(var, q, pts)
pdf1 = fitKDE(eVal, bWidth, x=pdf0.index.values)
sse=np.sum((pdf1-pdf0)**2)
return sse
def mpPDF(var, q, pts):
eMin, eMax=var*(1-(1./q)**.5)**2,var*(1+(1./q)**.5)**2
eVal = np.linspace(eMin, eMax, pts)
pdf = q/(2*np.pi*var*eVal)*((eMax-eVal)*(eVal-eMin))**.5
pdf = pd.Series(pdf, index=eVal)
return pdf
def fitKDE(obs, bWidth = .25, kernel='gaussian', x=None):
if len(obs.shape) == 1: obs = obs.reshape(-1, 1)
kde=KernelDensity(kernel=kernel, bandwidth=bWidth).fit(obs)
if x is None: x = np.unique(obs).reshape(-1, 1)
if len(x.shape) == 1: x = x.reshape(-1, 1)
logProb = kde.score_samples(x)
pdf=pd.Series(np.exp(logProb), index=x.flatten())
return pdf
eMax0, var0 = findMaxEval((1000,), 10, bWidth=.01)
print(eMax0)
print(var0)
Here is the updated output on Macbook in PyCharm Community, Python version 3.8.1:
I'm passing my fmin this way:
position, value, dictionary =fmin_l_bfgs_b(func =self.objective,x0=wcb,fprime = self.objective_grad,args=(X,y),disp = 10)
This seems pretty standard: objective returns a scalar, objective gradient returns an ndarray of size 2n+1.
I am getting the following error:
<ipython-input-35-6a424a7ec22a> in fit(self, X, y)
35 wcb = np.asarray(wcb)
36 print(wcb)
---> 37 position, value, dictionary =fmin_l_bfgs_b(func =self.objective,x0=wcb,fprime = self.objective_grad,args=(X,y),disp = 10)
38
39
~\Anaconda3\envs\smc\lib\site-packages\scipy\optimize\lbfgsb.py in fmin_l_bfgs_b(func, x0, fprime, args, approx_grad, bounds, m, factr, pgtol, epsilon, iprint, maxfun, maxiter, disp, callback, maxls)
197
198 res = _minimize_lbfgsb(fun, x0, args=args, jac=jac, bounds=bounds,
--> 199 **opts)
200 d = {'grad': res['jac'],
201 'task': res['message'],
~\Anaconda3\envs\smc\lib\site-packages\scipy\optimize\lbfgsb.py in _minimize_lbfgsb(fun, x0, args, jac, bounds, disp, maxcor, ftol, gtol, eps, maxfun, maxiter, iprint, callback, maxls, **unknown_options)
326 _lbfgsb.setulb(m, x, low_bnd, upper_bnd, nbd, f, g, factr,
327 pgtol, wa, iwa, task, iprint, csave, lsave,
--> 328 isave, dsave, maxls)
329 task_str = task.tostring()
330 if task_str.startswith(b'FG'):
ValueError: failed to initialize intent(inout) array -- input 'O' not compatible to 'd'
Any idea how to get rid of it?