Mohammad Haizad
commited on
Commit
·
564fdde
1
Parent(s):
5a02990
initial commit
Browse files- README.md +1 -1
- app.py +123 -0
- requirements.txt +3 -0
README.md
CHANGED
|
@@ -4,7 +4,7 @@ emoji: 🏃
|
|
| 4 |
colorFrom: gray
|
| 5 |
colorTo: gray
|
| 6 |
sdk: gradio
|
| 7 |
-
sdk_version: 3.
|
| 8 |
app_file: app.py
|
| 9 |
pinned: false
|
| 10 |
license: mit
|
|
|
|
| 4 |
colorFrom: gray
|
| 5 |
colorTo: gray
|
| 6 |
sdk: gradio
|
| 7 |
+
sdk_version: 3.27.0
|
| 8 |
app_file: app.py
|
| 9 |
pinned: false
|
| 10 |
license: mit
|
app.py
ADDED
|
@@ -0,0 +1,123 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import numpy as np
|
| 3 |
+
import matplotlib
|
| 4 |
+
matplotlib.use("Agg")
|
| 5 |
+
import matplotlib.pyplot as plt
|
| 6 |
+
from sklearn.datasets import fetch_openml
|
| 7 |
+
from sklearn.utils import shuffle
|
| 8 |
+
from sklearn.ensemble import StackingRegressor
|
| 9 |
+
from sklearn.linear_model import RidgeCV
|
| 10 |
+
from skops.hub_utils import download
|
| 11 |
+
import joblib
|
| 12 |
+
import shutil
|
| 13 |
+
|
| 14 |
+
# load dataset
|
| 15 |
+
def load_ames_housing():
|
| 16 |
+
df = fetch_openml(name="house_prices", as_frame=True, parser="pandas")
|
| 17 |
+
X = df.data
|
| 18 |
+
y = df.target
|
| 19 |
+
|
| 20 |
+
features = [
|
| 21 |
+
"YrSold",
|
| 22 |
+
"HeatingQC",
|
| 23 |
+
"Street",
|
| 24 |
+
"YearRemodAdd",
|
| 25 |
+
"Heating",
|
| 26 |
+
"MasVnrType",
|
| 27 |
+
"BsmtUnfSF",
|
| 28 |
+
"Foundation",
|
| 29 |
+
"MasVnrArea",
|
| 30 |
+
"MSSubClass",
|
| 31 |
+
"ExterQual",
|
| 32 |
+
"Condition2",
|
| 33 |
+
"GarageCars",
|
| 34 |
+
"GarageType",
|
| 35 |
+
"OverallQual",
|
| 36 |
+
"TotalBsmtSF",
|
| 37 |
+
"BsmtFinSF1",
|
| 38 |
+
"HouseStyle",
|
| 39 |
+
"MiscFeature",
|
| 40 |
+
"MoSold",
|
| 41 |
+
]
|
| 42 |
+
|
| 43 |
+
X = X.loc[:, features]
|
| 44 |
+
X, y = shuffle(X, y, random_state=0)
|
| 45 |
+
|
| 46 |
+
X = X.iloc[:600]
|
| 47 |
+
y = y.iloc[:600]
|
| 48 |
+
return X, np.log(y)
|
| 49 |
+
|
| 50 |
+
def stacked_model(model1,model2,model3):
|
| 51 |
+
X, y = load_ames_housing()
|
| 52 |
+
estimators = []
|
| 53 |
+
for model in [model1,model2,model3]:
|
| 54 |
+
download(repo_id="haizad/ames-housing-lasso-predictor", dst='temp_dir')
|
| 55 |
+
pipeline = joblib.load( "temp_dir/model.pkl")
|
| 56 |
+
estimators.append((model.split('/')[-1], pipeline))
|
| 57 |
+
shutil.rmtree("temp_dir")
|
| 58 |
+
|
| 59 |
+
stacking_regressor = StackingRegressor(estimators=estimators, final_estimator=RidgeCV())
|
| 60 |
+
|
| 61 |
+
# plot and compare the performance of the single models and the stacked model
|
| 62 |
+
import time
|
| 63 |
+
import matplotlib.pyplot as plt
|
| 64 |
+
from sklearn.metrics import PredictionErrorDisplay
|
| 65 |
+
from sklearn.model_selection import cross_validate, cross_val_predict
|
| 66 |
+
|
| 67 |
+
fig, axs = plt.subplots(2, 2, figsize=(9, 7))
|
| 68 |
+
axs = np.ravel(axs)
|
| 69 |
+
|
| 70 |
+
for ax, (name, est) in zip(
|
| 71 |
+
axs, estimators + [("Stacking Regressor", stacking_regressor)]
|
| 72 |
+
):
|
| 73 |
+
scorers = {"R2": "r2", "MAE": "neg_mean_absolute_error"}
|
| 74 |
+
|
| 75 |
+
start_time = time.time()
|
| 76 |
+
scores = cross_validate(
|
| 77 |
+
est, X, y, scoring=list(scorers.values()), n_jobs=-1, verbose=0
|
| 78 |
+
)
|
| 79 |
+
|
| 80 |
+
elapsed_time = time.time() - start_time
|
| 81 |
+
|
| 82 |
+
y_pred = cross_val_predict(est, X, y, n_jobs=-1, verbose=0)
|
| 83 |
+
scores = {
|
| 84 |
+
key: (
|
| 85 |
+
f"{np.abs(np.mean(scores[f'test_{value}'])):.2f} +- "
|
| 86 |
+
f"{np.std(scores[f'test_{value}']):.2f}"
|
| 87 |
+
)
|
| 88 |
+
for key, value in scorers.items()
|
| 89 |
+
}
|
| 90 |
+
|
| 91 |
+
display = PredictionErrorDisplay.from_predictions(
|
| 92 |
+
y_true=y,
|
| 93 |
+
y_pred=y_pred,
|
| 94 |
+
kind="actual_vs_predicted",
|
| 95 |
+
ax=ax,
|
| 96 |
+
scatter_kwargs={"alpha": 0.2, "color": "tab:blue"},
|
| 97 |
+
line_kwargs={"color": "tab:red"},
|
| 98 |
+
)
|
| 99 |
+
ax.set_title(f"{name}\nEvaluation in {elapsed_time:.2f} seconds")
|
| 100 |
+
|
| 101 |
+
for name, score in scores.items():
|
| 102 |
+
ax.plot([], [], " ", label=f"{name}: {score}")
|
| 103 |
+
ax.legend(loc="upper left")
|
| 104 |
+
|
| 105 |
+
fig.suptitle("Single predictors versus stacked predictors")
|
| 106 |
+
fig.tight_layout()
|
| 107 |
+
fig.subplots_adjust(top=0.9)
|
| 108 |
+
return fig
|
| 109 |
+
|
| 110 |
+
title = "Multi-class AdaBoosted Decision Trees"
|
| 111 |
+
with gr.Blocks(title=title) as demo:
|
| 112 |
+
gr.Markdown(f"## {title}")
|
| 113 |
+
gr.Markdown("This app demonstrates the Multi-class AdaBoosted Decision Trees")
|
| 114 |
+
|
| 115 |
+
model1 = gr.Textbox(label="Repo id of first model")
|
| 116 |
+
model2 = gr.Textbox(label="Repo id of second model")
|
| 117 |
+
model3 = gr.Textbox(label="Repo id of third model")
|
| 118 |
+
plot = gr.Plot()
|
| 119 |
+
stack_btn = gr.Button("Stack")
|
| 120 |
+
stack_btn.click(fn=stacked_model, inputs=[model1,model2,model3], outputs=[plot])
|
| 121 |
+
|
| 122 |
+
demo.launch()
|
| 123 |
+
|
requirements.txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
scikit-learn==1.2.2
|
| 2 |
+
matplotlib==3.7.1
|
| 3 |
+
skops==0.6.0
|