Upload with huggingface_hub
Browse files- DESCRIPTION.md +1 -0
- README.md +1 -1
- app.py +0 -16
DESCRIPTION.md
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
This demo takes in 12 inputs from the user in dropdowns and sliders and predicts income. It also has a separate button for explaining the prediction.
|
README.md
CHANGED
@@ -1,7 +1,7 @@
|
|
1 |
|
2 |
---
|
3 |
title: xgboost-income-prediction-with-explainability
|
4 |
-
emoji:
|
5 |
colorFrom: indigo
|
6 |
colorTo: indigo
|
7 |
sdk: gradio
|
|
|
1 |
|
2 |
---
|
3 |
title: xgboost-income-prediction-with-explainability
|
4 |
+
emoji: 🔥
|
5 |
colorFrom: indigo
|
6 |
colorTo: indigo
|
7 |
sdk: gradio
|
app.py
CHANGED
@@ -1,6 +1,3 @@
|
|
1 |
-
# URL: https://huggingface.co/spaces/gradio/xgboost-income-prediction-with-explainability
|
2 |
-
# DESCRIPTION: This demo takes in 12 inputs from the user in dropdowns and sliders and predicts income. It also has a separate button for explaining the prediction.
|
3 |
-
# imports
|
4 |
import gradio as gr
|
5 |
import random
|
6 |
import matplotlib
|
@@ -11,7 +8,6 @@ import xgboost as xgb
|
|
11 |
from datasets import load_dataset
|
12 |
|
13 |
|
14 |
-
# loading the model and setting up
|
15 |
matplotlib.use("Agg")
|
16 |
dataset = load_dataset("scikit-learn/adult-census-income")
|
17 |
X_train = dataset["train"].to_pandas()
|
@@ -33,8 +29,6 @@ data = xgb.DMatrix(X_train, label=y_train, enable_categorical=True)
|
|
33 |
model = xgb.train(params={"objective": "binary:logistic"}, dtrain=data)
|
34 |
explainer = shap.TreeExplainer(model)
|
35 |
|
36 |
-
# defining the two core fns
|
37 |
-
|
38 |
def predict(*args):
|
39 |
df = pd.DataFrame([args], columns=X_train.columns)
|
40 |
df = df.astype({col: "category" for col in categorical_columns})
|
@@ -65,17 +59,12 @@ unique_occupation = sorted(X_train["occupation"].unique())
|
|
65 |
unique_sex = sorted(X_train["sex"].unique())
|
66 |
unique_country = sorted(X_train["native.country"].unique())
|
67 |
|
68 |
-
# starting the block
|
69 |
-
|
70 |
with gr.Blocks() as demo:
|
71 |
-
# defining text on the page
|
72 |
gr.Markdown("""
|
73 |
**Income Classification with XGBoost 💰**: This demo uses an XGBoost classifier predicts income based on demographic factors, along with Shapley value-based *explanations*. The [source code for this Gradio demo is here](https://huggingface.co/spaces/gradio/xgboost-income-prediction-with-explainability/blob/main/app.py).
|
74 |
""")
|
75 |
-
# defining the layout
|
76 |
with gr.Row():
|
77 |
with gr.Column():
|
78 |
-
# defining the inputs
|
79 |
age = gr.Slider(label="Age", minimum=17, maximum=90, step=1, randomize=True)
|
80 |
work_class = gr.Dropdown(
|
81 |
label="Workclass",
|
@@ -131,14 +120,11 @@ with gr.Blocks() as demo:
|
|
131 |
value=lambda: random.choice(unique_country),
|
132 |
)
|
133 |
with gr.Column():
|
134 |
-
# defining the outputs
|
135 |
label = gr.Label()
|
136 |
plot = gr.Plot()
|
137 |
with gr.Row():
|
138 |
-
# defining the buttons
|
139 |
predict_btn = gr.Button(value="Predict")
|
140 |
interpret_btn = gr.Button(value="Explain")
|
141 |
-
# defining the fn that will run when predict is clicked, what it will get as inputs, and which output it will update
|
142 |
predict_btn.click(
|
143 |
predict,
|
144 |
inputs=[
|
@@ -157,7 +143,6 @@ with gr.Blocks() as demo:
|
|
157 |
],
|
158 |
outputs=[label],
|
159 |
)
|
160 |
-
# defining the fn that will run when interpret is clicked, what it will get as inputs, and which output it will update
|
161 |
interpret_btn.click(
|
162 |
interpret,
|
163 |
inputs=[
|
@@ -177,5 +162,4 @@ with gr.Blocks() as demo:
|
|
177 |
outputs=[plot],
|
178 |
)
|
179 |
|
180 |
-
# launch
|
181 |
demo.launch()
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
import random
|
3 |
import matplotlib
|
|
|
8 |
from datasets import load_dataset
|
9 |
|
10 |
|
|
|
11 |
matplotlib.use("Agg")
|
12 |
dataset = load_dataset("scikit-learn/adult-census-income")
|
13 |
X_train = dataset["train"].to_pandas()
|
|
|
29 |
model = xgb.train(params={"objective": "binary:logistic"}, dtrain=data)
|
30 |
explainer = shap.TreeExplainer(model)
|
31 |
|
|
|
|
|
32 |
def predict(*args):
|
33 |
df = pd.DataFrame([args], columns=X_train.columns)
|
34 |
df = df.astype({col: "category" for col in categorical_columns})
|
|
|
59 |
unique_sex = sorted(X_train["sex"].unique())
|
60 |
unique_country = sorted(X_train["native.country"].unique())
|
61 |
|
|
|
|
|
62 |
with gr.Blocks() as demo:
|
|
|
63 |
gr.Markdown("""
|
64 |
**Income Classification with XGBoost 💰**: This demo uses an XGBoost classifier predicts income based on demographic factors, along with Shapley value-based *explanations*. The [source code for this Gradio demo is here](https://huggingface.co/spaces/gradio/xgboost-income-prediction-with-explainability/blob/main/app.py).
|
65 |
""")
|
|
|
66 |
with gr.Row():
|
67 |
with gr.Column():
|
|
|
68 |
age = gr.Slider(label="Age", minimum=17, maximum=90, step=1, randomize=True)
|
69 |
work_class = gr.Dropdown(
|
70 |
label="Workclass",
|
|
|
120 |
value=lambda: random.choice(unique_country),
|
121 |
)
|
122 |
with gr.Column():
|
|
|
123 |
label = gr.Label()
|
124 |
plot = gr.Plot()
|
125 |
with gr.Row():
|
|
|
126 |
predict_btn = gr.Button(value="Predict")
|
127 |
interpret_btn = gr.Button(value="Explain")
|
|
|
128 |
predict_btn.click(
|
129 |
predict,
|
130 |
inputs=[
|
|
|
143 |
],
|
144 |
outputs=[label],
|
145 |
)
|
|
|
146 |
interpret_btn.click(
|
147 |
interpret,
|
148 |
inputs=[
|
|
|
162 |
outputs=[plot],
|
163 |
)
|
164 |
|
|
|
165 |
demo.launch()
|