{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "6b84fefd-5936-4da4-ab6b-5b944329ad1d", "metadata": {}, "outputs": [], "source": [ "import os\n", "os.environ['CUDA_DEVICE_ORDER'] = 'PCB_BUS_ID'\n", "os.environ['CUDA_VISIBLE_DEVICES'] = '0, 1'" ] }, { "cell_type": "code", "execution_count": 2, "id": "9cf130e3-62ef-46e0-bbdc-b13d9d29318d", "metadata": {}, "outputs": [], "source": [ "import pandas as pd\n", "import numpy as np\n", "from sklearn.model_selection import train_test_split\n", "import matplotlib.pyplot as plt\n", "#新增加的两行\n", "from pylab import mpl\n", "# 设置显示中文字体\n", "mpl.rcParams[\"font.sans-serif\"] = [\"SimHei\"]\n", "\n", "mpl.rcParams[\"axes.unicode_minus\"] = False" ] }, { "cell_type": "code", "execution_count": 3, "id": "752381a5-0aeb-4c54-bc48-f9c3f8fc5d17", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
碳源共碳化物质共碳化物/煤沥青加热次数是否有碳化过程模板剂种类模板剂比例KOH与煤沥青比例活化温度升温速率活化时间混合方式比表面积总孔体积微孔体积平均孔径
0煤沥青0.01自制氧化钙1.01.050052.0溶剂908.070.400.341.75
1煤沥青0.01自制氧化钙1.00.560052.0溶剂953.950.660.352.76
2煤沥青0.01自制氧化钙1.01.060052.0溶剂1388.620.610.531.77
3煤沥青0.01自制氧化钙1.02.060052.0溶剂1444.630.590.551.62
4煤沥青0.02自制碱式碳酸镁1.01.060052.0溶剂1020.990.450.351.77
\n", "
" ], "text/plain": [ " 碳源 共碳化物质 共碳化物/煤沥青 加热次数 是否有碳化过程 模板剂种类 模板剂比例 KOH与煤沥青比例 活化温度 升温速率 \\\n", "0 煤沥青 无 0.0 1 否 自制氧化钙 1.0 1.0 500 5 \n", "1 煤沥青 无 0.0 1 否 自制氧化钙 1.0 0.5 600 5 \n", "2 煤沥青 无 0.0 1 否 自制氧化钙 1.0 1.0 600 5 \n", "3 煤沥青 无 0.0 1 否 自制氧化钙 1.0 2.0 600 5 \n", "4 煤沥青 无 0.0 2 是 自制碱式碳酸镁 1.0 1.0 600 5 \n", "\n", " 活化时间 混合方式 比表面积 总孔体积 微孔体积 平均孔径 \n", "0 2.0 溶剂 908.07 0.40 0.34 1.75 \n", "1 2.0 溶剂 953.95 0.66 0.35 2.76 \n", "2 2.0 溶剂 1388.62 0.61 0.53 1.77 \n", "3 2.0 溶剂 1444.63 0.59 0.55 1.62 \n", "4 2.0 溶剂 1020.99 0.45 0.35 1.77 " ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data = pd.read_excel('./data/20240123/煤沥青数据.xlsx')\n", "data.head()" ] }, { "cell_type": "code", "execution_count": 4, "id": "b016e6bd-b4de-4a6e-a3d5-2fc544042eb8", "metadata": {}, "outputs": [], "source": [ "data.drop(columns=['碳源'], inplace=True)" ] }, { "cell_type": "code", "execution_count": 5, "id": "d042e811-9548-480f-8a0b-27b72454fe43", "metadata": {}, "outputs": [], "source": [ "object_cols = ['共碳化物质', '是否有碳化过程', '模板剂种类', '混合方式']" ] }, { "cell_type": "code", "execution_count": 6, "id": "4ccf1708-e9cd-49d5-bdf0-27f6fdb60e1c", "metadata": {}, "outputs": [], "source": [ "data_0102 = pd.get_dummies(data, columns=object_cols)" ] }, { "cell_type": "code", "execution_count": 7, "id": "04b177a7-2f02-4e23-8ea9-29f34cf3eafc", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['共碳化物/煤沥青',\n", " '加热次数',\n", " '模板剂比例',\n", " 'KOH与煤沥青比例',\n", " '活化温度',\n", " '升温速率',\n", " '活化时间',\n", " '共碳化物质_2-甲基咪唑',\n", " '共碳化物质_三聚氰胺',\n", " '共碳化物质_尿素',\n", " '共碳化物质_无',\n", " '共碳化物质_硫酸铵',\n", " '共碳化物质_聚磷酸铵',\n", " '是否有碳化过程_否',\n", " '是否有碳化过程_是',\n", " '模板剂种类_Al2O3',\n", " '模板剂种类_TiO2',\n", " '模板剂种类_α-Fe2O3',\n", " '模板剂种类_γ-Fe2O3',\n", " '模板剂种类_二氧化硅',\n", " '模板剂种类_无',\n", " '模板剂种类_氯化钾',\n", " '模板剂种类_纤维素',\n", " '模板剂种类_自制氢氧化镁',\n", " '模板剂种类_自制氧化钙',\n", " '模板剂种类_自制氧化锌',\n", " '模板剂种类_自制氧化镁',\n", " '模板剂种类_自制碱式碳酸镁',\n", " '模板剂种类_购买氢氧化镁',\n", " '模板剂种类_购买氧化钙',\n", " '模板剂种类_购买氧化锌',\n", " '模板剂种类_购买氧化镁',\n", " '模板剂种类_购买氯化钠',\n", " '模板剂种类_购买碳酸钙',\n", " '混合方式_溶剂',\n", " '混合方式_研磨']" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "out_cols = ['比表面积', '总孔体积', '微孔体积', '平均孔径']\n", "feature_cols = [x for x in data_0102.columns if x not in out_cols]\n", "feature_cols" ] }, { "cell_type": "code", "execution_count": 8, "id": "31169fbf-d78e-42f7-87f3-71ba3dd0979d", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['比表面积', '总孔体积', '微孔体积', '平均孔径']" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "out_cols" ] }, { "cell_type": "code", "execution_count": 9, "id": "535d37b6-b9de-4025-ac8f-62f5bdbe2451", "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "2024-01-23 12:21:49.081644: I tensorflow/core/util/port.cc:110] oneDNN custom operations are on. You may see slightly different numerical results due to floating-point round-off errors from different computation orders. To turn them off, set the environment variable `TF_ENABLE_ONEDNN_OPTS=0`.\n", "2024-01-23 12:21:49.083823: I tensorflow/tsl/cuda/cudart_stub.cc:28] Could not find cuda drivers on your machine, GPU will not be used.\n", "2024-01-23 12:21:49.125771: I tensorflow/tsl/cuda/cudart_stub.cc:28] Could not find cuda drivers on your machine, GPU will not be used.\n", "2024-01-23 12:21:49.126872: I tensorflow/core/platform/cpu_feature_guard.cc:182] This TensorFlow binary is optimized to use available CPU instructions in performance-critical operations.\n", "To enable the following instructions: AVX2 AVX512F AVX512_VNNI FMA, in other operations, rebuild TensorFlow with the appropriate compiler flags.\n", "2024-01-23 12:21:50.338510: W tensorflow/compiler/tf2tensorrt/utils/py_utils.cc:38] TF-TRT Warning: Could not find TensorRT\n" ] } ], "source": [ "import tensorflow as tf\n", "from tensorflow import keras\n", "from tensorflow.keras import layers\n", "import tensorflow.keras.backend as K" ] }, { "cell_type": "code", "execution_count": 10, "id": "790284a3-b9d3-4144-b481-38a7c3ecb4b9", "metadata": {}, "outputs": [], "source": [ "from tensorflow.keras import Model" ] }, { "cell_type": "code", "execution_count": 12, "id": "cd9a1ca1-d0ca-4cb5-9ef5-fd5d63576cd2", "metadata": {}, "outputs": [], "source": [ "from tensorflow.keras.initializers import Constant" ] }, { "cell_type": "code", "execution_count": 13, "id": "80f32155-e71f-4615-8d0c-01dfd04988fe", "metadata": {}, "outputs": [], "source": [ "def get_prediction_model():\n", " inputs = layers.Input(shape=(1, len(feature_cols)), name='input')\n", " x = layers.Conv1D(filters=64, kernel_size=1, activation='relu')(inputs)\n", " # x = layers.Dropout(rate=0.1)(x)\n", " lstm_out = layers.Bidirectional(layers.LSTM(units=64, return_sequences=True))(x)\n", " lstm_out = layers.Dense(128, activation='relu')(lstm_out)\n", " # transformer_block = TransformerBlock(128, num_heads, ff_dim, name='first_attn')\n", " # out = transformer_block(lstm_out)\n", " # out = layers.GlobalAveragePooling1D()(out)\n", " out = layers.Dropout(0.1)(lstm_out)\n", " out = layers.Dense(64, activation='relu')(out)\n", " bet = layers.Dense(1, activation='sigmoid', name='vad')(out)\n", " model = Model(inputs=[inputs], outputs=[bet])\n", " return model" ] }, { "cell_type": "code", "execution_count": 14, "id": "7a9915ee-0016-44e5-a6fb-5ee90532dc14", "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "2024-01-23 12:22:03.707721: E tensorflow/compiler/xla/stream_executor/cuda/cuda_driver.cc:268] failed call to cuInit: CUDA_ERROR_INVALID_DEVICE: invalid device ordinal\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Model: \"model\"\n", "_________________________________________________________________\n", " Layer (type) Output Shape Param # \n", "=================================================================\n", " input (InputLayer) [(None, 1, 36)] 0 \n", " \n", " conv1d (Conv1D) (None, 1, 64) 2368 \n", " \n", " bidirectional (Bidirection (None, 1, 128) 66048 \n", " al) \n", " \n", " dense (Dense) (None, 1, 128) 16512 \n", " \n", " dropout (Dropout) (None, 1, 128) 0 \n", " \n", " dense_1 (Dense) (None, 1, 64) 8256 \n", " \n", " vad (Dense) (None, 1, 1) 65 \n", " \n", "=================================================================\n", "Total params: 93249 (364.25 KB)\n", "Trainable params: 93249 (364.25 KB)\n", "Non-trainable params: 0 (0.00 Byte)\n", "_________________________________________________________________\n" ] } ], "source": [ "model = get_prediction_model()\n", "model.summary()" ] }, { "cell_type": "code", "execution_count": 15, "id": "372011ea-9876-41eb-a4e6-83ccd6c71559", "metadata": {}, "outputs": [], "source": [ "from tensorflow.python.keras.utils.vis_utils import plot_model" ] }, { "cell_type": "code", "execution_count": 16, "id": "965b1d6e-8b9f-4536-8205-06b314aeab51", "metadata": {}, "outputs": [], "source": [ "train_data = data_0102.copy()" ] }, { "cell_type": "code", "execution_count": 17, "id": "1eebdab3-1f88-48a1-b5e0-bc8787528c1b", "metadata": {}, "outputs": [], "source": [ "maxs = train_data.max()\n", "mins = train_data.min()\n", "for col in train_data.columns:\n", " if maxs[col] - mins[col] == 0:\n", " continue\n", " train_data[col] = (train_data[col] - mins[col]) / (maxs[col] - mins[col])" ] }, { "cell_type": "code", "execution_count": 18, "id": "7f27bd56-4f6b-4242-9f79-c7d6b3ee2f13", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
共碳化物/煤沥青加热次数模板剂比例KOH与煤沥青比例活化温度升温速率活化时间比表面积总孔体积微孔体积...模板剂种类_自制氧化镁模板剂种类_自制碱式碳酸镁模板剂种类_购买氢氧化镁模板剂种类_购买氧化钙模板剂种类_购买氧化锌模板剂种类_购买氧化镁模板剂种类_购买氯化钠模板剂种类_购买碳酸钙混合方式_溶剂混合方式_研磨
00.00.00.10.0666670.0000000.30.3333330.2734370.1376280.270767...0.00.00.00.00.00.00.00.01.00.0
10.00.00.10.0333330.1666670.30.3333330.2873450.2291450.278754...0.00.00.00.00.00.00.00.01.00.0
20.00.00.10.0666670.1666670.30.3333330.4191030.2115450.422524...0.00.00.00.00.00.00.00.01.00.0
30.00.00.10.1333330.1666670.30.3333330.4360810.2045050.438498...0.00.00.00.00.00.00.00.01.00.0
40.01.00.10.0666670.1666670.30.3333330.3076660.1552270.278754...0.01.00.00.00.00.00.00.01.00.0
..................................................................
1440.00.00.00.2666670.1666670.30.0000000.5923010.3312210.638179...0.00.00.00.00.00.00.00.00.01.0
1450.00.00.00.2666670.3333330.30.0000000.8435890.4720170.941693...0.00.00.00.00.00.00.00.00.01.0
1460.00.00.00.2666670.5000000.30.0000000.6826310.3769800.781949...0.00.00.00.00.00.00.00.00.01.0
1470.00.00.00.2000000.3333330.30.0000000.5695670.2925030.614217...0.00.00.00.00.00.00.00.00.01.0
1480.00.00.00.3333330.3333330.30.0000000.7769020.3945790.885783...0.00.00.00.00.00.00.00.00.01.0
\n", "

