Python Forum
scipy curve_fit for multiple independent variables
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
scipy curve_fit for multiple independent variables
#1
Hello

I have been trying to fit my data to a custom equation. I have tried with scipy curve_fit and I have two independent variables x and y . I want to curve fit this data in order to get p,q and r. I used the following code
import matplotlib
import os,sys, math
import matplotlib.pyplot as plt
import numpy as np
import scipy as sy
from scipy import optimize
from scipy.optimize import curve_fit

xdata = [214.737191559, -5.64912101538e-36, 36.1372453686, 189.459700978, 233.562136902, 201.230228832, -5.59364882619e-36, -36.3232002416, -188.192199081, -212.837139143, -232.342545403, -200.699429716]
ydata = [-5.88273617837e-37, -211.536123799, -186.67108047, -35.9497006815, 200.282998159, 232.085860035, 213.44274878, 187.945919272, 35.7227474297, -6.00785257974e-37, -199.746844708, -230.856058666]

xdata = np.array(xdata)
ydata = np.array(ydata)

def func1(X,a,b,c):
    x,y = X
    
#     x = np.array(X[0])
#     y = np.array(X[1])
    
    n  = 8
#     % A  = ydata
#     % B  = -xdata
#     % C  = xdata. - ydata
#     % H  = zdata
    
    g = np.subtract(x,y)
    I_0 = np.subtract(x,y)   # x-y = C
    I_1 = np.multiply(I_0,c) # c(x-y) = cC
    I_2 = np.multiply(b,-x)   #b(-x) = bB
    I_3 = np.multiply(a,y)  # aA
    
    
    I3_0 = np.subtract(I_1,I_2) # cC-bB
    I3_1 = np.subtract(I_3,I_1) # aA-cC
    I3_2 = np.subtract(I_2,I_3) # bB-aA
    
    I3_00 = np.multiply(I3_0,I3_1) # (cC-bB)(aA-cC)
    I3_01 = np.multiply(I3_00,I3_2) # (cC-bB)(aA-cC)(bB-aA)
    
    I3 = np.divide(I3_01,54) # (cC-bB)(aA-cC)(bB-aA)/54
    
    I2_0 = np.power((I3_1),2)  # (aA-cC)^2
    I2_1 = np.power((I3_0),2)  # (cC-bB)^2
    I2_2 = np.power((I3_2),2)  # (bB-aA)^2
    
    I2_00 = np.add(I2_0,I2_1)  # (aA-cC)^2 + (cC-bB)^2
    I2_01 = np.add(I2_00,I2_2) # (aA-cC)^2 + (cC-bB)^2 + (bB-aA)^2
    
    I2 = np.divide(I2_01,54)  # ((aA-cC)^2 + (cC-bB)^2 + (bB-aA)^2)/54
    
    th_0 = np.divide(I3,(np.power(I2,(3/2))))  # I3/(I2^(3/2))
    
#     print(th_0)
    
    th = np.arccos(np.clip((th_0),-1,1))  # arccos(I3/(I2^(3/2)))
    
#     print(th)
    
    ans_0 = np.divide(np.add((2*th),(np.pi)),6)   # (2*th + pi)/6
    ans_1 = np.divide(np.add((2*th),(3*np.pi)),6) # (2*th + 3*pi)/6
    ans_2 = np.divide(np.add((2*th),(5*np.pi)),6) # (2*th + 5*pi)/6
    
    ans_00 = np.multiply(np.cos(ans_0),2)  # 2*cos((2*th + pi)/6)
    ans_11 = np.multiply(np.cos(ans_1),2)  # 2*cos((2*th + 3*pi)/6)
    ans_22 = np.multiply(np.cos(ans_2),2)  # 2*cos((2*th + 5*pi)/6)
    
    ans_000 = np.power(np.absolute(ans_00),n)  # (abs(2*cos((2*th + pi)/6)))^n
    ans_111 = np.power(np.absolute(ans_11),n)  # (abs(2*cos((2*th + 3*pi)/6)))^n
    ans_222 = np.power(np.absolute(ans_22),n)  # (abs(2*cos((2*th + 5*pi)/6)))^n
    
    ans_0000 = np.add((np.power(np.absolute(ans_00),n)),(np.power(np.absolute(ans_11),n))) # (abs(2*cos((2*th + pi)/6)))^n + (abs(2*cos((2*th + 3*pi)/6)))^n 
    ans_1111 = np.add((ans_0000),(np.power(np.absolute(ans_22),n)))  # (abs(2*cos((2*th + pi)/6)))^n + (abs(2*cos((2*th + 3*pi)/6)))^n + (abs(2*cos((2*th + 5*pi)/6)))^n
    
    sna_0 = np.power(np.multiply(3,I2),(n/2))  # (3*I2)^(n/2) !!
    sna_1 = 2*(np.power(190,n)) # 2*(sigma^n) !!
    
    sna_00 = np.multiply(sna_0,ans_1111)
    sna_11 = np.subtract(sna_00,sna_1)
    
    return sna_11

