Initial commit
Browse files- app.py +75 -0
- requirements.txt +1 -0
app.py
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Demo is based on https://scikit-learn.org/stable/auto_examples/feature_selection/plot_rfe_digits.html
|
| 3 |
+
"""
|
| 4 |
+
from sklearn.svm import SVC
|
| 5 |
+
from sklearn.datasets import load_digits
|
| 6 |
+
from sklearn.feature_selection import RFE
|
| 7 |
+
import matplotlib.pyplot as plt
|
| 8 |
+
|
| 9 |
+
# Load the digits dataset
|
| 10 |
+
digits = load_digits()
|
| 11 |
+
X = digits.images.reshape((len(digits.images), -1))
|
| 12 |
+
y = digits.target
|
| 13 |
+
|
| 14 |
+
# Create the RFE object and rank each pixel
|
| 15 |
+
svc = SVC(kernel="linear", C=1)
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
def recursive_feature_elimination(n_features_to_select, step, esimator=svc):
|
| 19 |
+
# Plot the results
|
| 20 |
+
fig = plt.figure()
|
| 21 |
+
rfe = RFE(estimator=esimator, n_features_to_select=1, step=1)
|
| 22 |
+
# step : Number of feature to remove at each iteration, least important are removed
|
| 23 |
+
# n_features_to_select : Number of features to be selected after repeated elimination
|
| 24 |
+
rfe.fit(X, y)
|
| 25 |
+
ranking = rfe.ranking_.reshape(digits.images[0].shape)
|
| 26 |
+
|
| 27 |
+
# Plot pixel ranking
|
| 28 |
+
plt.matshow(ranking, cmap=plt.cm.Blues)
|
| 29 |
+
plt.colorbar()
|
| 30 |
+
plt.title("Ranking of pixels with RFE")
|
| 31 |
+
# plt.show()
|
| 32 |
+
return plt
|
| 33 |
+
|
| 34 |
+
|
| 35 |
+
import gradio as gr
|
| 36 |
+
|
| 37 |
+
title = " Illustration of Recursive feature elimination.🌲 "
|
| 38 |
+
|
| 39 |
+
with gr.Blocks(title=title) as demo:
|
| 40 |
+
gr.Markdown(f"# {title}")
|
| 41 |
+
gr.Markdown(
|
| 42 |
+
" This example the feature importnace by using Recursive feature elimination <br>"
|
| 43 |
+
" Dataset is load_digits() which is images of size 8 X 8 hand-written digits <br>"
|
| 44 |
+
" **Parameters** <br> <br> **Number of features to select** : Represents the features left at the end of feature selection process. <br>"
|
| 45 |
+
" **Step** : Number of feature to remove at each iteration, least important are removed. <br>"
|
| 46 |
+
)
|
| 47 |
+
|
| 48 |
+
gr.Markdown(
|
| 49 |
+
" Support Vector classifier is used as estimator to rank features. <br>"
|
| 50 |
+
)
|
| 51 |
+
|
| 52 |
+
gr.Markdown(
|
| 53 |
+
" **[Demo is based on sklearn docs](https://scikit-learn.org/stable/auto_examples/feature_selection/plot_rfe_digits.html)**"
|
| 54 |
+
)
|
| 55 |
+
with gr.Row():
|
| 56 |
+
n_features_to_select = gr.Slider(
|
| 57 |
+
minimum=0, maximum=20, step=1, value=1, label="Number of features to select"
|
| 58 |
+
)
|
| 59 |
+
step = gr.Slider(minimum=0, maximum=20, step=1, value=1, label="Step")
|
| 60 |
+
|
| 61 |
+
btn = gr.Button(value="Submit")
|
| 62 |
+
|
| 63 |
+
btn.click(
|
| 64 |
+
recursive_feature_elimination,
|
| 65 |
+
inputs=[n_features_to_select, step],
|
| 66 |
+
outputs=gr.Plot(
|
| 67 |
+
label="Recursive feature elimination of pixels in digit classification"
|
| 68 |
+
),
|
| 69 |
+
) #
|
| 70 |
+
|
| 71 |
+
gr.Markdown(
|
| 72 |
+
" Plot shows the importance of each pixel in the classification of the digits. <br>"
|
| 73 |
+
)
|
| 74 |
+
|
| 75 |
+
demo.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
scikit-learn==1.2.1
|