This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from shiny import App, render, ui | |
app_ui = ui.page_fluid( | |
ui.h2("Hello Shiny!"), | |
ui.input_slider("n", "N", 0, 100, 20), | |
ui.output_text_verbatim("txt"), | |
) | |
def server(input, output, session): |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
score_list = [] | |
for fit_col in ["model_1", "model_2", "model_3"]: | |
scores = { | |
"model": fit_col, | |
"train_score": mean_absolute_error( | |
results_df.iloc[:TRAIN_END]["actuals"], | |
results_df.iloc[:TRAIN_END][fit_col] | |
), | |
"test_score": mean_absolute_error( | |
results_df.iloc[TRAIN_END:]["actuals"], |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
results_df.plot(title="Comparison of fits using different time-based features", | |
figsize=(16,4), | |
color = ["c", "k", "b", "r"]) | |
plt.axvline(date(2020, 1, 1), c="m", linestyle="--"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
model_3 = LinearRegression().fit(X_3.iloc[:TRAIN_END], | |
y.iloc[:TRAIN_END]) | |
results_df["model_3"] = model_3.predict(X_3) | |
results_df[["actuals", "model_3"]].plot(figsize=(16,4), | |
title="Fit using RBF features") | |
plt.axvline(date(2020, 1, 1), c="m", linestyle="--"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
rbf = RepeatingBasisFunction(n_periods=12, | |
column="day_of_year", | |
input_range=(1,365), | |
remainder="drop") | |
rbf.fit(X) | |
X_3 = pd.DataFrame(index=X.index, | |
data=rbf.transform(X)) | |
X_3.plot(subplots=True, figsize=(14, 8), | |
sharex=True, title="Radial Basis Functions", | |
legend=False); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
X_2_daily = X_2[["day_sin", "day_cos"]] | |
model_2 = LinearRegression().fit(X_2_daily.iloc[:TRAIN_END], | |
y.iloc[:TRAIN_END]) | |
results_df["model_2"] = model_2.predict(X_2_daily) | |
results_df[["actuals", "model_2"]].plot(figsize=(16,4), | |
title="Fit using sine/cosine features") | |
plt.axvline(date(2020, 1, 1), c="m", linestyle="--"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
X_2 = X.copy() | |
X_2["month"] = X_2.index.month | |
X_2["month_sin"] = sin_transformer(12).fit_transform(X_2)["month"] | |
X_2["month_cos"] = cos_transformer(12).fit_transform(X_2)["month"] | |
X_2["day_sin"] = sin_transformer(365).fit_transform(X_2)["day_of_year"] | |
X_2["day_cos"] = cos_transformer(365).fit_transform(X_2)["day_of_year"] | |
fig, ax = plt.subplots(2, 1, sharex=True, figsize=(16,8)) | |
X_2[["month_sin", "month_cos"]].plot(ax=ax[0]) | |
X_2[["day_sin", "day_cos"]].plot(ax=ax[1]) | |
plt.suptitle("Cyclical encoding with sine/cosine transformation"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def sin_transformer(period): | |
return FunctionTransformer(lambda x: np.sin(x / period * 2 * np.pi)) | |
def cos_transformer(period): | |
return FunctionTransformer(lambda x: np.cos(x / period * 2 * np.pi)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
model_1 = LinearRegression().fit(X_1.iloc[:TRAIN_END], | |
y.iloc[:TRAIN_END]) | |
results_df["model_1"] = model_1.predict(X_1) | |
results_df[["actuals", "model_1"]].plot(figsize=(16,4), | |
title="Fit using month dummies") | |
plt.axvline(date(2020, 1, 1), c="m", linestyle="--"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
X_1 = pd.DataFrame( | |
data=pd.get_dummies(X.index.month, drop_first=True, prefix="month") | |
) | |
X_1.index = X.index | |
X_1 |