Skip to content

Commit ada266f

Browse files
committed
add quantile regressor but it is weird
1 parent 9579085 commit ada266f

1 file changed

Lines changed: 121 additions & 0 deletions

File tree

content/python_files/feature_engineering.py

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1524,3 +1524,124 @@ def scoring(regressor, X, y):
15241524
display(chart)
15251525

15261526
# %%
1527+
from sklearn.metrics import mean_pinball_loss
1528+
1529+
scoring = {
1530+
"r2": get_scorer("r2"),
1531+
"mape": mape_scorer,
1532+
"mean_pinball_05_loss": make_scorer(mean_pinball_loss, alpha=0.05),
1533+
"mean_pinball_50_loss": make_scorer(mean_pinball_loss, alpha=0.5),
1534+
"mean_pinball_95_loss": make_scorer(mean_pinball_loss, alpha=0.95),
1535+
}
1536+
1537+
# %%
1538+
common_params = dict(
1539+
loss="quantile", learning_rate=0.1, max_leaf_nodes=100, random_state=0
1540+
)
1541+
predictions_gbrt_05 = features_with_dropped_cols.skb.apply(
1542+
HistGradientBoostingRegressor(**common_params, quantile=0.05),
1543+
y=target,
1544+
)
1545+
predictions_gbrt_50 = features_with_dropped_cols.skb.apply(
1546+
HistGradientBoostingRegressor(**common_params, quantile=0.5),
1547+
y=target,
1548+
)
1549+
predictions_gbrt_95 = features_with_dropped_cols.skb.apply(
1550+
HistGradientBoostingRegressor(**common_params, quantile=0.95),
1551+
y=target,
1552+
)
1553+
1554+
# %%
1555+
cv_results_05 = predictions_gbrt_05.skb.cross_validate(
1556+
cv=ts_cv_5,
1557+
scoring=scoring,
1558+
verbose=1,
1559+
n_jobs=-1,
1560+
)
1561+
cv_results_50 = predictions_gbrt_50.skb.cross_validate(
1562+
cv=ts_cv_5,
1563+
scoring=scoring,
1564+
verbose=1,
1565+
n_jobs=-1,
1566+
)
1567+
cv_results_95 = predictions_gbrt_95.skb.cross_validate(
1568+
cv=ts_cv_5,
1569+
scoring=scoring,
1570+
verbose=1,
1571+
n_jobs=-1,
1572+
)
1573+
1574+
# %%
1575+
cv_results_05.mean(axis=0).round(3)
1576+
1577+
# %%
1578+
cv_results_50.mean(axis=0).round(3)
1579+
1580+
# %%
1581+
cv_results_95.mean(axis=0).round(3)
1582+
1583+
# %%
1584+
results = pl.concat(
1585+
[
1586+
targets.skb.select(cols=["prediction_time", target_column_name]).skb.eval(),
1587+
predictions_gbrt_05.rename({target_column_name: "quantile_05"}).skb.eval(),
1588+
predictions_gbrt_50.rename({target_column_name: "median"}).skb.eval(),
1589+
predictions_gbrt_95.rename({target_column_name: "quantile_95"}).skb.eval(),
1590+
],
1591+
how="horizontal",
1592+
).tail(24 * 7)
1593+
1594+
# %%
1595+
median_chart = (
1596+
altair.Chart(results)
1597+
.transform_fold([target_column_name, "median"])
1598+
.mark_line(tooltip=True)
1599+
.encode(x="prediction_time:T", y="value:Q", color="key:N")
1600+
)
1601+
1602+
quantile_band_chart = (
1603+
altair.Chart(results)
1604+
.mark_area(opacity=0.4, tooltip=True)
1605+
.encode(
1606+
x="prediction_time:T",
1607+
y="quantile_05:Q",
1608+
y2="quantile_95:Q",
1609+
color=altair.value("green"),
1610+
)
1611+
)
1612+
1613+
combined_chart = quantile_band_chart + median_chart
1614+
combined_chart.interactive()
1615+
1616+
# %%
1617+
import matplotlib.pyplot as plt
1618+
from sklearn.metrics import PredictionErrorDisplay
1619+
1620+
fig, axs = plt.subplots(1, 3, figsize=(15, 5), sharey=True)
1621+
1622+
PredictionErrorDisplay.from_predictions(
1623+
y_true=targets["load_mw_horizon_24h"].skb.eval().to_numpy(),
1624+
y_pred=predictions_gbrt_05["load_mw_horizon_24h"].skb.eval().to_numpy(),
1625+
kind="actual_vs_predicted",
1626+
ax=axs[0],
1627+
)
1628+
axs[0].set_title("0.05 Quantile")
1629+
1630+
PredictionErrorDisplay.from_predictions(
1631+
y_true=targets["load_mw_horizon_24h"].skb.eval().to_numpy(),
1632+
y_pred=predictions_gbrt_50["load_mw_horizon_24h"].skb.eval().to_numpy(),
1633+
kind="actual_vs_predicted",
1634+
ax=axs[1],
1635+
)
1636+
axs[1].set_title("Median")
1637+
1638+
PredictionErrorDisplay.from_predictions(
1639+
y_true=targets["load_mw_horizon_24h"].skb.eval().to_numpy(),
1640+
y_pred=predictions_gbrt_95["load_mw_horizon_24h"].skb.eval().to_numpy(),
1641+
kind="actual_vs_predicted",
1642+
ax=axs[2],
1643+
)
1644+
axs[2].set_title("0.95 Quantile")
1645+
_ = fig.suptitle("GBRT Predictions for different quantiles")
1646+
1647+
# %%

0 commit comments

Comments
 (0)