|
39 | 39 | "source": [ |
40 | 40 | "# Libraries\n", |
41 | 41 | "import os\n", |
42 | | - "import numpy as np\n", |
43 | 42 | "import pandas as pd\n", |
44 | | - "from sklearn import tree\n", |
45 | 43 | "import matplotlib.pyplot as plt\n", |
46 | 44 | "from sklearn.metrics import r2_score\n", |
47 | 45 | "from sklearn.preprocessing import MinMaxScaler\n", |
48 | | - "from sklearn.neural_network import MLPRegressor\n", |
49 | 46 | "from sklearn.model_selection import train_test_split\n", |
50 | 47 | "\n", |
51 | | - "os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'\n", |
| 48 | + "# Set TensorFlow log level to suppress warnings and info messages\n", |
| 49 | + "os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'\n", |
| 50 | + "\n", |
| 51 | + "from tensorflow import keras\n", |
52 | 52 | "from tensorflow.keras.models import Sequential\n", |
53 | 53 | "from tensorflow.keras.layers import Dense, Dropout\n", |
54 | 54 | "\n", |
|
128 | 128 | }, |
129 | 129 | { |
130 | 130 | "cell_type": "code", |
131 | | - "execution_count": 18, |
| 131 | + "execution_count": 244, |
132 | 132 | "id": "6d76a5cc", |
133 | 133 | "metadata": {}, |
134 | 134 | "outputs": [], |
|
200 | 200 | "# Define the number of features\n", |
201 | 201 | "num_features = X_train.shape[1]\n", |
202 | 202 | "\n", |
203 | | - "# Define the model with dropout\n", |
204 | | - "model = Sequential([\n", |
205 | | - " Dense(64, activation='relu', input_shape=(num_features,)), # Hidden layer 1\n", |
206 | | - " Dropout(0.2), # Dropout layer 1\n", |
207 | | - " Dense(32, activation='relu'), # Hidden layer 2\n", |
208 | | - " Dropout(0.2), # Dropout layer 2\n", |
209 | | - " Dense(16, activation='relu'), # Hidden layer 3\n", |
210 | | - " Dropout(0.2), # Dropout layer 3\n", |
211 | | - " Dense(1) # Output layer\n", |
| 203 | + "# Define the model\n", |
| 204 | + "model = keras.Sequential([\n", |
| 205 | + " keras.Input(shape=(num_features,)),\n", |
| 206 | + " keras.layers.Dense(256, activation='relu'),\n", |
| 207 | + " keras.layers.Dense(128, activation='relu'),\n", |
| 208 | + " keras.layers.Dense(64, activation='relu'),\n", |
| 209 | + " keras.layers.Dense(32, activation='relu'),\n", |
| 210 | + " keras.layers.Dense(1),\n", |
212 | 211 | "])\n", |
213 | 212 | "\n", |
214 | 213 | "# Compile the model\n", |
215 | | - "model.compile(optimizer='adam', loss='mse', metrics=['mae'])\n", |
| 214 | + "model.compile(optimizer='adam', loss='mse', metrics=['mape'])\n", |
216 | 215 | "\n", |
217 | 216 | "# Train the model\n", |
218 | 217 | "history = model.fit(X_train, \n", |
219 | 218 | " y_train, \n", |
220 | 219 | " epochs=100, \n", |
221 | 220 | " validation_split=0.20, \n", |
222 | 221 | " batch_size=32,\n", |
223 | | - " verbose=1)\n", |
224 | | - "\n", |
225 | | - "# Evaluate the model on the test set using the mean absolute error (MAE)\n", |
226 | | - "test_loss, test_mae = model.evaluate(X_test, y_test)\n", |
227 | | - "# print(f\"Test MAE: {test_mae}\")\n", |
| 222 | + " verbose=0)\n", |
228 | 223 | "\n", |
229 | 224 | "# Predict the response for test dataset\n", |
230 | 225 | "y_pred = model.predict(X_test)\n", |
231 | 226 | "\n", |
| 227 | + "# Evaluate the model on the test set using the mean absolute error (MAPE)\n", |
| 228 | + "test_loss, test_mape = model.evaluate(X_test, y_test)\n", |
| 229 | + "print(f\"\\nMAPE: {test_mape:.2f}\")\n", |
| 230 | + "\n", |
232 | 231 | "# Calculate R2 score\n", |
233 | 232 | "r2 = r2_score(y_test, y_pred)\n", |
234 | 233 | "print(f\"R2 score: {r2:.4f}\")\n" |
|
259 | 258 | "plt.show()\n", |
260 | 259 | "\n", |
261 | 260 | "# Plot training & validation MAE values\n", |
262 | | - "plt.plot(history.history['mae'])\n", |
263 | | - "plt.plot(history.history['val_mae'])\n", |
264 | | - "plt.title('Model MAE')\n", |
265 | | - "plt.ylabel('MAE')\n", |
| 261 | + "plt.plot(history.history['mape'])\n", |
| 262 | + "plt.plot(history.history['val_mape'])\n", |
| 263 | + "plt.title('Model MAPE')\n", |
| 264 | + "plt.ylabel('MAPE')\n", |
266 | 265 | "plt.xlabel('Epoch')\n", |
267 | 266 | "plt.legend(['Train', 'Validation'], loc='upper right')\n", |
268 | 267 | "plt.show()" |
|
0 commit comments