Spaces:
Runtime error
Runtime error
File size: 15,902 Bytes
f637442 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 |
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import pandas as pd\n",
"import numpy as np\n",
"from sklearn.model_selection import train_test_split\n",
"from sklearn.metrics import mean_squared_error, r2_score, mean_absolute_error\n",
"from sklearn.preprocessing import StandardScaler, OneHotEncoder\n",
"from tensorflow.keras.models import Sequential\n",
"from tensorflow.keras.layers import Dense\n",
"from scipy.stats import f\n",
"\n",
"# Load the dataset\n",
"dataset = pd.read_csv('/Users/ashishpoudel/Downloads/AircraftFuelPrediction-main/datasets/preprocessed_data.csv')\n",
"dataset.dropna(inplace=True)"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"# Features and target\n",
"features = dataset[['distance', 'model', 'seats', 'fuel_burn', 'fuel_burn_total']]\n",
"target = dataset['fuel_burn_total']\n",
"\n",
"# Encoding the 'model' column\n",
"encoder = OneHotEncoder(sparse_output=False)\n",
"model_encoded = pd.DataFrame(encoder.fit_transform(features[['model']]))\n",
"model_encoded.columns = encoder.get_feature_names_out(['model'])\n",
"\n",
"# Drop the original 'model' column and add the encoded data\n",
"features = features.drop('model', axis=1)\n",
"features = pd.concat([features.reset_index(drop=True), model_encoded.reset_index(drop=True)], axis=1)\n",
"\n",
"# Train-test split\n",
"feature_train, feature_test, target_train, target_test = train_test_split(features, target, test_size=0.1, random_state=42)\n",
"\n",
"# Feature scaling\n",
"scaler = StandardScaler()\n",
"feature_train_scaled = scaler.fit_transform(feature_train)\n",
"feature_test_scaled = scaler.transform(feature_test)"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Epoch 1/50\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"/opt/anaconda3/envs/Intenv/lib/python3.9/site-packages/keras/src/layers/core/dense.py:87: UserWarning: Do not pass an `input_shape`/`input_dim` argument to a layer. When using Sequential models, prefer using an `Input(shape)` object as the first layer in the model instead.\n",
" super().__init__(activity_regularizer=activity_regularizer, **kwargs)\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"\u001b[1m1314/1314\u001b[0m \u001b[32mββββββββββββββββββββ\u001b[0m\u001b[37m\u001b[0m \u001b[1m9s\u001b[0m 6ms/step - loss: 140.5811\n",
"Epoch 2/50\n",
"\u001b[1m1314/1314\u001b[0m \u001b[32mββββββββββββββββββββ\u001b[0m\u001b[37m\u001b[0m \u001b[1m8s\u001b[0m 6ms/step - loss: 1.9729\n",
"Epoch 3/50\n",
"\u001b[1m1314/1314\u001b[0m \u001b[32mββββββββββββββββββββ\u001b[0m\u001b[37m\u001b[0m \u001b[1m8s\u001b[0m 6ms/step - loss: 0.7662\n",
"Epoch 4/50\n",
"\u001b[1m1314/1314\u001b[0m \u001b[32mββββββββββββββββββββ\u001b[0m\u001b[37m\u001b[0m \u001b[1m9s\u001b[0m 7ms/step - loss: 0.8330\n",
"Epoch 5/50\n",
"\u001b[1m1314/1314\u001b[0m \u001b[32mββββββββββββββββββββ\u001b[0m\u001b[37m\u001b[0m \u001b[1m8s\u001b[0m 6ms/step - loss: 0.7197\n",
"Epoch 6/50\n",
"\u001b[1m1314/1314\u001b[0m \u001b[32mββββββββββββββββββββ\u001b[0m\u001b[37m\u001b[0m \u001b[1m8s\u001b[0m 6ms/step - loss: 0.7294\n",
"Epoch 7/50\n",
"\u001b[1m1314/1314\u001b[0m \u001b[32mββββββββββββββββββββ\u001b[0m\u001b[37m\u001b[0m \u001b[1m8s\u001b[0m 6ms/step - loss: 0.6337\n",
"Epoch 8/50\n",
"\u001b[1m1314/1314\u001b[0m \u001b[32mββββββββββββββββββββ\u001b[0m\u001b[37m\u001b[0m \u001b[1m9s\u001b[0m 7ms/step - loss: 0.4558\n",
"Epoch 9/50\n",
"\u001b[1m1314/1314\u001b[0m \u001b[32mββββββββββββββββββββ\u001b[0m\u001b[37m\u001b[0m \u001b[1m9s\u001b[0m 7ms/step - loss: 0.3461\n",
"Epoch 10/50\n",
"\u001b[1m1314/1314\u001b[0m \u001b[32mββββββββββββββββββββ\u001b[0m\u001b[37m\u001b[0m \u001b[1m8s\u001b[0m 6ms/step - loss: 0.4073\n",
"Epoch 11/50\n",
"\u001b[1m1314/1314\u001b[0m \u001b[32mββββββββββββββββββββ\u001b[0m\u001b[37m\u001b[0m \u001b[1m8s\u001b[0m 6ms/step - loss: 0.3993\n",
"Epoch 12/50\n",
"\u001b[1m1314/1314\u001b[0m \u001b[32mββββββββββββββββββββ\u001b[0m\u001b[37m\u001b[0m \u001b[1m8s\u001b[0m 6ms/step - loss: 0.3657\n",
"Epoch 13/50\n",
"\u001b[1m1314/1314\u001b[0m \u001b[32mββββββββββββββββββββ\u001b[0m\u001b[37m\u001b[0m \u001b[1m8s\u001b[0m 6ms/step - loss: 0.3334\n",
"Epoch 14/50\n",
"\u001b[1m1314/1314\u001b[0m \u001b[32mββββββββββββββββββββ\u001b[0m\u001b[37m\u001b[0m \u001b[1m8s\u001b[0m 6ms/step - loss: 0.3895\n",
"Epoch 15/50\n",
"\u001b[1m1314/1314\u001b[0m \u001b[32mββββββββββββββββββββ\u001b[0m\u001b[37m\u001b[0m \u001b[1m8s\u001b[0m 6ms/step - loss: 0.4462\n",
"Epoch 16/50\n",
"\u001b[1m1314/1314\u001b[0m \u001b[32mββββββββββββββββββββ\u001b[0m\u001b[37m\u001b[0m \u001b[1m8s\u001b[0m 6ms/step - loss: 0.2150\n",
"Epoch 17/50\n",
"\u001b[1m1314/1314\u001b[0m \u001b[32mββββββββββββββββββββ\u001b[0m\u001b[37m\u001b[0m \u001b[1m8s\u001b[0m 6ms/step - loss: 0.3340\n",
"Epoch 18/50\n",
"\u001b[1m1314/1314\u001b[0m \u001b[32mββββββββββββββββββββ\u001b[0m\u001b[37m\u001b[0m \u001b[1m8s\u001b[0m 6ms/step - loss: 0.2634\n",
"Epoch 19/50\n",
"\u001b[1m1314/1314\u001b[0m \u001b[32mββββββββββββββββββββ\u001b[0m\u001b[37m\u001b[0m \u001b[1m9s\u001b[0m 7ms/step - loss: 0.2737\n",
"Epoch 20/50\n",
"\u001b[1m1314/1314\u001b[0m \u001b[32mββββββββββββββββββββ\u001b[0m\u001b[37m\u001b[0m \u001b[1m8s\u001b[0m 6ms/step - loss: 0.2614\n",
"Epoch 21/50\n",
"\u001b[1m1314/1314\u001b[0m \u001b[32mββββββββββββββββββββ\u001b[0m\u001b[37m\u001b[0m \u001b[1m8s\u001b[0m 6ms/step - loss: 0.2445\n",
"Epoch 22/50\n",
"\u001b[1m1314/1314\u001b[0m \u001b[32mββββββββββββββββββββ\u001b[0m\u001b[37m\u001b[0m \u001b[1m8s\u001b[0m 6ms/step - loss: 0.2159\n",
"Epoch 23/50\n",
"\u001b[1m1314/1314\u001b[0m \u001b[32mββββββββββββββββββββ\u001b[0m\u001b[37m\u001b[0m \u001b[1m8s\u001b[0m 6ms/step - loss: 0.4048\n",
"Epoch 24/50\n",
"\u001b[1m1314/1314\u001b[0m \u001b[32mββββββββββββββββββββ\u001b[0m\u001b[37m\u001b[0m \u001b[1m8s\u001b[0m 6ms/step - loss: 0.2998\n",
"Epoch 25/50\n",
"\u001b[1m1314/1314\u001b[0m \u001b[32mββββββββββββββββββββ\u001b[0m\u001b[37m\u001b[0m \u001b[1m8s\u001b[0m 6ms/step - loss: 0.2747\n",
"Epoch 26/50\n",
"\u001b[1m1314/1314\u001b[0m \u001b[32mββββββββββββββββββββ\u001b[0m\u001b[37m\u001b[0m \u001b[1m8s\u001b[0m 6ms/step - loss: 0.2207\n",
"Epoch 27/50\n",
"\u001b[1m1314/1314\u001b[0m \u001b[32mββββββββββββββββββββ\u001b[0m\u001b[37m\u001b[0m \u001b[1m8s\u001b[0m 6ms/step - loss: 0.1944\n",
"Epoch 28/50\n",
"\u001b[1m1314/1314\u001b[0m \u001b[32mββββββββββββββββββββ\u001b[0m\u001b[37m\u001b[0m \u001b[1m8s\u001b[0m 6ms/step - loss: 0.3801\n",
"Epoch 29/50\n",
"\u001b[1m1314/1314\u001b[0m \u001b[32mββββββββββββββββββββ\u001b[0m\u001b[37m\u001b[0m \u001b[1m8s\u001b[0m 6ms/step - loss: 0.2268\n",
"Epoch 30/50\n",
"\u001b[1m1314/1314\u001b[0m \u001b[32mββββββββββββββββββββ\u001b[0m\u001b[37m\u001b[0m \u001b[1m7s\u001b[0m 6ms/step - loss: 0.2105\n",
"Epoch 31/50\n",
"\u001b[1m1314/1314\u001b[0m \u001b[32mββββββββββββββββββββ\u001b[0m\u001b[37m\u001b[0m \u001b[1m8s\u001b[0m 6ms/step - loss: 0.1308\n",
"Epoch 32/50\n",
"\u001b[1m1314/1314\u001b[0m \u001b[32mββββββββββββββββββββ\u001b[0m\u001b[37m\u001b[0m \u001b[1m7s\u001b[0m 6ms/step - loss: 0.1518\n",
"Epoch 33/50\n",
"\u001b[1m1314/1314\u001b[0m \u001b[32mββββββββββββββββββββ\u001b[0m\u001b[37m\u001b[0m \u001b[1m7s\u001b[0m 6ms/step - loss: 0.1473\n",
"Epoch 34/50\n",
"\u001b[1m1314/1314\u001b[0m \u001b[32mββββββββββββββββββββ\u001b[0m\u001b[37m\u001b[0m \u001b[1m8s\u001b[0m 6ms/step - loss: 0.2194\n",
"Epoch 35/50\n",
"\u001b[1m1314/1314\u001b[0m \u001b[32mββββββββββββββββββββ\u001b[0m\u001b[37m\u001b[0m \u001b[1m7s\u001b[0m 6ms/step - loss: 0.1172\n",
"Epoch 36/50\n",
"\u001b[1m1314/1314\u001b[0m \u001b[32mββββββββββββββββββββ\u001b[0m\u001b[37m\u001b[0m \u001b[1m8s\u001b[0m 6ms/step - loss: 0.1910\n",
"Epoch 37/50\n",
"\u001b[1m1314/1314\u001b[0m \u001b[32mββββββββββββββββββββ\u001b[0m\u001b[37m\u001b[0m \u001b[1m8s\u001b[0m 6ms/step - loss: 0.1921\n",
"Epoch 38/50\n",
"\u001b[1m1314/1314\u001b[0m \u001b[32mββββββββββββββββββββ\u001b[0m\u001b[37m\u001b[0m \u001b[1m8s\u001b[0m 6ms/step - loss: 0.2753\n",
"Epoch 39/50\n",
"\u001b[1m1314/1314\u001b[0m \u001b[32mββββββββββββββββββββ\u001b[0m\u001b[37m\u001b[0m \u001b[1m8s\u001b[0m 6ms/step - loss: 0.2847\n",
"Epoch 40/50\n",
"\u001b[1m1314/1314\u001b[0m \u001b[32mββββββββββββββββββββ\u001b[0m\u001b[37m\u001b[0m \u001b[1m8s\u001b[0m 6ms/step - loss: 0.1538\n",
"Epoch 41/50\n",
"\u001b[1m1314/1314\u001b[0m \u001b[32mββββββββββββββββββββ\u001b[0m\u001b[37m\u001b[0m \u001b[1m8s\u001b[0m 6ms/step - loss: 0.1008\n",
"Epoch 42/50\n",
"\u001b[1m1314/1314\u001b[0m \u001b[32mββββββββββββββββββββ\u001b[0m\u001b[37m\u001b[0m \u001b[1m8s\u001b[0m 6ms/step - loss: 0.1592\n",
"Epoch 43/50\n",
"\u001b[1m1314/1314\u001b[0m \u001b[32mββββββββββββββββββββ\u001b[0m\u001b[37m\u001b[0m \u001b[1m8s\u001b[0m 6ms/step - loss: 0.0971\n",
"Epoch 44/50\n",
"\u001b[1m1314/1314\u001b[0m \u001b[32mββββββββββββββββββββ\u001b[0m\u001b[37m\u001b[0m \u001b[1m8s\u001b[0m 6ms/step - loss: 0.1211\n",
"Epoch 45/50\n",
"\u001b[1m1314/1314\u001b[0m \u001b[32mββββββββββββββββββββ\u001b[0m\u001b[37m\u001b[0m \u001b[1m8s\u001b[0m 6ms/step - loss: 0.1177\n",
"Epoch 46/50\n",
"\u001b[1m1314/1314\u001b[0m \u001b[32mββββββββββββββββββββ\u001b[0m\u001b[37m\u001b[0m \u001b[1m8s\u001b[0m 6ms/step - loss: 0.0955\n",
"Epoch 47/50\n",
"\u001b[1m1314/1314\u001b[0m \u001b[32mββββββββββββββββββββ\u001b[0m\u001b[37m\u001b[0m \u001b[1m8s\u001b[0m 6ms/step - loss: 0.0695\n",
"Epoch 48/50\n",
"\u001b[1m1314/1314\u001b[0m \u001b[32mββββββββββββββββββββ\u001b[0m\u001b[37m\u001b[0m \u001b[1m8s\u001b[0m 6ms/step - loss: 0.2184\n",
"Epoch 49/50\n",
"\u001b[1m1314/1314\u001b[0m \u001b[32mββββββββββββββββββββ\u001b[0m\u001b[37m\u001b[0m \u001b[1m8s\u001b[0m 6ms/step - loss: 0.1073\n",
"Epoch 50/50\n",
"\u001b[1m1314/1314\u001b[0m \u001b[32mββββββββββββββββββββ\u001b[0m\u001b[37m\u001b[0m \u001b[1m8s\u001b[0m 6ms/step - loss: 0.1462\n",
"\u001b[1m146/146\u001b[0m \u001b[32mββββββββββββββββββββ\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 2ms/step - loss: 0.0717\n",
"Mean Squared Error: 0.16058479249477386\n",
"\u001b[1m146/146\u001b[0m \u001b[32mββββββββββββββββββββ\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 1ms/step\n"
]
}
],
"source": [
"# Neural network model\n",
"model = Sequential([\n",
" Dense(64, activation='relu', input_shape=(feature_train_scaled.shape[1],)),\n",
" Dense(64, activation='relu'),\n",
" Dense(1)\n",
"])\n",
"\n",
"# Compile and train the model\n",
"model.compile(optimizer='adam', loss='mean_squared_error')\n",
"model.fit(feature_train_scaled, target_train, epochs=50, batch_size=32, verbose=1)\n",
"\n",
"# Evaluate the model\n",
"mse = model.evaluate(feature_test_scaled, target_test)\n",
"print(\"Mean Squared Error:\", mse)\n",
"\n",
"# Predictions and performance metrics\n",
"target_prediction = model.predict(feature_test_scaled)\n",
"r2 = r2_score(target_test, target_prediction)\n",
"mae = mean_absolute_error(target_test, target_prediction)\n",
"mse = mean_squared_error(target_test, target_prediction)"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"R-squared: 0.9780861666108605\n",
"Mean Absolute Error: 0.7006260730692777\n",
"Mean Squared Error: 2.554603752569432\n",
"p-value: 0.0000\n",
"Root Squared Error: 1.60\n",
"F-statistic: 24052.88\n"
]
}
],
"source": [
"# Calculate F-statistic and p-value \n",
"n_samples = len(target)\n",
"n_predictors = feature_train_scaled.shape[1]\n",
"residual = n_samples - n_predictors - 1\n",
"explained_variance = r2 * np.sum((target - np.mean(target))**2)\n",
"unexplained_variance = mse * n_samples\n",
"\n",
"F_value = (explained_variance / n_predictors) / (unexplained_variance / residual)\n",
"p_value = 1 - f.cdf(F_value, n_predictors, residual)\n",
"rse = np.sqrt(mse)\n",
"\n",
"# Print the results\n",
"print(f\"R-squared: {r2}\")\n",
"print(f\"Mean Absolute Error: {mae}\")\n",
"print(f\"Mean Squared Error: {mse}\")\n",
"print(f\"p-value: {p_value:.4f}\")\n",
"print(f\"Root Squared Error: {rse:.2f}\")\n",
"print(f\"F-statistic: {F_value:.2f}\")"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Intenv",
"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.18"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
|