Monday, 3 May 2021

Scipy curve fit - vectorizing a conditional in exponents

I'm trying to use scipy curve_fit to capture the value of a0 parameter. As of now, it is not changing (always comes out as 1):

X = [[1,2,3],[4,5,6]]
def func(X, a0, c):
     x = X[0]; y = X[1]
     a = x*y
     result = np.where(a(a<a0), -c*(a + np.sqrt(y)), -c*x)
     return result

Popt, Cov = scipy.curve_fit(func, X, y) 
a0, c = Popt
Predicted = func(X, a0, c) # a0 and c are constants

I get the values for c, which is a scalar, without any problem. I can't explain why a0 (also a scalar) is always 1, and I am not sure how to fix it. I did see elsewhere on SO that np.where can be used the way I have written it here, but it doesn't work. I've spent much time on this, and can't manage to figure it out. The only explanation I have is that y requires the value of a0 to be 1, but I'm suspicious because it's precisely 1.0. I'd like some pointers!

I tried the construct suggested by Brad, but that's not it.



from Scipy curve fit - vectorizing a conditional in exponents

No comments:

Post a Comment