Skip to content

Instantly share code, notes, and snippets.

@danyashorokh
Created February 5, 2019 08:44
Show Gist options
  • Select an option

  • Save danyashorokh/acc26505ce035c1738931e0b0b954ef7 to your computer and use it in GitHub Desktop.

Select an option

Save danyashorokh/acc26505ce035c1738931e0b0b954ef7 to your computer and use it in GitHub Desktop.
[Python] Interpolate & extrapolate
from scipy.optimize import curve_fit
# interpolate
df_int[field] = df[field].interpolate(method='piecewise_polynomial')
'''
'linear’, ‘time’, ‘index’, ‘values’, ‘nearest’, ‘zero’,
‘slinear’, ‘quadratic’, ‘cubic’, ‘barycentric’, ‘krogh’, ‘polynomial’, ‘spline’ ‘piecewise_polynomial’, ‘pchip’}
'''
# extrapolate
df_ext = df_int.copy()
# Function to curve fit to the data
def func(x, a, b, c, d):
return a * (x ** 3) + b * (x ** 2) + c * x + d
# Initial parameter guess, just to kick off the optimization
guess = (0.5, 0.5, 0.5, 0.5)
# Create copy of data to remove NaNs for curve fitting
fit_df = df_ext.dropna()
# Place to store function parameters for each column
col_params = {}
# Curve fit each column
for col in fit_df.columns:
# Get x & y
x = fit_df.index.astype(float).values
y = fit_df[col].values
# Curve fit column and get curve parameters
params = curve_fit(func, x, y, guess)
# Store optimized parameters
col_params[col] = params[0]
# Extrapolate each column
for col in df_ext.columns:
# Get the index values for NaNs in the column
x = df_ext[pd.isnull(df_ext[col])].index.astype(float).values
# Extrapolate those points with the fitted function
df_ext[col][x] = func(x, *col_params[col])
# Display result
print('Extrapolated data:')
printdf_extdf)
print('Data was extrapolated with these column functions:')
for col in col_params:
print('f_{}(x) = {:0.3e} x^3 + {:0.3e} x^2 + {:0.4f} x + {:0.4f}'.format(col, *col_params[col]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment