|
from RandomForest.RandomForestClass import My_RandomForest
|
|
import gradio as gr
|
|
import pandas as pd
|
|
import matplotlib.pyplot as plt
|
|
|
|
class RandomForest_UI():
|
|
def __init__(self):
|
|
self.rf_model = My_RandomForest()
|
|
self.rf_model.train_model("Male")
|
|
self.rf_model.train_model("Female")
|
|
self.rf_model.train_model("Unspecified")
|
|
|
|
def get_interface(self) -> gr.Blocks:
|
|
with gr.Blocks(theme=gr.themes.Soft()) as interface:
|
|
gr.Markdown("## Experience Level Prediction through Random Forest Classifier" )
|
|
gr.Markdown("")
|
|
gr.Markdown(
|
|
"Welcome to the **Experience Level Prediction** section. Here, you can determine an individual's experience level based on a set of input parameters."
|
|
)
|
|
gr.Markdown(
|
|
"The prediction output will be categorized as follows:"
|
|
"\n\n- **1**: Low experience level"
|
|
"\n- **2**: Medium experience level"
|
|
"\n- **3**: High experience level"
|
|
)
|
|
gr.Markdown(
|
|
"Provide details such as workout frequency, session duration, and water intake to make a prediction. "
|
|
"You may also specify gender as an additional parameter. "
|
|
"Generally, higher values for these inputs indicate a higher experience level."
|
|
)
|
|
|
|
|
|
with gr.Row():
|
|
workout_frequency = gr.Number(label="Workout Frequency (days/week)")
|
|
session_duration = gr.Number(label="Session Duration (hours)")
|
|
water_intake = gr.Number(label="Water Intake (liters)")
|
|
gender = gr.Radio(["Male", "Female", "Unspecified"], label="Gender")
|
|
|
|
predict_btn = gr.Button("Predict")
|
|
gr.Markdown(
|
|
"### Prediction Output"
|
|
"\nAfter entering the required inputs, the predicted experience level will be displayed along with the model's accuracy. "
|
|
"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."
|
|
)
|
|
output = gr.Textbox(
|
|
label="Prediction",
|
|
interactive=False,
|
|
lines=2
|
|
)
|
|
gr.Markdown(
|
|
"### Feature Importance Plot"
|
|
"\nThe **Feature Importance Plot** provides insights into which input parameters contribute the most to determining the experience level. "
|
|
"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."
|
|
)
|
|
plot_output = gr.Plot()
|
|
|
|
predict_btn.click(
|
|
fn=self.make_prediction_and_plot,
|
|
inputs=[workout_frequency, session_duration, water_intake, gender],
|
|
outputs=[output, plot_output]
|
|
)
|
|
|
|
return interface
|
|
|
|
def make_prediction_and_plot(self, workout_frequency, session_duration, water_intake, gender):
|
|
|
|
input_data = pd.DataFrame({
|
|
"Workout_Frequency (days/week)": [workout_frequency],
|
|
"Session_Duration (hours)": [session_duration],
|
|
"Water_Intake (liters)": [water_intake]
|
|
})
|
|
|
|
prediction = self.rf_model.predict(input_data, gender=gender)
|
|
accuracy = self.rf_model.accuracies[gender]
|
|
|
|
prediction_text = f"Predicted Experience Level: {prediction[0]} with an accuracy of {accuracy*100:.4f}%"
|
|
|
|
|
|
model = self.rf_model.models[gender]
|
|
features = self.rf_model.selected_features[gender]
|
|
feature_importances = pd.Series(model.feature_importances_, index=features)
|
|
feature_importances = feature_importances.sort_values(ascending=False)
|
|
|
|
plt.figure(figsize=(10, 6))
|
|
feature_importances.plot(kind='bar')
|
|
plt.title(f"Feature Importances plot for {gender} Model")
|
|
plt.xlabel("Features")
|
|
plt.ylabel("Importance")
|
|
plt.tight_layout()
|
|
|
|
return prediction_text, plt
|
|
|
|
|
|
|