{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "fbe643f3",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "from sklearn.model_selection import train_test_split\n",
    "from sklearn.tree import DecisionTreeRegressor\n",
    "import pickle\n",
    "from sklearn.metrics import mean_squared_error, mean_absolute_error, r2_score\n",
    "\n",
    "# 加载数据集\n",
    "df = __________\n",
    "\n",
    "# 显示前五行数据\n",
    "print(__________)\n",
    "\n",
    "# 选择相关特征进行建模\n",
    "X = df[['Your gender ', 'How important is exercise to you ?', 'How healthy do you consider yourself?']]\n",
    "X = __________(X)  # 将分类变量转为数值变量\n",
    "\n",
    "# 设置目标变量\n",
    "y = __________  \n",
    "\n",
    "# 将数据集划分为训练集和测试集（测试集占20%）\n",
    "X_train, X_test, y_train, y_test = __________(__________, random_state=42)\n",
    "\n",
    "# 创建并训练决策树回归模型\n",
    "__________ = __________(random_state=42)\n",
    "# 训练决策树回归模型\n",
    "__________\n",
    "\n",
    "# 保存训练好的模型\n",
    "with open('2.2.5_model.pkl', 'wb') as model_file:\n",
    "    pickle.__________\n",
    "\n",
    "# 进行预测\n",
    "y_pred = __________\n",
    "\n",
    "# 将结果保存到文本文件中\n",
    "results = pd.DataFrame({'实际值': y_test, '预测值': y_pred})\n",
    "results_filename = '2.2.5_results.txt'\n",
    "__________(__________, index=False, sep='\\t')  \n",
    "\n",
    "# 将测试结果保存到报告文件中\n",
    "report_filename = '2.2.5_report.txt'\n",
    "with open(__________) as f:\n",
    "    f.write(f'均方误差: {__________}\\n')\n",
    "    f.write(f'平均绝对误差: {__________}\\n')\n",
    "    f.write(f'决定系数: {__________}\\n')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8481e046",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
