antitheft159 commited on
Commit
14f6769
·
verified ·
1 Parent(s): 02e0aec

Upload 2 files

Browse files
Files changed (2) hide show
  1. _908_electricity_demands.ipynb +474 -0
  2. electricity.csv +0 -0
_908_electricity_demands.ipynb ADDED
@@ -0,0 +1,474 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "nbformat": 4,
3
+ "nbformat_minor": 0,
4
+ "metadata": {
5
+ "colab": {
6
+ "provenance": []
7
+ },
8
+ "kernelspec": {
9
+ "name": "python3",
10
+ "display_name": "Python 3"
11
+ },
12
+ "language_info": {
13
+ "name": "python"
14
+ }
15
+ },
16
+ "cells": [
17
+ {
18
+ "cell_type": "code",
19
+ "execution_count": 1,
20
+ "metadata": {
21
+ "id": "qmBKOQx4783m"
22
+ },
23
+ "outputs": [],
24
+ "source": [
25
+ "import pandas as pd\n",
26
+ "from sklearn.model_selection import train_test_split\n",
27
+ "from sklearn.ensemble import RandomForestClassifier\n",
28
+ "from sklearn.metrics import accuracy_score, classification_report"
29
+ ]
30
+ },
31
+ {
32
+ "cell_type": "code",
33
+ "source": [
34
+ "file_path = \"/content/electricity.csv\"\n",
35
+ "data = pd.read_csv(file_path)"
36
+ ],
37
+ "metadata": {
38
+ "id": "uPLyiFpw-Mq3"
39
+ },
40
+ "execution_count": 2,
41
+ "outputs": []
42
+ },
43
+ {
44
+ "cell_type": "code",
45
+ "source": [
46
+ "data.info()"
47
+ ],
48
+ "metadata": {
49
+ "colab": {
50
+ "base_uri": "https://localhost:8080/"
51
+ },
52
+ "id": "oL-xXlvy-ZLl",
53
+ "outputId": "dda52986-4081-490e-bbe7-f114103ef28a"
54
+ },
55
+ "execution_count": 3,
56
+ "outputs": [
57
+ {
58
+ "output_type": "stream",
59
+ "name": "stdout",
60
+ "text": [
61
+ "<class 'pandas.core.frame.DataFrame'>\n",
62
+ "RangeIndex: 45312 entries, 0 to 45311\n",
63
+ "Data columns (total 9 columns):\n",
64
+ " # Column Non-Null Count Dtype \n",
65
+ "--- ------ -------------- ----- \n",
66
+ " 0 date 45312 non-null float64\n",
67
+ " 1 day 45312 non-null object \n",
68
+ " 2 period 45312 non-null float64\n",
69
+ " 3 nswprice 45312 non-null float64\n",
70
+ " 4 nswdemand 45312 non-null float64\n",
71
+ " 5 vicprice 45312 non-null float64\n",
72
+ " 6 vicdemand 45312 non-null float64\n",
73
+ " 7 transfer 45312 non-null float64\n",
74
+ " 8 class 45312 non-null object \n",
75
+ "dtypes: float64(7), object(2)\n",
76
+ "memory usage: 3.1+ MB\n"
77
+ ]
78
+ }
79
+ ]
80
+ },
81
+ {
82
+ "cell_type": "code",
83
+ "source": [
84
+ "data.head(), data.tail()"
85
+ ],
86
+ "metadata": {
87
+ "colab": {
88
+ "base_uri": "https://localhost:8080/"
89
+ },
90
+ "id": "-91jmy1g-lL9",
91
+ "outputId": "885c6254-53cd-473c-a77e-5abfc6b43ddf"
92
+ },
93
+ "execution_count": 4,
94
+ "outputs": [
95
+ {
96
+ "output_type": "execute_result",
97
+ "data": {
98
+ "text/plain": [
99
+ "( date day period nswprice nswdemand vicprice vicdemand transfer \\\n",
100
+ " 0 0.0 b'2' 0.000000 0.056443 0.439155 0.003467 0.422915 0.414912 \n",
101
+ " 1 0.0 b'2' 0.021277 0.051699 0.415055 0.003467 0.422915 0.414912 \n",
102
+ " 2 0.0 b'2' 0.042553 0.051489 0.385004 0.003467 0.422915 0.414912 \n",
103
+ " 3 0.0 b'2' 0.063830 0.045485 0.314639 0.003467 0.422915 0.414912 \n",
104
+ " 4 0.0 b'2' 0.085106 0.042482 0.251116 0.003467 0.422915 0.414912 \n",
105
+ " \n",
106
+ " class \n",
107
+ " 0 b'UP' \n",
108
+ " 1 b'UP' \n",
109
+ " 2 b'UP' \n",
110
+ " 3 b'UP' \n",
111
+ " 4 b'DOWN' ,\n",
112
+ " date day period nswprice nswdemand vicprice vicdemand \\\n",
113
+ " 45307 0.9158 b'7' 0.914894 0.044224 0.340672 0.003033 0.255049 \n",
114
+ " 45308 0.9158 b'7' 0.936170 0.044884 0.355549 0.003072 0.241326 \n",
115
+ " 45309 0.9158 b'7' 0.957447 0.043593 0.340970 0.002983 0.247799 \n",
116
+ " 45310 0.9158 b'7' 0.978723 0.066651 0.329366 0.004630 0.345417 \n",
117
+ " 45311 0.9158 b'7' 1.000000 0.050679 0.288753 0.003542 0.355256 \n",
118
+ " \n",
119
+ " transfer class \n",
120
+ " 45307 0.405263 b'DOWN' \n",
121
+ " 45308 0.420614 b'DOWN' \n",
122
+ " 45309 0.362281 b'DOWN' \n",
123
+ " 45310 0.206579 b'UP' \n",
124
+ " 45311 0.231140 b'DOWN' )"
125
+ ]
126
+ },
127
+ "metadata": {},
128
+ "execution_count": 4
129
+ }
130
+ ]
131
+ },
132
+ {
133
+ "cell_type": "code",
134
+ "source": [
135
+ "data = data.drop(columns=['date'])"
136
+ ],
137
+ "metadata": {
138
+ "id": "T1FZym90-oI8"
139
+ },
140
+ "execution_count": 5,
141
+ "outputs": []
142
+ },
143
+ {
144
+ "cell_type": "code",
145
+ "source": [
146
+ "data = pd.get_dummies(data, columns=['day'], prefix='day')"
147
+ ],
148
+ "metadata": {
149
+ "id": "4J2DpzhT-tBC"
150
+ },
151
+ "execution_count": 6,
152
+ "outputs": []
153
+ },
154
+ {
155
+ "cell_type": "code",
156
+ "source": [
157
+ "X = data.drop(columns=['class'])\n",
158
+ "y = data['class']"
159
+ ],
160
+ "metadata": {
161
+ "id": "NrgeoBNd-xLy"
162
+ },
163
+ "execution_count": 7,
164
+ "outputs": []
165
+ },
166
+ {
167
+ "cell_type": "code",
168
+ "source": [
169
+ "from sklearn.preprocessing import LabelEncoder\n",
170
+ "le = LabelEncoder()\n",
171
+ "y_encoded = le.fit_transform(y)"
172
+ ],
173
+ "metadata": {
174
+ "id": "LEfwdL5Z-1ki"
175
+ },
176
+ "execution_count": 8,
177
+ "outputs": []
178
+ },
179
+ {
180
+ "cell_type": "code",
181
+ "source": [
182
+ "X_train, X_test, y_train, y_test = train_test_split(X, y_encoded, test_size=0.2, random_state=42, stratify=y_encoded)"
183
+ ],
184
+ "metadata": {
185
+ "id": "n1rbHlbz_Isl"
186
+ },
187
+ "execution_count": 9,
188
+ "outputs": []
189
+ },
190
+ {
191
+ "cell_type": "code",
192
+ "source": [
193
+ "model = RandomForestClassifier(n_estimators=100, random_state=42)\n",
194
+ "model.fit(X_train, y_train)"
195
+ ],
196
+ "metadata": {
197
+ "colab": {
198
+ "base_uri": "https://localhost:8080/"
199
+ },
200
+ "id": "OwUeKsVD_bPM",
201
+ "outputId": "38350735-dfc7-496f-ea69-00ee207bade7"
202
+ },
203
+ "execution_count": 10,
204
+ "outputs": [
205
+ {
206
+ "output_type": "execute_result",
207
+ "data": {
208
+ "text/plain": [
209
+ "RandomForestClassifier(random_state=42)"
210
+ ]
211
+ },
212
+ "metadata": {},
213
+ "execution_count": 10
214
+ }
215
+ ]
216
+ },
217
+ {
218
+ "cell_type": "code",
219
+ "source": [
220
+ "y_pred = model.predict(X_test)"
221
+ ],
222
+ "metadata": {
223
+ "id": "2SLxRDDd_iFH"
224
+ },
225
+ "execution_count": 11,
226
+ "outputs": []
227
+ },
228
+ {
229
+ "cell_type": "code",
230
+ "source": [
231
+ "accuracy = accuracy_score(y_test, y_pred)\n",
232
+ "print(f\"Model Accuracy: {accuracy:.2f}\")\n",
233
+ "\n",
234
+ "print(\"Classification Report:\")\n",
235
+ "print(classification_report(y_test, y_pred, target_names=le.classes_))"
236
+ ],
237
+ "metadata": {
238
+ "colab": {
239
+ "base_uri": "https://localhost:8080/"
240
+ },
241
+ "id": "MMUwJ1x__kw6",
242
+ "outputId": "038415e4-74ee-49bb-bea9-2fc5649bcb57"
243
+ },
244
+ "execution_count": 12,
245
+ "outputs": [
246
+ {
247
+ "output_type": "stream",
248
+ "name": "stdout",
249
+ "text": [
250
+ "Model Accuracy: 0.85\n",
251
+ "Classification Report:\n",
252
+ " precision recall f1-score support\n",
253
+ "\n",
254
+ " b'DOWN' 0.86 0.89 0.88 5215\n",
255
+ " b'UP' 0.85 0.80 0.82 3848\n",
256
+ "\n",
257
+ " accuracy 0.85 9063\n",
258
+ " macro avg 0.85 0.85 0.85 9063\n",
259
+ "weighted avg 0.85 0.85 0.85 9063\n",
260
+ "\n"
261
+ ]
262
+ }
263
+ ]
264
+ },
265
+ {
266
+ "cell_type": "code",
267
+ "source": [
268
+ "accuracy = accuracy_score(y_test, y_pred)\n",
269
+ "print(f\"Model Accuracy on Test Set: {accuracy:.2f}\")\n",
270
+ "print(\"Classification Report:\")\n",
271
+ "print(classification_report(y_test, y_pred, target_names=le.classes_))"
272
+ ],
273
+ "metadata": {
274
+ "colab": {
275
+ "base_uri": "https://localhost:8080/"
276
+ },
277
+ "id": "H414Ttnf_1zh",
278
+ "outputId": "1958ff35-21cb-4367-f99c-e9ff752f99ef"
279
+ },
280
+ "execution_count": 13,
281
+ "outputs": [
282
+ {
283
+ "output_type": "stream",
284
+ "name": "stdout",
285
+ "text": [
286
+ "Model Accuracy on Test Set: 0.85\n",
287
+ "Classification Report:\n",
288
+ " precision recall f1-score support\n",
289
+ "\n",
290
+ " b'DOWN' 0.86 0.89 0.88 5215\n",
291
+ " b'UP' 0.85 0.80 0.82 3848\n",
292
+ "\n",
293
+ " accuracy 0.85 9063\n",
294
+ " macro avg 0.85 0.85 0.85 9063\n",
295
+ "weighted avg 0.85 0.85 0.85 9063\n",
296
+ "\n"
297
+ ]
298
+ }
299
+ ]
300
+ },
301
+ {
302
+ "cell_type": "code",
303
+ "source": [
304
+ "from xgboost import XGBClassifier\n",
305
+ "from sklearn.linear_model import LogisticRegression"
306
+ ],
307
+ "metadata": {
308
+ "id": "5hpdaFd8AG_I"
309
+ },
310
+ "execution_count": 14,
311
+ "outputs": []
312
+ },
313
+ {
314
+ "cell_type": "code",
315
+ "source": [
316
+ "!pip install scikit-learn==1.0.2\n",
317
+ "!pip install xgboost --upgrade\n",
318
+ "\n",
319
+ "model = XGBClassifier(n_estimators=500, random_state=42)\n",
320
+ "model.fit(X_train, y_train)"
321
+ ],
322
+ "metadata": {
323
+ "colab": {
324
+ "base_uri": "https://localhost:8080/"
325
+ },
326
+ "id": "9RyBCF_sAMti",
327
+ "outputId": "072aa23a-7b25-4b6a-e00b-4b053fe16f32"
328
+ },
329
+ "execution_count": 15,
330
+ "outputs": [
331
+ {
332
+ "output_type": "stream",
333
+ "name": "stdout",
334
+ "text": [
335
+ "Requirement already satisfied: scikit-learn==1.0.2 in /usr/local/lib/python3.10/dist-packages (1.0.2)\n",
336
+ "Requirement already satisfied: numpy>=1.14.6 in /usr/local/lib/python3.10/dist-packages (from scikit-learn==1.0.2) (1.26.4)\n",
337
+ "Requirement already satisfied: scipy>=1.1.0 in /usr/local/lib/python3.10/dist-packages (from scikit-learn==1.0.2) (1.13.1)\n",
338
+ "Requirement already satisfied: joblib>=0.11 in /usr/local/lib/python3.10/dist-packages (from scikit-learn==1.0.2) (1.4.2)\n",
339
+ "Requirement already satisfied: threadpoolctl>=2.0.0 in /usr/local/lib/python3.10/dist-packages (from scikit-learn==1.0.2) (3.5.0)\n",
340
+ "Requirement already satisfied: xgboost in /usr/local/lib/python3.10/dist-packages (2.1.3)\n",
341
+ "Requirement already satisfied: numpy in /usr/local/lib/python3.10/dist-packages (from xgboost) (1.26.4)\n",
342
+ "Requirement already satisfied: nvidia-nccl-cu12 in /usr/local/lib/python3.10/dist-packages (from xgboost) (2.23.4)\n",
343
+ "Requirement already satisfied: scipy in /usr/local/lib/python3.10/dist-packages (from xgboost) (1.13.1)\n"
344
+ ]
345
+ },
346
+ {
347
+ "output_type": "execute_result",
348
+ "data": {
349
+ "text/plain": [
350
+ "XGBClassifier(base_score=None, booster=None, callbacks=None,\n",
351
+ " colsample_bylevel=None, colsample_bynode=None,\n",
352
+ " colsample_bytree=None, device=None, early_stopping_rounds=None,\n",
353
+ " enable_categorical=False, eval_metric=None, feature_types=None,\n",
354
+ " gamma=None, grow_policy=None, importance_type=None,\n",
355
+ " interaction_constraints=None, learning_rate=None, max_bin=None,\n",
356
+ " max_cat_threshold=None, max_cat_to_onehot=None,\n",
357
+ " max_delta_step=None, max_depth=None, max_leaves=None,\n",
358
+ " min_child_weight=None, missing=nan, monotone_constraints=None,\n",
359
+ " multi_strategy=None, n_estimators=500, n_jobs=None,\n",
360
+ " num_parallel_tree=None, random_state=42, ...)"
361
+ ]
362
+ },
363
+ "metadata": {},
364
+ "execution_count": 15
365
+ }
366
+ ]
367
+ },
368
+ {
369
+ "cell_type": "code",
370
+ "source": [
371
+ "y_pred = model.predict(X_test)\n",
372
+ "\n",
373
+ "accuracy = accuracy_score(y_test, y_pred)\n",
374
+ "print(f\"Model Accuracy: {accuracy:.2f}\")\n",
375
+ "\n",
376
+ "print(\"Classification Report:\")\n",
377
+ "print(classification_report(y_test, y_pred, target_names=le.classes_))"
378
+ ],
379
+ "metadata": {
380
+ "colab": {
381
+ "base_uri": "https://localhost:8080/"
382
+ },
383
+ "id": "80phZNiEAVZ5",
384
+ "outputId": "65e22213-91f5-4c27-c1a0-b4eaf04db020"
385
+ },
386
+ "execution_count": 16,
387
+ "outputs": [
388
+ {
389
+ "output_type": "stream",
390
+ "name": "stdout",
391
+ "text": [
392
+ "Model Accuracy: 0.84\n",
393
+ "Classification Report:\n",
394
+ " precision recall f1-score support\n",
395
+ "\n",
396
+ " b'DOWN' 0.86 0.88 0.87 5215\n",
397
+ " b'UP' 0.83 0.80 0.81 3848\n",
398
+ "\n",
399
+ " accuracy 0.84 9063\n",
400
+ " macro avg 0.84 0.84 0.84 9063\n",
401
+ "weighted avg 0.84 0.84 0.84 9063\n",
402
+ "\n"
403
+ ]
404
+ }
405
+ ]
406
+ },
407
+ {
408
+ "cell_type": "code",
409
+ "source": [
410
+ "model = LogisticRegression(penalty='l2', C=1.0, solver='liblinear', random_state=42) # Changed penalty to 'l2'\n",
411
+ "model.fit(X_train, y_train)"
412
+ ],
413
+ "metadata": {
414
+ "colab": {
415
+ "base_uri": "https://localhost:8080/"
416
+ },
417
+ "id": "7TazBj_1AlgB",
418
+ "outputId": "bedb4784-c6fb-4dab-9d72-a041073daa2c"
419
+ },
420
+ "execution_count": 17,
421
+ "outputs": [
422
+ {
423
+ "output_type": "execute_result",
424
+ "data": {
425
+ "text/plain": [
426
+ "LogisticRegression(random_state=42, solver='liblinear')"
427
+ ]
428
+ },
429
+ "metadata": {},
430
+ "execution_count": 17
431
+ }
432
+ ]
433
+ },
434
+ {
435
+ "cell_type": "code",
436
+ "source": [
437
+ "y_pred = model.predict(X_test)\n",
438
+ "\n",
439
+ "accuracy = accuracy_score(y_test, y_pred)\n",
440
+ "print(f\"Model Accuracy: {accuracy:.2f}\")\n",
441
+ "\n",
442
+ "print(\"Classificatoin Report:\")\n",
443
+ "print(classification_report(y_test, y_pred, target_names=le.classes_))"
444
+ ],
445
+ "metadata": {
446
+ "colab": {
447
+ "base_uri": "https://localhost:8080/"
448
+ },
449
+ "id": "fA57l0S0A4hB",
450
+ "outputId": "1814a73e-5f7d-44ef-e5d6-5d0763f5f2a9"
451
+ },
452
+ "execution_count": 18,
453
+ "outputs": [
454
+ {
455
+ "output_type": "stream",
456
+ "name": "stdout",
457
+ "text": [
458
+ "Model Accuracy: 0.76\n",
459
+ "Classificatoin Report:\n",
460
+ " precision recall f1-score support\n",
461
+ "\n",
462
+ " b'DOWN' 0.75 0.87 0.81 5215\n",
463
+ " b'UP' 0.78 0.61 0.68 3848\n",
464
+ "\n",
465
+ " accuracy 0.76 9063\n",
466
+ " macro avg 0.76 0.74 0.75 9063\n",
467
+ "weighted avg 0.76 0.76 0.75 9063\n",
468
+ "\n"
469
+ ]
470
+ }
471
+ ]
472
+ }
473
+ ]
474
+ }
electricity.csv ADDED
The diff for this file is too large to render. See raw diff