Skip to content

Instantly share code, notes, and snippets.

data = data.sort_values('Mean Temp').dropna().reset_index(drop=True)
fig, ax = plt.subplots(figsize=(20, 10))
sns.scatterplot(x='Mean Temp', y='Casual Trips', data=data)
from kneed import KneeLocator
from scipy.optimize import curve_fit
# Define the curve fitting equations
def linear(x, m, b):
return m*x + b
def exp_growth_no_shift(x, a, b):
return a * np.exp(-b * x)
def exp_growth(x, a, b, c):
return a * np.exp(-b * x) + c
popt, pcov = curve_fit(exp_growth_no_shift, x, y, maxfev=2000)
# Estimate the first knee point
kneedle = KneeLocator(x=x, y=exp_growth_no_shift(x, *popt), curve='convex', direction='increasing')
knee_start = kneedle.knee
print(knee_start)
def logistic_growth(x, k, x0):
return 1 / (1 + np.exp(-k*(x - x0)))
data['Cummulative Trips'] = data['Casual Trips'].cumsum()
data['Percentage Trips'] = data['Cummulative Trips'] / data['Casual Trips'].sum()
x = data['Mean Temp'].values
y = data['Percentage Trips'].values
popt, pcov = curve_fit(logistic_growth, x, y, maxfev=2000)
for idx, val in enumerate(x):
if val > knee_start:
kneedle = KneeLocator(x=x[idx:], y=y_fit[idx:], curve='concave', direction='increasing')
knee_end = kneedle.knee
print(knee_end)
break