{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "a3901bba-d66d-4358-89a7-50dc4b3dd91e",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "a4713d33-c5a2-4f49-8aed-873069543bec",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead tr th {\n",
       "        text-align: left;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th>Unnamed: 0_level_0</th>\n",
       "      <th colspan=\"2\" halign=\"left\">原料特征</th>\n",
       "      <th colspan=\"2\" halign=\"left\">热处理条件</th>\n",
       "      <th colspan=\"3\" halign=\"left\">第一次热处理</th>\n",
       "      <th colspan=\"2\" halign=\"left\">第二次热处理</th>\n",
       "      <th>...</th>\n",
       "      <th>模板剂</th>\n",
       "      <th colspan=\"2\" halign=\"left\">活化剂</th>\n",
       "      <th>混合方式</th>\n",
       "      <th colspan=\"5\" halign=\"left\">碳材料结构特征</th>\n",
       "      <th>参考文献</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th>原料</th>\n",
       "      <th>是否预处理</th>\n",
       "      <th>软化点</th>\n",
       "      <th>热处理次数</th>\n",
       "      <th>是否是中温停留</th>\n",
       "      <th>温度</th>\n",
       "      <th>升温速率</th>\n",
       "      <th>保留时间</th>\n",
       "      <th>温度</th>\n",
       "      <th>升温速率·</th>\n",
       "      <th>...</th>\n",
       "      <th>与沥青比例</th>\n",
       "      <th>是否KOH活化</th>\n",
       "      <th>比例</th>\n",
       "      <th>混合方式</th>\n",
       "      <th>比表面积</th>\n",
       "      <th>总孔体积</th>\n",
       "      <th>微孔体积</th>\n",
       "      <th>平均孔径</th>\n",
       "      <th>平均孔径.1</th>\n",
       "      <th>Unnamed: 25_level_1</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>煤沥青</td>\n",
       "      <td>否</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1</td>\n",
       "      <td>否</td>\n",
       "      <td>500</td>\n",
       "      <td>5</td>\n",
       "      <td>2</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>...</td>\n",
       "      <td>1.0</td>\n",
       "      <td>是</td>\n",
       "      <td>1.0</td>\n",
       "      <td>溶剂</td>\n",
       "      <td>908.07</td>\n",
       "      <td>0.40</td>\n",
       "      <td>0.34</td>\n",
       "      <td>1.75</td>\n",
       "      <td>NaN</td>\n",
       "      <td>姜宇晨   硬模板法制备煤焦油沥青基多孔碳及其超电性能研究</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>煤沥青</td>\n",
       "      <td>否</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1</td>\n",
       "      <td>否</td>\n",
       "      <td>600</td>\n",
       "      <td>5</td>\n",
       "      <td>2</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>...</td>\n",
       "      <td>1.0</td>\n",
       "      <td>是</td>\n",
       "      <td>0.5</td>\n",
       "      <td>溶剂</td>\n",
       "      <td>953.95</td>\n",
       "      <td>0.66</td>\n",
       "      <td>0.35</td>\n",
       "      <td>2.76</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>煤沥青</td>\n",
       "      <td>否</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1</td>\n",
       "      <td>否</td>\n",
       "      <td>600</td>\n",
       "      <td>5</td>\n",
       "      <td>2</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>...</td>\n",
       "      <td>1.0</td>\n",
       "      <td>是</td>\n",
       "      <td>1.0</td>\n",
       "      <td>溶剂</td>\n",
       "      <td>1388.62</td>\n",
       "      <td>0.61</td>\n",
       "      <td>0.53</td>\n",
       "      <td>1.77</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>煤沥青</td>\n",
       "      <td>否</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1</td>\n",
       "      <td>否</td>\n",
       "      <td>600</td>\n",
       "      <td>5</td>\n",
       "      <td>2</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>...</td>\n",
       "      <td>1.0</td>\n",
       "      <td>是</td>\n",
       "      <td>2.0</td>\n",
       "      <td>溶剂</td>\n",
       "      <td>1444.63</td>\n",
       "      <td>0.59</td>\n",
       "      <td>0.55</td>\n",
       "      <td>1.62</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>煤沥青</td>\n",
       "      <td>否</td>\n",
       "      <td>NaN</td>\n",
       "      <td>2</td>\n",
       "      <td>否</td>\n",
       "      <td>500</td>\n",
       "      <td>5</td>\n",
       "      <td>2</td>\n",
       "      <td>600.0</td>\n",
       "      <td>5.0</td>\n",
       "      <td>...</td>\n",
       "      <td>1.0</td>\n",
       "      <td>是</td>\n",
       "      <td>1.0</td>\n",
       "      <td>溶剂</td>\n",
       "      <td>1020.99</td>\n",
       "      <td>0.45</td>\n",
       "      <td>0.35</td>\n",
       "      <td>1.77</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 26 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "  Unnamed: 0_level_0  原料特征     热处理条件         第一次热处理           第二次热处理        \\\n",
       "                  原料 是否预处理 软化点 热处理次数 是否是中温停留     温度 升温速率 保留时间     温度 升温速率·   \n",
       "0                煤沥青     否 NaN     1       否    500    5    2    NaN   NaN   \n",
       "1                煤沥青     否 NaN     1       否    600    5    2    NaN   NaN   \n",
       "2                煤沥青     否 NaN     1       否    600    5    2    NaN   NaN   \n",
       "3                煤沥青     否 NaN     1       否    600    5    2    NaN   NaN   \n",
       "4                煤沥青     否 NaN     2       否    500    5    2  600.0   5.0   \n",
       "\n",
       "   ...   模板剂     活化剂      混合方式  碳材料结构特征                           \\\n",
       "   ... 与沥青比例 是否KOH活化   比例 混合方式     比表面积  总孔体积  微孔体积  平均孔径 平均孔径.1   \n",
       "0  ...   1.0       是  1.0   溶剂   908.07  0.40  0.34  1.75    NaN   \n",
       "1  ...   1.0       是  0.5   溶剂   953.95  0.66  0.35  2.76    NaN   \n",
       "2  ...   1.0       是  1.0   溶剂  1388.62  0.61  0.53  1.77    NaN   \n",
       "3  ...   1.0       是  2.0   溶剂  1444.63  0.59  0.55  1.62    NaN   \n",
       "4  ...   1.0       是  1.0   溶剂  1020.99  0.45  0.35  1.77    NaN   \n",
       "\n",
       "                             参考文献  \n",
       "              Unnamed: 25_level_1  \n",
       "0  姜宇晨   硬模板法制备煤焦油沥青基多孔碳及其超电性能研究  \n",
       "1                             NaN  \n",
       "2                             NaN  \n",
       "3                             NaN  \n",
       "4                             NaN  \n",
       "\n",
       "[5 rows x 26 columns]"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data = pd.read_excel('./data/20240102/20231227.xlsx', header=[0, 1])\n",
    "data.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "8b113a99-8c56-455c-9ee8-3ac32a686e24",
   "metadata": {},
   "outputs": [],
   "source": [
    "data.columns = ['-'.join([y for y in x if 'Unnamed' not in y]) for x in data.columns]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "e134c799-b11c-46c8-898d-ac1d9e47e527",
   "metadata": {},
   "outputs": [],
   "source": [
    "ignore_cols = data.columns[:3].tolist() + data.columns[-2:].tolist()\n",
    "data.drop(columns=ignore_cols, inplace=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "359c9cc6-2694-46a6-9f18-6361e220542a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Index(['热处理条件-热处理次数', '热处理条件-是否是中温停留', '第一次热处理-温度', '第一次热处理-升温速率',\n",
       "       '第一次热处理-保留时间', '第二次热处理-温度', '第二次热处理-升温速率·', '第二次热处理-保留时间',\n",
       "       '共碳化-是否是共碳化物质', '共碳化-共碳化物质/沥青', '模板剂-与沥青比例', '活化剂-是否KOH活化', '活化剂-比例',\n",
       "       '混合方式-混合方式', '碳材料结构特征-比表面积', '碳材料结构特征-总孔体积', '碳材料结构特征-微孔体积',\n",
       "       '碳材料结构特征-平均孔径', '共碳化-种类_2-甲基咪唑', '共碳化-种类_三聚氰胺', '共碳化-种类_尿素',\n",
       "       '共碳化-种类_硫酸铵', '共碳化-种类_聚磷酸铵', '模板剂-模板剂制备方式_无', '模板剂-模板剂制备方式_溶液合成',\n",
       "       '模板剂-模板剂制备方式_热分解', '模板剂-模板剂制备方式_直接购买', '模板剂-模板剂制备方式_自己合成',\n",
       "       '模板剂-种类_Al2O3', '模板剂-种类_TiO2', '模板剂-种类_α-Fe2O3', '模板剂-种类_γ-Fe2O3',\n",
       "       '模板剂-种类_二氧化硅', '模板剂-种类_氢氧化镁', '模板剂-种类_氧化钙', '模板剂-种类_氧化锌', '模板剂-种类_氧化镁',\n",
       "       '模板剂-种类_氯化钠', '模板剂-种类_氯化钾', '模板剂-种类_碱式碳酸镁', '模板剂-种类_碳酸钙', '模板剂-种类_纤维素'],\n",
       "      dtype='object')"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.columns"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "6cc22ecb-1070-4e63-a496-efc838094958",
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.preprocessing import LabelEncoder\n",
    "encoder = LabelEncoder()\n",
    "for col in ['热处理条件-热处理次数', '热处理条件-是否是中温停留', '共碳化-是否是共碳化物质', '活化剂-是否KOH活化', '混合方式-混合方式']:\n",
    "    encoded_labels = encoder.fit_transform(data[col])\n",
    "    data[col] = encoded_labels"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "24f58281-9f13-49ef-b44d-81d0644d6976",
   "metadata": {},
   "outputs": [],
   "source": [
    "object_cols = ['共碳化-种类', '模板剂-模板剂制备方式', '模板剂-种类']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "3368163e-85a1-4487-8078-be51cb5fb560",
   "metadata": {},
   "outputs": [],
   "source": [
    "data = pd.get_dummies(data, columns=object_cols)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "92d5da6b-f714-4a78-9aa7-7cf9dff1d0a0",
   "metadata": {},
   "outputs": [],
   "source": [
    "out_cols = [x for x in data.columns if '碳材料结构特征' in x]\n",
    "feature_cols = [x for x in data.columns if x not in out_cols]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "e4946bd7-ae94-4981-82ed-66e2b496e035",
   "metadata": {},
   "outputs": [],
   "source": [
    "train_data = data.reset_index(drop=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "4109685a-4d5b-4c63-b4e2-eb9db3989d02",
   "metadata": {},
   "outputs": [],
   "source": [
    "import xgboost as xgb\n",
    "from sklearn.metrics import mean_absolute_error, mean_squared_error, mean_absolute_percentage_error, r2_score"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "2bbdcd34-16c1-43ba-b249-6c7d54db8ac2",
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.model_selection import KFold, train_test_split\n",
    "kf = KFold(n_splits=10, shuffle=True, random_state=42)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "42597842-1acb-4263-bdad-bfca7b11bcb5",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "94af2a3a-6f61-46bf-8cd4-2b7e0da8b2c4",
   "metadata": {},
   "outputs": [],
   "source": [
    "params_xgb = {\"objective\": 'reg:squarederror',\n",
    "             \"subsample\": 0.9,\n",
    "             \"max_depth\": 15,\n",
    "             \"eta\": 0.05,\n",
    "             \"gamma\": 0,\n",
    "             \"lambda\": 1,\n",
    "             \"alpha\": 0,\n",
    "             \"colsample_bytree\": 0.8,}\n",
    "num_boost_round = 1000"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "f17eadb3-4767-4eca-bbed-880bf9cbb7a3",
   "metadata": {},
   "outputs": [],
   "source": [
    "import matplotlib.pyplot as plt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "5bfcc8aa-f13c-4a7d-9d15-b79087e11017",
   "metadata": {},
   "outputs": [],
   "source": [
    "plt.rcParams[\"font.sans-serif\"] = [\"SimHei\"]  # 设置字体\n",
    "plt.rcParams[\"axes.unicode_minus\"] = False  # 正常显示负号"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "db4dbc2d-534e-4a7e-b45c-ea25ab269502",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 175672 characters]",
      "text/plain": [
       "<Figure size 1200x800 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 155564 characters]",
      "text/plain": [
       "<Figure size 1200x800 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 178672 characters]",
      "text/plain": [
       "<Figure size 1200x800 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 153936 characters]",
      "text/plain": [
       "<Figure size 1200x800 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "eva_total = list()\n",
    "index_list = list()\n",
    "eva_cols = ['MSE', 'RMSE', 'MAE', 'MAPE', 'R2']\n",
    "for col in out_cols:\n",
    "    eva_list = list()\n",
    "    train_data = train_data[~train_data[col].isna()].reset_index(drop=True)\n",
    "    cur_test = list()\n",
    "    cur_real = list()\n",
    "    for (train_index, test_index) in kf.split(train_data):\n",
    "        train = train_data.loc[train_index]\n",
    "        valid = train_data.loc[test_index]\n",
    "        X_train, Y_train = train[feature_cols], np.log1p(train[col])\n",
    "        X_valid, Y_valid = valid[feature_cols], np.log1p(valid[col])\n",
    "        dtrain = xgb.DMatrix(X_train, Y_train)\n",
    "        dvalid = xgb.DMatrix(X_valid, Y_valid)\n",
    "        watchlist = [(dvalid, 'eval')]\n",
    "        gb_model = xgb.train(params_xgb, dtrain, num_boost_round, evals=watchlist,\n",
    "                        early_stopping_rounds=50, verbose_eval=False)\n",
    "        y_pred = np.expm1(gb_model.predict(xgb.DMatrix(X_valid)))\n",
    "        y_true = np.expm1(Y_valid.values)\n",
    "        MSE = mean_squared_error(y_true, y_pred)\n",
    "        RMSE = np.sqrt(mean_squared_error(y_true, y_pred))\n",
    "        MAE = mean_absolute_error(y_true, y_pred)\n",
    "        MAPE = mean_absolute_percentage_error(y_true, y_pred)\n",
    "        R_2 = r2_score(y_true, y_pred)\n",
    "        cur_test.extend(y_pred[:7])\n",
    "        cur_real.extend(y_true[:7])\n",
    "        # print('MSE:', round(MSE, 4), end=', ')\n",
    "        # print('RMSE:', round(RMSE, 4), end=', ')\n",
    "        # print('MAE:', round(MAE, 4), end=', ')\n",
    "        # print('MAPE:', round(MAPE*100, 2), '%', end=', ')\n",
    "        # print('R_2:', round(R_2, 4))  #R方为负就说明拟合效果比平均值差\n",
    "        eva_list.append([MSE, RMSE, MAE, MAPE, R_2])\n",
    "    plt.figure(figsize=(12, 8))\n",
    "    plt.plot(range(len(cur_test)), cur_real, 'o-', label='real')\n",
    "    plt.plot(range(len(cur_test)), cur_test, '*-', label='pred')\n",
    "    plt.legend(loc='best')\n",
    "    plt.title(f'{col}')\n",
    "    plt.show()\n",
    "    eva_total.append(np.mean(eva_list, axis=0))\n",
    "    index_list.append(f\"{col}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "dcce8331-256f-4e22-8ac5-f07dca12f3cf",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>MSE</th>\n",
       "      <th>RMSE</th>\n",
       "      <th>MAE</th>\n",
       "      <th>MAPE</th>\n",
       "      <th>R2</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>碳材料结构特征-比表面积</th>\n",
       "      <td>303184.717382</td>\n",
       "      <td>544.657385</td>\n",
       "      <td>421.657610</td>\n",
       "      <td>0.847088</td>\n",
       "      <td>0.372696</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>碳材料结构特征-总孔体积</th>\n",
       "      <td>0.128721</td>\n",
       "      <td>0.354206</td>\n",
       "      <td>0.280979</td>\n",
       "      <td>0.900388</td>\n",
       "      <td>0.435381</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>碳材料结构特征-微孔体积</th>\n",
       "      <td>0.042627</td>\n",
       "      <td>0.205114</td>\n",
       "      <td>0.160595</td>\n",
       "      <td>3.522258</td>\n",
       "      <td>0.497659</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>碳材料结构特征-平均孔径</th>\n",
       "      <td>0.675843</td>\n",
       "      <td>0.763879</td>\n",
       "      <td>0.509585</td>\n",
       "      <td>0.172561</td>\n",
       "      <td>0.307433</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                        MSE        RMSE         MAE      MAPE        R2\n",
       "碳材料结构特征-比表面积  303184.717382  544.657385  421.657610  0.847088  0.372696\n",
       "碳材料结构特征-总孔体积       0.128721    0.354206    0.280979  0.900388  0.435381\n",
       "碳材料结构特征-微孔体积       0.042627    0.205114    0.160595  3.522258  0.497659\n",
       "碳材料结构特征-平均孔径       0.675843    0.763879    0.509585  0.172561  0.307433"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.DataFrame.from_records(eva_total, index=index_list, columns=eva_cols)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7ea3c810-754a-484b-b14e-e4fe571386b8",
   "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.8.16"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
