my date is in an object format therefore I am struggling to convert it into float or integer
I am getting errors using the spline function in python.
How can I extract a mathematical equation from the spline plot? Thanks
my date is in an object format therefore I am struggling to convert it into float or integer
I am getting errors using the spline function in python.
How can I extract a mathematical equation from the spline plot? Thanks
Try converting the datetime to seconds with a time difference from the start. Use timedelta and total_seconds(). Here are additional suggestions on converting a datetime to seconds: Obtaining total seconds from a datetime.time object and working with time-series data in Pandas.
Here is example code for a cubic spline with gekko that also uses the spline to find the minimum.
from gekko import gekko
import numpy as np
import matplotlib.pyplot as plt
"""
minimize y
s.t. y = f(x)
using cubic spline with random sampling of data
"""
# Function to generate data for cspline
def f(x):
return 3*np.sin(x) - (x-3)
# Create model
c = gekko()
# Cubic spline
x = c.Var(value=15)
y = c.Var()
x_data = np.random.rand(50)*10+10
y_data = f(x_data)
c.cspline(x,y,x_data,y_data,True)
c.Obj(y)
# Options
c.options.IMODE = 3
c.options.CSV_READ = 0
c.options.SOLVER = 3
c.solve()
# Generate continuous trend for plot
z = np.linspace(10,20,100)
# Check if solved successfully
if c.options.SOLVESTATUS == 1:
plt.figure()
plt.plot(z,f(z),'r-',label='original')
plt.scatter(x_data,y_data,5,'b',label='data')
plt.scatter(x.value,y.value,200,'k','x',label='minimum')
plt.legend(loc='best')
else:
print ('Failed to converge!')
plt.figure()
plt.plot(z,f(z),'r-',label='original')
plt.scatter(x_data,y_data,5,'b')
plt.legend(loc='best')
plt.show()
As a cubic spline, there are separate cubic polynomial parameters for each segment of the spline. There is no closed form solution that can be compactly written. Try polynomial regression if there is a need to obtain a single equation that fits all of the data.