# initial guesses for a,b,c:
a, b, c = 1, 2, 3
p0      = np.array([a, b, c])
popt,pcov = (curve_fit(func1, (xdata,ydata), 1,1,1))
And I got the following error

Error:
--------------------------------------------------------------------------- ValueError Traceback (most recent call last) <ipython-input-33-54a610457b47> in <module>() 2 a, b, c = 1, 2, 3 3 p0 = np.array([a, b, c]) ----> 4 popt,pcov = (curve_fit(func1, (xdata,ydata), 1,1,1)) ~/.conda/envs/ML/lib/python3.6/site-packages/scipy/optimize/minpack.py in curve_fit(f, xdata, ydata, p0, sigma, absolute_sigma, check_finite, bounds, method, jac, **kwargs) 736 raise ValueError("`sigma` must be positive definite.") 737 else: --> 738 raise ValueError("`sigma` has incorrect shape.") 739 else: 740 transform = None ValueError: `sigma` has incorrect shape.

(May-07-2019, 08:07 AM)Jay_Nerella Wrote: Hello

I have been trying to fit my data to a custom equation. I have tried with scipy curve_fit and I have two independent variables x and y . I want to curve fit this data in order to get a,b and c. I used the following code
import matplotlib
import os,sys, math
import matplotlib.pyplot as plt
import numpy as np
import scipy as sy
from scipy import optimize
from scipy.optimize import curve_fit

xdata = [214.737191559, -5.64912101538e-36, 36.1372453686, 189.459700978, 233.562136902, 201.230228832, -5.59364882619e-36, -36.3232002416, -188.192199081, -212.837139143, -232.342545403, -200.699429716]
ydata = [-5.88273617837e-37, -211.536123799, -186.67108047, -35.9497006815, 200.282998159, 232.085860035, 213.44274878, 187.945919272, 35.7227474297, -6.00785257974e-37, -199.746844708, -230.856058666]

xdata = np.array(xdata)
ydata = np.array(ydata)

def func1(X,a,b,c):
    x,y = X
    
#     x = np.array(X[0])
#     y = np.array(X[1])
    
    n  = 8
#     % A  = ydata
#     % B  = -xdata
#     % C  = xdata. - ydata
#     % H  = zdata
    
    g = np.subtract(x,y)
    I_0 = np.subtract(x,y)   # x-y = C
    I_1 = np.multiply(I_0,c) # c(x-y) = cC
    I_2 = np.multiply(b,-x)   #b(-x) = bB
    I_3 = np.multiply(a,y)  # aA
    
    
    I3_0 = np.subtract(I_1,I_2) # cC-bB
    I3_1 = np.subtract(I_3,I_1) # aA-cC
    I3_2 = np.subtract(I_2,I_3) # bB-aA
    
    I3_00 = np.multiply(I3_0,I3_1) # (cC-bB)(aA-cC)
    I3_01 = np.multiply(I3_00,I3_2) # (cC-bB)(aA-cC)(bB-aA)
    
    I3 = np.divide(I3_01,54) # (cC-bB)(aA-cC)(bB-aA)/54
    
    I2_0 = np.power((I3_1),2)  # (aA-cC)^2
    I2_1 = np.power((I3_0),2)  # (cC-bB)^2
    I2_2 = np.power((I3_2),2)  # (bB-aA)^2
    
    I2_00 = np.add(I2_0,I2_1)  # (aA-cC)^2 + (cC-bB)^2
    I2_01 = np.add(I2_00,I2_2) # (aA-cC)^2 + (cC-bB)^2 + (bB-aA)^2
    
    I2 = np.divide(I2_01,54)  # ((aA-cC)^2 + (cC-bB)^2 + (bB-aA)^2)/54
    
    th_0 = np.divide(I3,(np.power(I2,(3/2))))  # I3/(I2^(3/2))
    
