Mohammad Haizad commited on
Commit
d8e0581
1 Parent(s): d865696

initial commit

Browse files
Files changed (3) hide show
  1. README.md +2 -2
  2. app.py +57 -0
  3. requirements.txt +2 -0
README.md CHANGED
@@ -1,10 +1,10 @@
1
  ---
2
- title: Model Based And Sequential Feature Selection
3
  emoji: 🔥
4
  colorFrom: blue
5
  colorTo: gray
6
  sdk: gradio
7
- sdk_version: 3.24.1
8
  app_file: app.py
9
  pinned: false
10
  license: mit
 
1
  ---
2
+ title: Model-based And Sequential Feature Selection
3
  emoji: 🔥
4
  colorFrom: blue
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,57 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import numpy as np
3
+ from sklearn.datasets import load_diabetes
4
+ from sklearn.linear_model import RidgeCV
5
+ from sklearn.feature_selection import SelectFromModel
6
+ from time import time
7
+ from sklearn.feature_selection import SequentialFeatureSelector
8
+ import matplotlib
9
+ matplotlib.use("Agg")
10
+ import matplotlib.pyplot as plt
11
+
12
+ def select_features(method,num_features):
13
+ diabetes = load_diabetes()
14
+ X, y = diabetes.data, diabetes.target
15
+ ridge = RidgeCV(alphas=np.logspace(-6, 6, num=5)).fit(X, y)
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=threshold).fit(X, y)
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(
30
+ ridge, n_features_to_select=int(num_features), direction="forward"
31
+ ).fit(X, y)
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(
38
+ ridge, n_features_to_select=int(num_features), direction="backward"
39
+ ).fit(X, y)
40
+ toc_bwd = time()
41
+ selected_features = feature_names[sfs_backward.get_support()]
42
+ execution_time = toc_bwd - tic_bwd
43
+ return f"Selected the following features: {selected_features} in {execution_time:.3f} seconds"
44
+
45
+ title = "Selecting features with Sequential Feature Selection"
46
+ with gr.Blocks(title=title) as demo:
47
+ gr.Markdown(f"## {title}")
48
+ gr.Markdown("This app demonstrates feature selection techniques using model based selection and sequential feature selection. The app uses the diabetes dataset from sklearn")
49
+
50
+ method = gr.Radio(["model", "sfs-forward", "sfs-backward"], label="Method")
51
+ num_features = gr.Textbox(label="Number of features")
52
+ output = gr.Textbox(label="Output Box")
53
+ select_btn = gr.Button("Select")
54
+ select_btn.click(fn=select_features, inputs=[method,num_features], outputs=output)
55
+
56
+ demo.launch()
57
+
requirements.txt ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ scikit-learn==1.2.2
2
+ matplotlib==3.7.1