# Least Squares fit function - Python

I would like to find an approximation of deterministic function parameters with least_squares() python function but i get several issues - i am quite new in Python. Most of the issues were:

So I tried to take into account, but the function least_squares() doesn't return any parameter or something expected, I guess the least_squares() returns an array (https://docs.scipy.org/doc/scipy/reference/generated/scipy.optimize.least_squares.html#:~:text=Solve%20a%20nonlinear%20least%2Dsquares%20problem%20with%20bounds%20on%20the%20variables.&text=The%20purpose%20of%20the%20loss,of%20outliers%20on%20the%20solution.)

from scipy.optimize import least_squares, curve_fit, minimize, leastsq, shgo, fmin_cg
import numpy as np

def seasonality(coeff,x,y):
a = coeff[0]
b = coeff[1]
c1 =coeff[2]
c2 =coeff[3]
d1 = coeff[4]
d2 = coeff[5]
result = y - a - (b*x) - c1*math.sin(2*math.pi*x) - c2*math.cos(2*math.pi*x) - d1*math.sin(4*math.pi*x) - d2*math.cos(4*math.pi*x)
return np.int(result)

result = np.arange(1, 15.1, 0.1)
x0 = np.array([0.0,0.0,0.0,0.0,0.0,0.0])
df_bis = np.genfromtxt("NordPool_2013-2019_2.csv", delimiter = ',', skip_header = 1, invalid_raise = False)
seasonality2 = np.vectorize(seasonality)
z = least_squares(seasonality2, x0, jac="2-point", method="dogbox", verbose=2, f_scale=0.01,args=(df_bis[:,0],df_bis[:,2]))
print(z["y"])


I would like to estimate all the parameters a, b, c1, c2, d1 and d2 and the data I have is a csv file with:

1st col: Observations (1,2,3....)
2nd col: Hours(01-02, 02-03,...23-00, 01 - 02...)
3st col: Values (34.45, 37.38,...)


Maybe it comes from the structure of the function i ve created

I found the answer - could be useful for someone else :)

from scipy.optimize import least_squares, curve_fit, minimize, leastsq, shgo, fmin_cg

def seasonality(coeff,t,y):
a = coeff[0]
b = coeff[1]
c1 =coeff[2]
c2 =coeff[3]
d1 = coeff[4]
d2 = coeff[5]
return y - a - (b*t) - c1*np.sin(2*math.pi*t) - c2*np.cos(2*math.pi*t) - d1*np.sin(4*math.pi*t) - d2*np.cos(4*math.pi*t)

#result = np.arange(1, 15.1, 0.1)
x0 = np.array([0.0,0.0,0.0,0.0,0.0,0.0])
#df_bis = np.genfromtxt("NordPool_2013-2019_3.csv", delimiter = ';', skip_header = 1, invalid_raise = False)
#seasonality2 = np.vectorize(seasonality)
z = least_squares(seasonality, x0, jac="2-point", method="dogbox", verbose=2, f_scale=0.01,
args=(df_bis[:,0],df_bis[:,1]),
bounds=([-100,-100,-100,-100,-100,-100],[1000,1000,1000,1000,1000,1000]))
print(z["x"])$$$$
`