Commit
·
239a99b
1
Parent(s):
e757838
Gradio app to run example
Browse files
app.py
ADDED
@@ -0,0 +1,97 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from sklearn.pipeline import make_pipeline
|
3 |
+
from sklearn.metrics import roc_curve, auc
|
4 |
+
from sklearn.datasets import make_classification
|
5 |
+
from sklearn.linear_model import LogisticRegression
|
6 |
+
from sklearn.model_selection import train_test_split
|
7 |
+
from sklearn.preprocessing import FunctionTransformer, OneHotEncoder
|
8 |
+
from sklearn.ensemble import RandomForestClassifier, GradientBoostingClassifier, RandomTreesEmbedding
|
9 |
+
|
10 |
+
import utils
|
11 |
+
|
12 |
+
def app_fn(n_samples: int, n_estimators: int, max_depth: int):
|
13 |
+
# Create Data
|
14 |
+
(X_train_ensemble, y_train_ensemble), \
|
15 |
+
(X_train_linear, y_train_linear), \
|
16 |
+
(X_test, y_test) = utils.create_and_split_dataset(n_samples)
|
17 |
+
|
18 |
+
# Creating and fitting Random Forest
|
19 |
+
random_forest = RandomForestClassifier(
|
20 |
+
n_estimators=n_estimators, max_depth=max_depth, random_state=10
|
21 |
+
)
|
22 |
+
random_forest.fit(X_train_ensemble, y_train_ensemble)
|
23 |
+
|
24 |
+
# Creating and fitting Gradient Boosting
|
25 |
+
gradient_boosting = GradientBoostingClassifier(
|
26 |
+
n_estimators=n_estimators, max_depth=max_depth, random_state=10
|
27 |
+
)
|
28 |
+
_ = gradient_boosting.fit(X_train_ensemble, y_train_ensemble)
|
29 |
+
|
30 |
+
# Creating and fitting Pipeline of Random Tree Embedding w/ Logistic Regression
|
31 |
+
random_tree_embedding = RandomTreesEmbedding(
|
32 |
+
n_estimators=n_estimators, max_depth=max_depth, random_state=0
|
33 |
+
)
|
34 |
+
rt_model = make_pipeline(random_tree_embedding, LogisticRegression(max_iter=1000))
|
35 |
+
rt_model.fit(X_train_linear, y_train_linear)
|
36 |
+
|
37 |
+
# Creating and fitting Pipeline of Random Forest Embedding w/ Logistic Regression
|
38 |
+
rf_leaves_yielder = FunctionTransformer(utils.rf_apply, kw_args={"model": random_forest})
|
39 |
+
rf_model = make_pipeline(
|
40 |
+
rf_leaves_yielder,
|
41 |
+
OneHotEncoder(handle_unknown="ignore"),
|
42 |
+
LogisticRegression(max_iter=1000),
|
43 |
+
)
|
44 |
+
rf_model.fit(X_train_linear, y_train_linear)
|
45 |
+
|
46 |
+
# Creating and fitting Pipeline of Gradient Boosting Embedding w/ Logistic Regression
|
47 |
+
gbdt_leaves_yielder = FunctionTransformer(
|
48 |
+
utils.gbdt_apply, kw_args={"model": gradient_boosting}
|
49 |
+
)
|
50 |
+
gbdt_model = make_pipeline(
|
51 |
+
gbdt_leaves_yielder,
|
52 |
+
OneHotEncoder(handle_unknown="ignore"),
|
53 |
+
LogisticRegression(max_iter=1000),
|
54 |
+
)
|
55 |
+
gbdt_model.fit(X_train_linear, y_train_linear)
|
56 |
+
|
57 |
+
# Plotting ROC Curve
|
58 |
+
models = [
|
59 |
+
("RT embedding -> LR", rt_model),
|
60 |
+
("RF", random_forest),
|
61 |
+
("RF embedding -> LR", rf_model),
|
62 |
+
("GBDT", gradient_boosting),
|
63 |
+
("GBDT embedding -> LR", gbdt_model),
|
64 |
+
]
|
65 |
+
|
66 |
+
fig = utils.plot_roc(
|
67 |
+
X_test,
|
68 |
+
y_test,
|
69 |
+
models
|
70 |
+
)
|
71 |
+
|
72 |
+
return fig
|
73 |
+
|
74 |
+
title="Feature Transformations with Ensembles of Trees 🌳"
|
75 |
+
with gr.Blocks(title=title) as demo:
|
76 |
+
gr.Markdown(f"# {title}")
|
77 |
+
gr.Markdown(
|
78 |
+
"""
|
79 |
+
## This example shows how one can apply features transformations using ensembles of trees \
|
80 |
+
on a synthetic dataset. The transformations are then used to train a linear model on the \
|
81 |
+
transformed data. The plot shows the ROC curve of the different models trained on the \
|
82 |
+
transformed data. The plot is interactive and you can zoom in and out.
|
83 |
+
"""
|
84 |
+
)
|
85 |
+
|
86 |
+
with gr.Row():
|
87 |
+
with gr.Column():
|
88 |
+
n_samples = gr.inputs.Slider(50_000, 100_000, 1000, label="Number of Samples", default=80_000)
|
89 |
+
n_estimators = gr.inputs.Slider(10, 100, 10, label="Number of Estimators", default=10)
|
90 |
+
max_depth = gr.inputs.Slider(1, 10, 1, label="Max Depth", default=3)
|
91 |
+
plot = gr.Plot(label="ROC Curve")
|
92 |
+
|
93 |
+
Reduction = gr.Button("Run")
|
94 |
+
Reduction.click(fn=app_fn, inputs=[n_samples, n_estimators, max_depth], outputs=[plot])
|
95 |
+
demo.load(fn=app_fn, inputs=[n_samples, n_estimators, max_depth], outputs=[plot])
|
96 |
+
|
97 |
+
demo.launch()
|