Upload RandomForest_UI.py
Browse files- RandomForest_UI.py +88 -3
RandomForest_UI.py
CHANGED
@@ -1,8 +1,93 @@
|
|
|
|
1 |
import gradio as gr
|
|
|
|
|
2 |
|
3 |
class RandomForest_UI():
|
|
|
|
|
|
|
|
|
|
|
|
|
4 |
def get_interface(self) -> gr.Blocks:
|
5 |
-
with gr.Blocks() as interface:
|
6 |
-
gr.Markdown("
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
|
8 |
-
return interface
|
|
|
1 |
+
from RandomForest.RandomForestClass import My_RandomForest
|
2 |
import gradio as gr
|
3 |
+
import pandas as pd
|
4 |
+
import matplotlib.pyplot as plt
|
5 |
|
6 |
class RandomForest_UI():
|
7 |
+
def __init__(self):
|
8 |
+
self.rf_model = My_RandomForest()
|
9 |
+
self.rf_model.train_model("Male")
|
10 |
+
self.rf_model.train_model("Female")
|
11 |
+
self.rf_model.train_model("Unspecified")
|
12 |
+
|
13 |
def get_interface(self) -> gr.Blocks:
|
14 |
+
with gr.Blocks(theme=gr.themes.Soft()) as interface:
|
15 |
+
gr.Markdown("## Experience Level Prediction through Random Forest Classifier" )
|
16 |
+
gr.Markdown("")
|
17 |
+
gr.Markdown(
|
18 |
+
"Welcome to the **Experience Level Prediction** section. Here, you can determine an individual's experience level based on a set of input parameters."
|
19 |
+
)
|
20 |
+
gr.Markdown(
|
21 |
+
"The prediction output will be categorized as follows:"
|
22 |
+
"\n\n- **1**: Low experience level"
|
23 |
+
"\n- **2**: Medium experience level"
|
24 |
+
"\n- **3**: High experience level"
|
25 |
+
)
|
26 |
+
gr.Markdown(
|
27 |
+
"Provide details such as workout frequency, session duration, and water intake to make a prediction. "
|
28 |
+
"You may also specify gender as an additional parameter. "
|
29 |
+
"Generally, higher values for these inputs indicate a higher experience level."
|
30 |
+
)
|
31 |
+
|
32 |
+
|
33 |
+
with gr.Row():
|
34 |
+
workout_frequency = gr.Number(label="Workout Frequency (days/week)")
|
35 |
+
session_duration = gr.Number(label="Session Duration (hours)")
|
36 |
+
water_intake = gr.Number(label="Water Intake (liters)")
|
37 |
+
gender = gr.Radio(["Male", "Female", "Unspecified"], label="Gender")
|
38 |
+
|
39 |
+
predict_btn = gr.Button("Predict")
|
40 |
+
gr.Markdown(
|
41 |
+
"### Prediction Output"
|
42 |
+
"\nAfter entering the required inputs, the predicted experience level will be displayed along with the model's accuracy. "
|
43 |
+
"The prediction categorizes the experience level into one of three categories (1: Low, 2: Medium, 3: High) and provides an accuracy percentage to indicate the confidence of the model's output."
|
44 |
+
)
|
45 |
+
output = gr.Textbox(
|
46 |
+
label="Prediction",
|
47 |
+
interactive=False,
|
48 |
+
lines=2
|
49 |
+
)
|
50 |
+
gr.Markdown(
|
51 |
+
"### Feature Importance Plot"
|
52 |
+
"\nThe **Feature Importance Plot** provides insights into which input parameters contribute the most to determining the experience level. "
|
53 |
+
"This visualization highlights the factors that play a significant role in increasing the predicted experience level, helping you better understand the model's decision-making process."
|
54 |
+
)
|
55 |
+
plot_output = gr.Plot()
|
56 |
+
|
57 |
+
predict_btn.click(
|
58 |
+
fn=self.make_prediction_and_plot,
|
59 |
+
inputs=[workout_frequency, session_duration, water_intake, gender],
|
60 |
+
outputs=[output, plot_output]
|
61 |
+
)
|
62 |
+
|
63 |
+
return interface
|
64 |
+
|
65 |
+
def make_prediction_and_plot(self, workout_frequency, session_duration, water_intake, gender):
|
66 |
+
# Generate prediction
|
67 |
+
input_data = pd.DataFrame({
|
68 |
+
"Workout_Frequency (days/week)": [workout_frequency],
|
69 |
+
"Session_Duration (hours)": [session_duration],
|
70 |
+
"Water_Intake (liters)": [water_intake]
|
71 |
+
})
|
72 |
+
|
73 |
+
prediction = self.rf_model.predict(input_data, gender=gender)
|
74 |
+
accuracy = self.rf_model.accuracies[gender]
|
75 |
+
|
76 |
+
prediction_text = f"Predicted Experience Level: {prediction[0]} with an accuracy of {accuracy*100:.4f}%"
|
77 |
+
|
78 |
+
# Generate plot
|
79 |
+
model = self.rf_model.models[gender]
|
80 |
+
features = self.rf_model.selected_features[gender]
|
81 |
+
feature_importances = pd.Series(model.feature_importances_, index=features)
|
82 |
+
feature_importances = feature_importances.sort_values(ascending=False)
|
83 |
+
|
84 |
+
plt.figure(figsize=(10, 6))
|
85 |
+
feature_importances.plot(kind='bar')
|
86 |
+
plt.title(f"Feature Importances plot for {gender} Model")
|
87 |
+
plt.xlabel("Features")
|
88 |
+
plt.ylabel("Importance")
|
89 |
+
plt.tight_layout()
|
90 |
+
|
91 |
+
return prediction_text, plt
|
92 |
+
|
93 |
|
|