#     print(th_0)
    
    th = np.arccos(np.clip((th_0),-1,1))  # arccos(I3/(I2^(3/2)))
    
#     print(th)
    
    ans_0 = np.divide(np.add((2*th),(np.pi)),6)   # (2*th + pi)/6
    ans_1 = np.divide(np.add((2*th),(3*np.pi)),6) # (2*th + 3*pi)/6
    ans_2 = np.divide(np.add((2*th),(5*np.pi)),6) # (2*th + 5*pi)/6
    
    ans_00 = np.multiply(np.cos(ans_0),2)  # 2*cos((2*th + pi)/6)
    ans_11 = np.multiply(np.cos(ans_1),2)  # 2*cos((2*th + 3*pi)/6)
    ans_22 = np.multiply(np.cos(ans_2),2)  # 2*cos((2*th + 5*pi)/6)
    
    ans_000 = np.power(np.absolute(ans_00),n)  # (abs(2*cos((2*th + pi)/6)))^n
    ans_111 = np.power(np.absolute(ans_11),n)  # (abs(2*cos((2*th + 3*pi)/6)))^n
    ans_222 = np.power(np.absolute(ans_22),n)  # (abs(2*cos((2*th + 5*pi)/6)))^n
    
    ans_0000 = np.add((np.power(np.absolute(ans_00),n)),(np.power(np.absolute(ans_11),n))) # (abs(2*cos((2*th + pi)/6)))^n + (abs(2*cos((2*th + 3*pi)/6)))^n 
    ans_1111 = np.add((ans_0000),(np.power(np.absolute(ans_22),n)))  # (abs(2*cos((2*th + pi)/6)))^n + (abs(2*cos((2*th + 3*pi)/6)))^n + (abs(2*cos((2*th + 5*pi)/6)))^n
    
    sna_0 = np.power(np.multiply(3,I2),(n/2))  # (3*I2)^(n/2) !!
    sna_1 = 2*(np.power(190,n)) # 2*(sigma^n) !!
    
    sna_00 = np.multiply(sna_0,ans_1111)
    sna_11 = np.subtract(sna_00,sna_1)
    
    return sna_11

# initial guesses for a,b,c:
a, b, c = 1, 2, 3
p0      = np.array([a, b, c])
popt,pcov = (curve_fit(func1, (xdata,ydata), 1,1,1))
And I got the following error

Error:
--------------------------------------------------------------------------- ValueError Traceback (most recent call last) <ipython-input-33-54a610457b47> in <module>() 2 a, b, c = 1, 2, 3 3 p0 = np.array([a, b, c]) ----> 4 popt,pcov = (curve_fit(func1, (xdata,ydata), 1,1,1)) ~/.conda/envs/ML/lib/python3.6/site-packages/scipy/optimize/minpack.py in curve_fit(f, xdata, ydata, p0, sigma, absolute_sigma, check_finite, bounds, method, jac, **kwargs) 736 raise ValueError("`sigma` must be positive definite.") 737 else: --> 738 raise ValueError("`sigma` has incorrect shape.") 739 else: 740 transform = None ValueError: `sigma` has incorrect shape.
Reply
#2
I've seen on this optimization problem several times, but couldn't understand what you really want.
You have F = func1, and you try to fit it as follows:

Quote:F(xdata, parameters) = ydata

(xdata, ydata) are known; parameters should be determined (e.g. in least squares sense).

If the problem could be formulated as I just did, you could try least_squares utility function
to solve it.

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Using scipy.optimize: curve_fit ju21878436312 0 934 Sep-24-2022, 01:15 PM
Last Post: ju21878436312
  How to get coefficient of determination R2 after scipy.curve_fit? AlekseyPython 0 1,838 Feb-12-2021, 09:03 AM
Last Post: AlekseyPython
  Time Series forecating with multiple independent variables Krychol88 1 1,824 Oct-23-2020, 08:11 AM
Last Post: DPaul
  curve_fit rizal 3 4,251 Dec-20-2017, 04:53 PM
Last Post: hshivaraj

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020