Reputation: 531
I have a set of a lot of Dataframes with the following shape:
dat = pd.DataFrame({'Y':[0.0455,0.079,0.059,0.144],'X':[0.055,0.110,0.165,0.220]})
`dat
Out[14]:
Y X
0 0.0455 0.055
1 0.0790 0.110
2 0.0590 0.165
3 0.1440 0.220
I'm trying to fit this data with a function: Y = kX**m My expected output is to get parameters 'k' and 'm'
Seems that is not a hard job to do, but I only find some examples using 'scipy.optimize.curve_fit' that requieres several rounds of simulation to fit the data and I think is too complicated only for a few numbers. Some ideas?
CLUE: Using a commercial software I already know that k = -0.3176 m = 0.8935
Thanks in advance
Upvotes: 1
Views: 52
Reputation: 111
You could linearize your data/model and use least squares:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
dat = pd.DataFrame({'Y':[0.0455,0.079,0.059,0.144],'X':[0.055,0.110,0.165,0.220]})
def func(x, y):
Y = np.log(y)
X = np.log(x)
M = np.array((np.ones(len(X)), X)).T
lnk, m = np.linalg.solve(np.dot(M.T, M), np.dot(M.T, Y))
return np.exp(lnk), m
x = dat['X']
y = dat['Y']
k, m = func(x, y)
print('k: ', k)
print('m: ', m)
k: 0.2922891128901261
m: 0.6501306234660938
Upvotes: 1
Reputation: 3031
It is not too complicated to follow the examples on scipy.optimize.curve_fit
documentation.
Also, you won't get the same values as from the commercial software since your dataset is too small to be correctly fitted.
import pandas as pd
from scipy.optimize import curve_fit
def func(x, k, m):
return k * (x**m)
dat = pd.DataFrame({'Y': [0.0455, 0.079, 0.059, 0.144], 'X': [0.055, 0.110, 0.165, 0.220]})
popt, pcov = curve_fit(func, dat.X.values, dat.Y.values)
print('k:', popt[0])
print('m:', popt[1])
k: 0.4813840693004318
m: 0.8935556114206501
Upvotes: 2