149 rows × 40 columns

\n", "
" ], "text/plain": [ " 共碳化物/煤沥青 加热次数 模板剂比例 KOH与煤沥青比例 活化温度 升温速率 活化时间 比表面积 \\\n", "0 0.0 0.0 0.1 0.066667 0.000000 0.3 0.333333 0.273437 \n", "1 0.0 0.0 0.1 0.033333 0.166667 0.3 0.333333 0.287345 \n", "2 0.0 0.0 0.1 0.066667 0.166667 0.3 0.333333 0.419103 \n", "3 0.0 0.0 0.1 0.133333 0.166667 0.3 0.333333 0.436081 \n", "4 0.0 1.0 0.1 0.066667 0.166667 0.3 0.333333 0.307666 \n", ".. ... ... ... ... ... ... ... ... \n", "144 0.0 0.0 0.0 0.266667 0.166667 0.3 0.000000 0.592301 \n", "145 0.0 0.0 0.0 0.266667 0.333333 0.3 0.000000 0.843589 \n", "146 0.0 0.0 0.0 0.266667 0.500000 0.3 0.000000 0.682631 \n", "147 0.0 0.0 0.0 0.200000 0.333333 0.3 0.000000 0.569567 \n", "148 0.0 0.0 0.0 0.333333 0.333333 0.3 0.000000 0.776902 \n", "\n", " 总孔体积 微孔体积 ... 模板剂种类_自制氧化镁 模板剂种类_自制碱式碳酸镁 模板剂种类_购买氢氧化镁 \\\n", "0 0.137628 0.270767 ... 0.0 0.0 0.0 \n", "1 0.229145 0.278754 ... 0.0 0.0 0.0 \n", "2 0.211545 0.422524 ... 0.0 0.0 0.0 \n", "3 0.204505 0.438498 ... 0.0 0.0 0.0 \n", "4 0.155227 0.278754 ... 0.0 1.0 0.0 \n", ".. ... ... ... ... ... ... \n", "144 0.331221 0.638179 ... 0.0 0.0 0.0 \n", "145 0.472017 0.941693 ... 0.0 0.0 0.0 \n", "146 0.376980 0.781949 ... 0.0 0.0 0.0 \n", "147 0.292503 0.614217 ... 0.0 0.0 0.0 \n", "148 0.394579 0.885783 ... 0.0 0.0 0.0 \n", "\n", " 模板剂种类_购买氧化钙 模板剂种类_购买氧化锌 模板剂种类_购买氧化镁 模板剂种类_购买氯化钠 模板剂种类_购买碳酸钙 混合方式_溶剂 \\\n", "0 0.0 0.0 0.0 0.0 0.0 1.0 \n", "1 0.0 0.0 0.0 0.0 0.0 1.0 \n", "2 0.0 0.0 0.0 0.0 0.0 1.0 \n", "3 0.0 0.0 0.0 0.0 0.0 1.0 \n", "4 0.0 0.0 0.0 0.0 0.0 1.0 \n", ".. ... ... ... ... ... ... \n", "144 0.0 0.0 0.0 0.0 0.0 0.0 \n", "145 0.0 0.0 0.0 0.0 0.0 0.0 \n", "146 0.0 0.0 0.0 0.0 0.0 0.0 \n", "147 0.0 0.0 0.0 0.0 0.0 0.0 \n", "148 0.0 0.0 0.0 0.0 0.0 0.0 \n", "\n", " 混合方式_研磨 \n", "0 0.0 \n", "1 0.0 \n", "2 0.0 \n", "3 0.0 \n", "4 0.0 \n", ".. ... \n", "144 1.0 \n", "145 1.0 \n", "146 1.0 \n", "147 1.0 \n", "148 1.0 \n", "\n", "[149 rows x 40 columns]" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "train_data" ] }, { "cell_type": "code", "execution_count": 19, "id": "baf45a3d-dc01-44fc-9f0b-456964ac2cdb", "metadata": {}, "outputs": [], "source": [ "from sklearn.model_selection import KFold, train_test_split" ] }, { "cell_type": "code", "execution_count": 20, "id": "70414dca-d785-4f70-9521-6e58221486be", "metadata": {}, "outputs": [], "source": [ "from tensorflow.keras import optimizers\n", "from tensorflow.python.keras.utils.vis_utils import plot_model\n", "from sklearn.metrics import mean_squared_error, mean_absolute_error, r2_score, mean_absolute_percentage_error" ] }, { "cell_type": "code", "execution_count": 21, "id": "7f985922-75d4-45f2-9603-4a38ca84f696", "metadata": {}, "outputs": [], "source": [ "from keras.callbacks import ReduceLROnPlateau\n", "reduce_lr = ReduceLROnPlateau(monitor='val_loss', patience=10, mode='auto')" ] }, { "cell_type": "code", "execution_count": 22, "id": "266cd0ae-5681-402b-a4f7-ef705e3ac0cb", "metadata": {}, "outputs": [], "source": [ "def print_eva(y_true, y_pred, tp):\n", " MSE = mean_squared_error(y_true, y_pred)\n", " RMSE = np.sqrt(MSE)\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", " print(f\"COL: {tp}, MSE: {format(MSE, '.2E')}\", end=',')\n", " print(f'RMSE: {round(RMSE, 3)}', end=',')\n", " print(f'MAPE: {round(MAPE * 100, 3)} %', end=',')\n", " print(f'MAE: {round(MAE, 3)}', end=',')\n", " print(f'R_2: {round(R_2, 3)}')\n", " return [MSE, RMSE, MAE, MAPE, R_2]" ] }, { "cell_type": "code", "execution_count": 23, "id": "a1e4e900-b97d-4c52-88ad-439b80866f6b", "metadata": {}, "outputs": [], "source": [ "from keras.losses import mean_squared_error" ] }, { "cell_type": "code", "execution_count": 30, "id": "c47baf5e-8557-46d7-b67d-85530baf1af0", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "比表面积\n", "1/1 [==============================] - 1s 583ms/step\n", "COL: 比表面积, MSE: 5.79E+05,RMSE: 761.2230224609375,MAPE: 253.027 %,MAE: 653.417,R_2: 0.086\n" ] }, { "ename": "KeyboardInterrupt", "evalue": "", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mKeyboardInterrupt\u001b[0m Traceback (most recent call last)", "Cell \u001b[0;32mIn[30], line 17\u001b[0m\n\u001b[1;32m 15\u001b[0m prediction_model \u001b[38;5;241m=\u001b[39m get_prediction_model()\n\u001b[1;32m 16\u001b[0m prediction_model\u001b[38;5;241m.\u001b[39mcompile(optimizer\u001b[38;5;241m=\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124madam\u001b[39m\u001b[38;5;124m'\u001b[39m, loss\u001b[38;5;241m=\u001b[39mmean_squared_error)\n\u001b[0;32m---> 17\u001b[0m hist \u001b[38;5;241m=\u001b[39m \u001b[43mprediction_model\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mfit\u001b[49m\u001b[43m(\u001b[49m\u001b[43mX\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mY\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mepochs\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;241;43m120\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mbatch_size\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;241;43m8\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mverbose\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;241;43m0\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\n\u001b[1;32m 18\u001b[0m \u001b[43m \u001b[49m\u001b[43mvalidation_data\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43m(\u001b[49m\u001b[43mX_valid\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mY_valid\u001b[49m\u001b[43m)\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 19\u001b[0m \u001b[43m \u001b[49m\u001b[43mcallbacks\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43m[\u001b[49m\u001b[43mreduce_lr\u001b[49m\u001b[43m]\u001b[49m\n\u001b[1;32m 20\u001b[0m \u001b[43m \u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 21\u001b[0m rst \u001b[38;5;241m=\u001b[39m prediction_model\u001b[38;5;241m.\u001b[39mpredict(X_valid)\n\u001b[1;32m 22\u001b[0m pred_rst \u001b[38;5;241m=\u001b[39m rst \u001b[38;5;241m*\u001b[39m (maxs[pred_col] \u001b[38;5;241m-\u001b[39m mins[pred_col]) \u001b[38;5;241m+\u001b[39m mins[pred_col]\n", "File \u001b[0;32m~/miniconda3/envs/python38/lib/python3.8/site-packages/keras/src/utils/traceback_utils.py:65\u001b[0m, in \u001b[0;36mfilter_traceback..error_handler\u001b[0;34m(*args, **kwargs)\u001b[0m\n\u001b[1;32m 63\u001b[0m filtered_tb \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;01mNone\u001b[39;00m\n\u001b[1;32m 64\u001b[0m \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[0;32m---> 65\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43mfn\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43margs\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43mkwargs\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 66\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m \u001b[38;5;167;01mException\u001b[39;00m \u001b[38;5;28;01mas\u001b[39;00m e:\n\u001b[1;32m 67\u001b[0m filtered_tb \u001b[38;5;241m=\u001b[39m _process_traceback_frames(e\u001b[38;5;241m.\u001b[39m__traceback__)\n", "File \u001b[0;32m~/miniconda3/envs/python38/lib/python3.8/site-packages/keras/src/engine/training.py:1742\u001b[0m, in \u001b[0;36mModel.fit\u001b[0;34m(self, x, y, batch_size, epochs, verbose, callbacks, validation_split, validation_data, shuffle, class_weight, sample_weight, initial_epoch, steps_per_epoch, validation_steps, validation_batch_size, validation_freq, max_queue_size, workers, use_multiprocessing)\u001b[0m\n\u001b[1;32m 1734\u001b[0m \u001b[38;5;28;01mwith\u001b[39;00m tf\u001b[38;5;241m.\u001b[39mprofiler\u001b[38;5;241m.\u001b[39mexperimental\u001b[38;5;241m.\u001b[39mTrace(\n\u001b[1;32m 1735\u001b[0m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mtrain\u001b[39m\u001b[38;5;124m\"\u001b[39m,\n\u001b[1;32m 1736\u001b[0m epoch_num\u001b[38;5;241m=\u001b[39mepoch,\n\u001b[0;32m (...)\u001b[0m\n\u001b[1;32m 1739\u001b[0m _r\u001b[38;5;241m=\u001b[39m\u001b[38;5;241m1\u001b[39m,\n\u001b[1;32m 1740\u001b[0m ):\n\u001b[1;32m 1741\u001b[0m callbacks\u001b[38;5;241m.\u001b[39mon_train_batch_begin(step)\n\u001b[0;32m-> 1742\u001b[0m tmp_logs \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mtrain_function\u001b[49m\u001b[43m(\u001b[49m\u001b[43miterator\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 1743\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m data_handler\u001b[38;5;241m.\u001b[39mshould_sync:\n\u001b[1;32m 1744\u001b[0m context\u001b[38;5;241m.\u001b[39masync_wait()\n", "File \u001b[0;32m~/miniconda3/envs/python38/lib/python3.8/site-packages/tensorflow/python/util/traceback_utils.py:150\u001b[0m, in \u001b[0;36mfilter_traceback..error_handler\u001b[0;34m(*args, **kwargs)\u001b[0m\n\u001b[1;32m 148\u001b[0m filtered_tb \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;01mNone\u001b[39;00m\n\u001b[1;32m 149\u001b[0m \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[0;32m--> 150\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43mfn\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43margs\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43mkwargs\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 151\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m \u001b[38;5;167;01mException\u001b[39;00m \u001b[38;5;28;01mas\u001b[39;00m e:\n\u001b[1;32m 152\u001b[0m filtered_tb \u001b[38;5;241m=\u001b[39m _process_traceback_frames(e\u001b[38;5;241m.\u001b[39m__traceback__)\n", "File \u001b[0;32m~/miniconda3/envs/python38/lib/python3.8/site-packages/tensorflow/python/eager/polymorphic_function/polymorphic_function.py:825\u001b[0m, in \u001b[0;36mFunction.__call__\u001b[0;34m(self, *args, **kwds)\u001b[0m\n\u001b[1;32m 822\u001b[0m compiler \u001b[38;5;241m=\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mxla\u001b[39m\u001b[38;5;124m\"\u001b[39m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_jit_compile \u001b[38;5;28;01melse\u001b[39;00m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mnonXla\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m 824\u001b[0m \u001b[38;5;28;01mwith\u001b[39;00m OptionalXlaContext(\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_jit_compile):\n\u001b[0;32m--> 825\u001b[0m result \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_call\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43margs\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43mkwds\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 827\u001b[0m new_tracing_count \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mexperimental_get_tracing_count()\n\u001b[1;32m 828\u001b[0m without_tracing \u001b[38;5;241m=\u001b[39m (tracing_count \u001b[38;5;241m==\u001b[39m new_tracing_count)\n", "File \u001b[0;32m~/miniconda3/envs/python38/lib/python3.8/site-packages/tensorflow/python/eager/polymorphic_function/polymorphic_function.py:857\u001b[0m, in \u001b[0;36mFunction._call\u001b[0;34m(self, *args, **kwds)\u001b[0m\n\u001b[1;32m 854\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_lock\u001b[38;5;241m.\u001b[39mrelease()\n\u001b[1;32m 855\u001b[0m \u001b[38;5;66;03m# In this case we have created variables on the first call, so we run the\u001b[39;00m\n\u001b[1;32m 856\u001b[0m \u001b[38;5;66;03m# defunned version which is guaranteed to never create variables.\u001b[39;00m\n\u001b[0;32m--> 857\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_no_variable_creation_fn\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43margs\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43mkwds\u001b[49m\u001b[43m)\u001b[49m \u001b[38;5;66;03m# pylint: disable=not-callable\u001b[39;00m\n\u001b[1;32m 858\u001b[0m \u001b[38;5;28;01melif\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_variable_creation_fn \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m:\n\u001b[1;32m 859\u001b[0m \u001b[38;5;66;03m# Release the lock early so that multiple threads can perform the call\u001b[39;00m\n\u001b[1;32m 860\u001b[0m \u001b[38;5;66;03m# in parallel.\u001b[39;00m\n\u001b[1;32m 861\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_lock\u001b[38;5;241m.\u001b[39mrelease()\n", "File \u001b[0;32m~/miniconda3/envs/python38/lib/python3.8/site-packages/tensorflow/python/eager/polymorphic_function/tracing_compiler.py:148\u001b[0m, in \u001b[0;36mTracingCompiler.__call__\u001b[0;34m(self, *args, **kwargs)\u001b[0m\n\u001b[1;32m 145\u001b[0m \u001b[38;5;28;01mwith\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_lock:\n\u001b[1;32m 146\u001b[0m (concrete_function,\n\u001b[1;32m 147\u001b[0m filtered_flat_args) \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_maybe_define_function(args, kwargs)\n\u001b[0;32m--> 148\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43mconcrete_function\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_call_flat\u001b[49m\u001b[43m(\u001b[49m\n\u001b[1;32m 149\u001b[0m \u001b[43m \u001b[49m\u001b[43mfiltered_flat_args\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mcaptured_inputs\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mconcrete_function\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mcaptured_inputs\u001b[49m\u001b[43m)\u001b[49m\n", "File \u001b[0;32m~/miniconda3/envs/python38/lib/python3.8/site-packages/tensorflow/python/eager/polymorphic_function/monomorphic_function.py:1349\u001b[0m, in \u001b[0;36mConcreteFunction._call_flat\u001b[0;34m(self, args, captured_inputs)\u001b[0m\n\u001b[1;32m 1345\u001b[0m possible_gradient_type \u001b[38;5;241m=\u001b[39m gradients_util\u001b[38;5;241m.\u001b[39mPossibleTapeGradientTypes(args)\n\u001b[1;32m 1346\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m (possible_gradient_type \u001b[38;5;241m==\u001b[39m gradients_util\u001b[38;5;241m.\u001b[39mPOSSIBLE_GRADIENT_TYPES_NONE\n\u001b[1;32m 1347\u001b[0m \u001b[38;5;129;01mand\u001b[39;00m executing_eagerly):\n\u001b[1;32m 1348\u001b[0m \u001b[38;5;66;03m# No tape is watching; skip to running the function.\u001b[39;00m\n\u001b[0;32m-> 1349\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_build_call_outputs(\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_inference_function\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43margs\u001b[49m\u001b[43m)\u001b[49m)\n\u001b[1;32m 1350\u001b[0m forward_backward \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_select_forward_and_backward_functions(\n\u001b[1;32m 1351\u001b[0m args,\n\u001b[1;32m 1352\u001b[0m possible_gradient_type,\n\u001b[1;32m 1353\u001b[0m executing_eagerly)\n\u001b[1;32m 1354\u001b[0m forward_function, args_with_tangents \u001b[38;5;241m=\u001b[39m forward_backward\u001b[38;5;241m.\u001b[39mforward()\n", "File \u001b[0;32m~/miniconda3/envs/python38/lib/python3.8/site-packages/tensorflow/python/eager/polymorphic_function/atomic_function.py:196\u001b[0m, in \u001b[0;36mAtomicFunction.__call__\u001b[0;34m(self, *args)\u001b[0m\n\u001b[1;32m 194\u001b[0m \u001b[38;5;28;01mwith\u001b[39;00m record\u001b[38;5;241m.\u001b[39mstop_recording():\n\u001b[1;32m 195\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_bound_context\u001b[38;5;241m.\u001b[39mexecuting_eagerly():\n\u001b[0;32m--> 196\u001b[0m outputs \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_bound_context\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mcall_function\u001b[49m\u001b[43m(\u001b[49m\n\u001b[1;32m 197\u001b[0m \u001b[43m \u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mname\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 198\u001b[0m \u001b[43m \u001b[49m\u001b[38;5;28;43mlist\u001b[39;49m\u001b[43m(\u001b[49m\u001b[43margs\u001b[49m\u001b[43m)\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 199\u001b[0m \u001b[43m \u001b[49m\u001b[38;5;28;43mlen\u001b[39;49m\u001b[43m(\u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mfunction_type\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mflat_outputs\u001b[49m\u001b[43m)\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 200\u001b[0m \u001b[43m \u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 201\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[1;32m 202\u001b[0m outputs \u001b[38;5;241m=\u001b[39m make_call_op_in_graph(\u001b[38;5;28mself\u001b[39m, \u001b[38;5;28mlist\u001b[39m(args))\n", "File \u001b[0;32m~/miniconda3/envs/python38/lib/python3.8/site-packages/tensorflow/python/eager/context.py:1457\u001b[0m, in \u001b[0;36mContext.call_function\u001b[0;34m(self, name, tensor_inputs, num_outputs)\u001b[0m\n\u001b[1;32m 1455\u001b[0m cancellation_context \u001b[38;5;241m=\u001b[39m cancellation\u001b[38;5;241m.\u001b[39mcontext()\n\u001b[1;32m 1456\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m cancellation_context \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m:\n\u001b[0;32m-> 1457\u001b[0m outputs \u001b[38;5;241m=\u001b[39m \u001b[43mexecute\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mexecute\u001b[49m\u001b[43m(\u001b[49m\n\u001b[1;32m 1458\u001b[0m \u001b[43m \u001b[49m\u001b[43mname\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mdecode\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mutf-8\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m)\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 1459\u001b[0m \u001b[43m \u001b[49m\u001b[43mnum_outputs\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mnum_outputs\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 1460\u001b[0m \u001b[43m \u001b[49m\u001b[43minputs\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mtensor_inputs\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 1461\u001b[0m \u001b[43m \u001b[49m\u001b[43mattrs\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mattrs\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 1462\u001b[0m \u001b[43m \u001b[49m\u001b[43mctx\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[43m,\u001b[49m\n\u001b[1;32m 1463\u001b[0m \u001b[43m \u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 1464\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[1;32m 1465\u001b[0m outputs \u001b[38;5;241m=\u001b[39m execute\u001b[38;5;241m.\u001b[39mexecute_with_cancellation(\n\u001b[1;32m 1466\u001b[0m name\u001b[38;5;241m.\u001b[39mdecode(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mutf-8\u001b[39m\u001b[38;5;124m\"\u001b[39m),\n\u001b[1;32m 1467\u001b[0m num_outputs\u001b[38;5;241m=\u001b[39mnum_outputs,\n\u001b[0;32m (...)\u001b[0m\n\u001b[1;32m 1471\u001b[0m cancellation_manager\u001b[38;5;241m=\u001b[39mcancellation_context,\n\u001b[1;32m 1472\u001b[0m )\n", "File \u001b[0;32m~/miniconda3/envs/python38/lib/python3.8/site-packages/tensorflow/python/eager/execute.py:53\u001b[0m, in \u001b[0;36mquick_execute\u001b[0;34m(op_name, num_outputs, inputs, attrs, ctx, name)\u001b[0m\n\u001b[1;32m 51\u001b[0m \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[1;32m 52\u001b[0m ctx\u001b[38;5;241m.\u001b[39mensure_initialized()\n\u001b[0;32m---> 53\u001b[0m tensors \u001b[38;5;241m=\u001b[39m \u001b[43mpywrap_tfe\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mTFE_Py_Execute\u001b[49m\u001b[43m(\u001b[49m\u001b[43mctx\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_handle\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mdevice_name\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mop_name\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 54\u001b[0m \u001b[43m \u001b[49m\u001b[43minputs\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mattrs\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mnum_outputs\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 55\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m core\u001b[38;5;241m.\u001b[39m_NotOkStatusException \u001b[38;5;28;01mas\u001b[39;00m e:\n\u001b[1;32m 56\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m name \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m:\n", "\u001b[0;31mKeyboardInterrupt\u001b[0m: " ] } ], "source": [ "for pred_col in out_cols:\n", " print(pred_col)\n", " use_cols = feature_cols + [pred_col]\n", " use_data = train_data[use_cols].dropna().reset_index(drop=True)\n", " kf = KFold(n_splits=5, shuffle=True, random_state=42)\n", " vad_eva_list = list()\n", " for (train_index, test_index) in kf.split(use_data):\n", " train = use_data.loc[train_index]\n", " valid = use_data.loc[test_index]\n", " X = np.expand_dims(train[feature_cols].values, axis=1)\n", " Y = train[pred_col].values.T\n", " X_valid = np.expand_dims(valid[feature_cols].values, axis=1)\n", " Y_valid = valid[pred_col].values.T\n", " prediction_model = get_prediction_model()\n", " prediction_model.compile(optimizer='adam', loss=mean_squared_error)\n", " hist = prediction_model.fit(X, Y, epochs=120, batch_size=8, verbose=0, \n", " validation_data=(X_valid, Y_valid),\n", " callbacks=[reduce_lr]\n", " )\n", " rst = prediction_model.predict(X_valid)\n", " pred_rst = rst * (maxs[pred_col] - mins[pred_col]) + mins[pred_col]\n", " real_rst = valid[pred_col] * (maxs[pred_col] - mins[pred_col]) + mins[pred_col]\n", " y_pred_vad = pred_rst.reshape(-1,)\n", " y_true_vad = real_rst.values.reshape(-1,)\n", " vad_eva = print_eva(y_true_vad, y_pred_vad, tp=pred_col)\n", " vad_eva_list.append(vad_eva)\n", " del prediction_model" ] }, { "cell_type": "code", "execution_count": null, "id": "27e0abf7-aa29-467f-bc5e-b66a1adf6165", "metadata": {}, "outputs": [], "source": [ "vad_df = pd.DataFrame.from_records(vad_eva_list, columns=['MSE', 'RMSE', 'MAE', 'MAPE', 'R_2'])\n", "vad_df.sort_values(by='R_2').mean()" ] }, { "cell_type": "code", "execution_count": 52, "id": "070cdb94-6e7b-4028-b6d5-ba8570c902ba", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "MSE 0.317628\n", "RMSE 0.557178\n", "MAE 0.412263\n", "MAPE 0.007993\n", "R_2 0.986373\n", "dtype: float64" ] }, "execution_count": 52, "metadata": {}, "output_type": "execute_result" } ], "source": [ "fcad_df = pd.DataFrame.from_records(fcad_eva_list, columns=['MSE', 'RMSE', 'MAE', 'MAPE', 'R_2'])\n", "fcad_df.sort_values(by='R_2').mean()" ] }, { "cell_type": "code", "execution_count": null, "id": "54c1df2c-c297-4b8d-be8a-3a99cff22545", "metadata": {}, "outputs": [], "source": [ "train, valid = train_test_split(use_data[use_cols], test_size=0.3, random_state=42, shuffle=True)\n", "valid, test = train_test_split(valid, test_size=0.3, random_state=42, shuffle=True)" ] }, { "cell_type": "code", "execution_count": 31, "id": "e7a914da-b9c2-40d9-96e0-459b0888adba", "metadata": {}, "outputs": [], "source": [ "prediction_model = get_prediction_model()\n", "trainable_model = get_trainable_model(prediction_model)" ] }, { "cell_type": "code", "execution_count": 34, "id": "2494ef5a-5b2b-4f11-b6cd-dc39503c9106", "metadata": {}, "outputs": [], "source": [ "X = np.expand_dims(train[feature_cols].values, axis=1)\n", "Y = [x for x in train[out_cols].values.T]\n", "Y_valid = [x for x in valid[out_cols].values.T]" ] }, { "cell_type": "code", "execution_count": null, "id": "cf869e4d-0fce-45a2-afff-46fd9b30fd1c", "metadata": {}, "outputs": [], "source": [ "trainable_model.compile(optimizer='adam', loss=None)\n", "hist = trainable_model.fit([X, Y[0], Y[1]], epochs=120, batch_size=8, verbose=1, \n", " validation_data=[np.expand_dims(valid[feature_cols].values, axis=1), Y_valid[0], Y_valid[1]],\n", " callbacks=[reduce_lr]\n", " )" ] }, { "cell_type": "code", "execution_count": 41, "id": "67bfbe88-5f2c-4659-b2dc-eb9f1b824d04", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[array([[0.73740077],\n", " [0.89292204],\n", " [0.7599046 ],\n", " [0.67802393],\n", " [0.6815233 ],\n", " [0.88627005],\n", " [0.6121343 ],\n", " [0.7072234 ],\n", " [0.8561135 ],\n", " [0.52762157],\n", " [0.8325021 ],\n", " [0.50241977],\n", " [0.8242289 ],\n", " [0.68957335],\n", " [0.6980361 ],\n", " [0.82116604],\n", " [0.8566438 ],\n", " [0.53687835],\n", " [0.56832707],\n", " [0.78476715],\n", " [0.85638577]], dtype=float32),\n", " array([[0.68600863],\n", " [0.78454906],\n", " [0.8179163 ],\n", " [0.94351083],\n", " [0.86383885],\n", " [0.69705516],\n", " [0.6913491 ],\n", " [0.80277354],\n", " [0.93557894],\n", " [0.82278305],\n", " [0.82674253],\n", " [0.93518937],\n", " [0.8094449 ],\n", " [0.9206344 ],\n", " [0.7747319 ],\n", " [0.9137207 ],\n", " [0.9491073 ],\n", " [0.93225 ],\n", " [0.6185102 ],\n", " [0.8867341 ],\n", " [0.82890105]], dtype=float32)]" ] }, "execution_count": 41, "metadata": {}, "output_type": "execute_result" } ], "source": [ "rst = prediction_model.predict(np.expand_dims(test[feature_cols], axis=1))\n", "rst" ] }, { "cell_type": "code", "execution_count": 42, "id": "7de501e9-05a2-424c-a5f4-85d43ad37592", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[0.9991559102070927, 0.9998196796918477]" ] }, "execution_count": 42, "metadata": {}, "output_type": "execute_result" } ], "source": [ "[np.exp(K.get_value(log_var[0]))**0.5 for log_var in trainable_model.layers[-1].log_vars]" ] }, { "cell_type": "code", "execution_count": 46, "id": "5c69d03b-34fd-4dbf-aec6-c15093bb22ab", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Index(['挥发分Vad(%)', '固定炭Fcad(%)'], dtype='object')" ] }, "execution_count": 46, "metadata": {}, "output_type": "execute_result" } ], "source": [ "real_rst.columns" ] }, { "cell_type": "code", "execution_count": null, "id": "294813b8-90be-4007-9fd6-c26ee7bb9652", "metadata": {}, "outputs": [], "source": [ "for col in out_cols:\n", " pred_rst[col] = pred_rst[col] * (maxs[col] - mins[col]) + mins[col]\n", " real_rst[col] = real_rst[col] * (maxs[col] - mins[col]) + mins[col]" ] }, { "cell_type": "code", "execution_count": 47, "id": "21739f82-d82a-4bde-8537-9504b68a96d5", "metadata": {}, "outputs": [], "source": [ "y_pred_vad = pred_rst['挥发分Vad(%)'].values.reshape(-1,)\n", "y_pred_fcad = pred_rst['固定炭Fcad(%)'].values.reshape(-1,)\n", "y_true_vad = real_rst['挥发分Vad(%)'].values.reshape(-1,)\n", "y_true_fcad = real_rst['固定炭Fcad(%)'].values.reshape(-1,)" ] }, { "cell_type": "code", "execution_count": 56, "id": "4ec4caa9-7c46-4fc8-a94b-cb659e924304", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "COL: 挥发分Vad, MSE: 3.35E-01,RMSE: 0.579,MAPE: 1.639 %,MAE: 0.504,R_2: 0.87\n", "COL: 固定炭Fcad, MSE: 1.11E+00,RMSE: 1.055,MAPE: 1.497 %,MAE: 0.814,R_2: 0.876\n" ] } ], "source": [ "pm25_eva = print_eva(y_true_vad, y_pred_vad, tp='挥发分Vad')\n", "pm10_eva = print_eva(y_true_fcad, y_pred_fcad, tp='固定炭Fcad')" ] }, { "cell_type": "code", "execution_count": null, "id": "ac4a4339-ec7d-4266-8197-5276c2395288", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "f15cbb91-1ce7-4fb0-979a-a4bdc452a1ec", "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.18" } }, "nbformat": 4, "nbformat_minor": 5 }