JavaScript
x
28
28
1
def func(x,a,b):
2
3
return a*x + b
4
5
guess = (0.5,0.5,0.5)
6
7
8
fit_df = df.dropna()
9
10
col_params = {}
11
12
for col in fit_df.columns:
13
14
x = fit_df.index.astype(float).values
15
16
y = fit_df[col].values
17
18
params = curve_fit(func,x,y,guess)
19
20
col_params[col] = params[0]
21
for col in df.columns:
22
23
x = df[pd.isnull(df[col])].index.astype(float).values
24
25
df[col][x] = func(x,*col_params[col])
26
print("Extrapolated data")
27
print(df)
28
I’m using the code from another post to extrapolate values. I changed the func() so that it is linear not cubic however I get an error “func() takes 3 positional arguments but 4 were give”
Extrapolate values in Pandas DataFrame is where I got the original code. My question is how would I change it so it works with a linear relationship
Advertisement
Answer
When using:
guess = (0.5,0.5)
you should be able to make it run.
You have the parameters a, b
while the original example had the parameters a, b, c, d
.
The initial guess is for the parameters a, b
in your function, not for x
.
Full code used to make your interpolation function run:
JavaScript
1
56
56
1
import pandas as pd
2
from io import StringIO
3
from scipy.optimize import curve_fit
4
5
df = pd.read_table(StringIO('''
6
neg neu pos avg
7
0 NaN NaN NaN NaN
8
250 0.508475 0.527027 0.641292 0.558931
9
500 NaN NaN NaN NaN
10
1000 0.650000 0.571429 0.653983 0.625137
11
2000 NaN NaN NaN NaN
12
3000 0.619718 0.663158 0.665468 0.649448
13
4000 NaN NaN NaN NaN
14
6000 NaN NaN NaN NaN
15
8000 NaN NaN NaN NaN
16
10000 NaN NaN NaN NaN
17
20000 NaN NaN NaN NaN
18
30000 NaN NaN NaN NaN
19
50000 NaN NaN NaN NaN'''), sep='s+')
20
21
# Do the original interpolation
22
df.interpolate(method='nearest', xis=0, inplace=True)
23
24
# Display result
25
print ('Interpolated data:')
26
print (df)
27
print ()
28
29
def func(x,a,b):
30
31
return a*x + b
32
33
guess = (0.5,0.5)
34
35
36
fit_df = df.dropna()
37
38
col_params = {}
39
40
for col in fit_df.columns:
41
42
x = fit_df.index.astype(float).values
43
44
y = fit_df[col].values
45
46
params = curve_fit(func,x,y,guess)
47
48
col_params[col] = params[0]
49
for col in df.columns:
50
51
x = df[pd.isnull(df[col])].index.astype(float).values
52
53
df[col][x] = func(x,*col_params[col])
54
print("Extrapolated data")
55
print(df)
56