File size: 4,305 Bytes
ed96a49
a34aae9
ed96a49
 
a34aae9
 
ed96a49
 
 
 
 
 
a34aae9
ed96a49
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a34aae9
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
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):
        # Generate prediction
        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}%"

        # Generate plot
        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