-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
128 lines (102 loc) · 4.9 KB
/
app.py
File metadata and controls
128 lines (102 loc) · 4.9 KB
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import streamlit as st
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sn
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.metrics import mean_squared_error
from sklearn.linear_model import LinearRegression
from sklearn.ensemble import RandomForestRegressor
from sklearn import metrics
import time
import warnings
warnings.filterwarnings('ignore')
st.write("## Personal Fitness Tracker")
#st.image("", use_column_width=True)
st.write("In this WebApp you will be able to observe your predicted calories burned in your body. Pass your parameters such as `Age`, `Gender`, `BMI`, etc., into this WebApp and then you will see the predicted value of kilocalories burned.")
st.sidebar.header("User Input Parameters: ")
def user_input_features():
age = st.sidebar.slider("Age: ", 10, 100, 30)
bmi = st.sidebar.slider("BMI: ", 15, 40, 20)
duration = st.sidebar.slider("Duration (min): ", 0, 35, 15)
heart_rate = st.sidebar.slider("Heart Rate: ", 60, 130, 80)
body_temp = st.sidebar.slider("Body Temperature (C): ", 36, 42, 38)
gender_button = st.sidebar.radio("Gender: ", ("Male", "Female"))
gender = 1 if gender_button == "Male" else 0
# Use column names to match the training data
data_model = {
"Age": age,
"BMI": bmi,
"Duration": duration,
"Heart_Rate": heart_rate,
"Body_Temp": body_temp,
"Gender_male": gender # Gender is encoded as 1 for male, 0 for female
}
features = pd.DataFrame(data_model, index=[0])
return features
df = user_input_features()
st.write("---")
st.header("Your Parameters: ")
latest_iteration = st.empty()
bar = st.progress(0)
for i in range(100):
bar.progress(i + 1)
time.sleep(0.01)
st.write(df)
# Load and preprocess data
calories = pd.read_csv("calories.csv")
exercise = pd.read_csv("exercise.csv")
exercise_df = exercise.merge(calories, on="User_ID")
exercise_df.drop(columns="User_ID", inplace=True)
exercise_train_data, exercise_test_data = train_test_split(exercise_df, test_size=0.2, random_state=1)
# Add BMI column to both training and test sets
for data in [exercise_train_data, exercise_test_data]:
data["BMI"] = data["Weight"] / ((data["Height"] / 100) ** 2)
data["BMI"] = round(data["BMI"], 2)
# Prepare the training and testing sets
exercise_train_data = exercise_train_data[["Gender", "Age", "BMI", "Duration", "Heart_Rate", "Body_Temp", "Calories"]]
exercise_test_data = exercise_test_data[["Gender", "Age", "BMI", "Duration", "Heart_Rate", "Body_Temp", "Calories"]]
exercise_train_data = pd.get_dummies(exercise_train_data, drop_first=True)
exercise_test_data = pd.get_dummies(exercise_test_data, drop_first=True)
# Separate features and labels
X_train = exercise_train_data.drop("Calories", axis=1)
y_train = exercise_train_data["Calories"]
X_test = exercise_test_data.drop("Calories", axis=1)
y_test = exercise_test_data["Calories"]
# Train the model
random_reg = RandomForestRegressor(n_estimators=1000, max_features=3, max_depth=6)
random_reg.fit(X_train, y_train)
# Align prediction data columns with training data
df = df.reindex(columns=X_train.columns, fill_value=0)
# Make prediction
prediction = random_reg.predict(df)
st.write("---")
st.header("Prediction: ")
latest_iteration = st.empty()
bar = st.progress(0)
for i in range(100):
bar.progress(i + 1)
time.sleep(0.01)
st.write(f"{round(prediction[0], 2)} **kilocalories**")
st.write("---")
st.header("Similar Results: ")
latest_iteration = st.empty()
bar = st.progress(0)
for i in range(100):
bar.progress(i + 1)
time.sleep(0.01)
# Find similar results based on predicted calories
calorie_range = [prediction[0] - 10, prediction[0] + 10]
similar_data = exercise_df[(exercise_df["Calories"] >= calorie_range[0]) & (exercise_df["Calories"] <= calorie_range[1])]
st.write(similar_data.sample(5))
st.write("---")
st.header("General Information: ")
# Boolean logic for age, duration, etc., compared to the user's input
boolean_age = (exercise_df["Age"] < df["Age"].values[0]).tolist()
boolean_duration = (exercise_df["Duration"] < df["Duration"].values[0]).tolist()
boolean_body_temp = (exercise_df["Body_Temp"] < df["Body_Temp"].values[0]).tolist()
boolean_heart_rate = (exercise_df["Heart_Rate"] < df["Heart_Rate"].values[0]).tolist()
st.write("You are older than", round(sum(boolean_age) / len(boolean_age), 2) * 100, "% of other people.")
st.write("Your exercise duration is higher than", round(sum(boolean_duration) / len(boolean_duration), 2) * 100, "% of other people.")
st.write("You have a higher heart rate than", round(sum(boolean_heart_rate) / len(boolean_heart_rate), 2) * 100, "% of other people during exercise.")
st.write("You have a higher body temperature than", round(sum(boolean_body_temp) / len(boolean_body_temp), 2) * 100, "% of other people during exercise.")