In [1]:
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.tree import DecisionTreeRegressor
import pickle
from sklearn.metrics import mean_squared_error, mean_absolute_error, r2_score
# 加载数据集
df = pd.read_csv('fitness analysis.csv')
# 显示前五行数据
print(df.head())
# 选择相关特征进行建模
X = df[['Your gender ', 'How important is exercise to you ?', 'How healthy do you consider yourself?']]
X = pd.get_dummies(X) # 将分类变量转为数值变量
# 设为目标变量
y = df['daily_steps']
# 将数据集划分为训练集和测试集(测试集占20%)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# 创建并训练决策树回归模型
dt_model = DecisionTreeRegressor(random_state=42)
# 训练决策树回归模型
dt_model.fit(X_train, y_train)
# 保存训练好的模型
with open('2.2.5_model.pkl', 'wb') as model_file:
pickle.dump(dt_model, model_file)
# 进行预测
y_pred = dt_model.predict(X_test)
# 将结果保存到文本文件中
results = pd.DataFrame({'实际值': y_test, '预测值': y_pred})
results_filename = '2.2.5_results.txt'
results.to_csv(results_filename, index=False, sep='\t')
# 将测试结果保存到报告文件中
report_filename = '2.2.5_report.txt'
with open(report_filename, 'w') as f:
f.write(f'均方误差: {mean_squared_error(y_test, y_pred)}\n')
f.write(f'平均绝对误差: {mean_absolute_error(y_test, y_pred)}\n')
f.write(f'决定系数: {r2_score(y_test, y_pred)}\n')
Timestamp Your name Your gender Your age \ 0 2019/07/03 11:48:07 PM GMT+5:30 Parkavi Female 19 to 25 1 2019/07/03 11:51:22 PM GMT+5:30 Nithilaa Female 19 to 25 2 2019/07/03 11:56:28 PM GMT+5:30 Karunya v Female 15 to 18 3 2019/07/04 5:43:35 AM GMT+5:30 Anusha Female 15 to 18 4 2019/07/04 5:44:29 AM GMT+5:30 Nikkitha Female 19 to 25 How important is exercise to you ? \ 0 2 1 4 2 3 3 4 4 3 How do you describe your current level of fitness ? \ 0 Good 1 Very good 2 Good 3 Good 4 Unfit How often do you exercise? \ 0 Never 1 Never 2 1 to 2 times a week 3 3 to 4 times a week 4 Never What barriers, if any, prevent you from exercising more regularly? (Please select all that apply) \ 0 I don't have enough time;I can't stay motivated 1 I don't have enough time;I'll become too tired 2 I can't stay motivated 3 I don't have enough time 4 I can't stay motivated What form(s) of exercise do you currently participate in ? (Please select all that apply) \ 0 I don't really exercise 1 Walking or jogging;Swimming 2 Walking or jogging 3 Walking or jogging;Gym;Lifting weights 4 I don't really exercise Do you exercise ___________ ? \ 0 I don't really exercise 1 With a group 2 Alone 3 Alone 4 I don't really exercise What time if the day do you prefer to exercise? \ 0 Early morning 1 Early morning 2 Early morning 3 Evening 4 Evening How long do you spend exercising per day ? \ 0 I don't really exercise 1 I don't really exercise 2 30 minutes 3 1 hour 4 I don't really exercise Would you say you eat a healthy balanced diet ? \ 0 Not always 1 Not always 2 Not always 3 Yes 4 Yes What prevents you from eating a healthy balanced diet, If any? (Please select all that apply) \ 0 Ease of access to fast food;Temptation and cra... 1 Ease of access to fast food;Temptation and cra... 2 Temptation and cravings 3 Temptation and cravings 4 Ease of access to fast food;Temptation and cra... How healthy do you consider yourself? \ 0 3 1 4 2 4 3 4 4 4 Have you ever recommended your friends to follow a fitness routine? \ 0 Yes 1 Yes 2 Yes 3 Yes 4 Yes Have you ever purchased a fitness equipment? \ 0 No 1 No 2 Yes 3 No 4 No What motivates you to exercise? (Please select all that applies ) \ 0 I'm sorry ... I'm not really interested in exe... 1 I want to be fit;I want to be flexible;I want ... 2 I want to be fit 3 I want to be fit;I want to lose weight 4 I want to be fit daily_steps 0 12270 1 5860 2 10390 3 10191 4 10734
In [ ]: