Spaces:
Build error
Build error
Add feature importance plot for model based selection and remove button
Browse files
app.py
CHANGED
|
@@ -16,14 +16,18 @@ def select_features(method,num_features):
|
|
| 16 |
feature_names = np.array(diabetes.feature_names)
|
| 17 |
if method == 'model':
|
| 18 |
importance = np.abs(ridge.coef_)
|
| 19 |
-
threshold = np.sort(importance)[-3] + 0.01
|
| 20 |
tic = time()
|
| 21 |
-
sfm = SelectFromModel(ridge, threshold=
|
| 22 |
toc = time()
|
| 23 |
selected_features = feature_names[sfm.get_support()]
|
| 24 |
if int(num_features) < len(selected_features):
|
| 25 |
selected_features = selected_features[:int(num_features)]
|
| 26 |
execution_time = toc - tic
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
elif method == 'sfs-forward':
|
| 28 |
tic_fwd = time()
|
| 29 |
sfs_forward = SequentialFeatureSelector(
|
|
@@ -32,6 +36,8 @@ def select_features(method,num_features):
|
|
| 32 |
toc_fwd = time()
|
| 33 |
selected_features = feature_names[sfs_forward.get_support()]
|
| 34 |
execution_time = toc_fwd - tic_fwd
|
|
|
|
|
|
|
| 35 |
elif method == 'sfs-backward':
|
| 36 |
tic_bwd = time()
|
| 37 |
sfs_backward = SequentialFeatureSelector(
|
|
@@ -40,7 +46,10 @@ def select_features(method,num_features):
|
|
| 40 |
toc_bwd = time()
|
| 41 |
selected_features = feature_names[sfs_backward.get_support()]
|
| 42 |
execution_time = toc_bwd - tic_bwd
|
| 43 |
-
|
|
|
|
|
|
|
|
|
|
| 44 |
|
| 45 |
title = "Selecting features with Sequential Feature Selection"
|
| 46 |
with gr.Blocks(title=title) as demo:
|
|
@@ -68,9 +77,9 @@ with gr.Blocks(title=title) as demo:
|
|
| 68 |
|
| 69 |
method = gr.Radio(["model", "sfs-forward", "sfs-backward"], label="Method")
|
| 70 |
num_features = gr.Slider(minimum=2, maximum=10, step=1, label = "Number of features")
|
| 71 |
-
output = gr.Textbox(label="
|
| 72 |
-
|
| 73 |
-
|
| 74 |
|
| 75 |
demo.launch()
|
| 76 |
|
|
|
|
| 16 |
feature_names = np.array(diabetes.feature_names)
|
| 17 |
if method == 'model':
|
| 18 |
importance = np.abs(ridge.coef_)
|
|
|
|
| 19 |
tic = time()
|
| 20 |
+
sfm = SelectFromModel(ridge, threshold=-np.inf,max_features=num_features).fit(X, y)
|
| 21 |
toc = time()
|
| 22 |
selected_features = feature_names[sfm.get_support()]
|
| 23 |
if int(num_features) < len(selected_features):
|
| 24 |
selected_features = selected_features[:int(num_features)]
|
| 25 |
execution_time = toc - tic
|
| 26 |
+
fig, ax = plt.subplots()
|
| 27 |
+
ax.bar(height=importance, x=feature_names)
|
| 28 |
+
ax.set_title("Feature importances via coefficients")
|
| 29 |
+
ax.set_ylabel("Importance coefficient")
|
| 30 |
+
ax.set_xlabel("Features")
|
| 31 |
elif method == 'sfs-forward':
|
| 32 |
tic_fwd = time()
|
| 33 |
sfs_forward = SequentialFeatureSelector(
|
|
|
|
| 36 |
toc_fwd = time()
|
| 37 |
selected_features = feature_names[sfs_forward.get_support()]
|
| 38 |
execution_time = toc_fwd - tic_fwd
|
| 39 |
+
importance = np.abs(sfs_forward.get_params()['estimator'].coef_)
|
| 40 |
+
fig = None
|
| 41 |
elif method == 'sfs-backward':
|
| 42 |
tic_bwd = time()
|
| 43 |
sfs_backward = SequentialFeatureSelector(
|
|
|
|
| 46 |
toc_bwd = time()
|
| 47 |
selected_features = feature_names[sfs_backward.get_support()]
|
| 48 |
execution_time = toc_bwd - tic_bwd
|
| 49 |
+
importance = np.abs(sfs_backward.get_params()['estimator'].coef_)
|
| 50 |
+
fig = None
|
| 51 |
+
|
| 52 |
+
return f"Selected the following features: {', '.join(selected_features)} in {execution_time:.3f} seconds", fig
|
| 53 |
|
| 54 |
title = "Selecting features with Sequential Feature Selection"
|
| 55 |
with gr.Blocks(title=title) as demo:
|
|
|
|
| 77 |
|
| 78 |
method = gr.Radio(["model", "sfs-forward", "sfs-backward"], label="Method")
|
| 79 |
num_features = gr.Slider(minimum=2, maximum=10, step=1, label = "Number of features")
|
| 80 |
+
output = gr.Textbox(label="Selected features")
|
| 81 |
+
plot = gr.Plot(label="Feature importance plot")
|
| 82 |
+
num_features.change(fn=select_features, inputs=[method,num_features], outputs=[output,plot])
|
| 83 |
|
| 84 |
demo.launch()
|
| 85 |
|