Spaces:
Sleeping
Sleeping
File size: 50,568 Bytes
e3c6dcf |
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 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 |
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import pandas as pd \n",
"from datetime import datetime \n",
"from datetime import date\n",
"import matplotlib.pyplot as plt\n",
"import numpy as np\n",
"import pandas as pd\n",
"from keras.models import Sequential\n",
"from keras.layers import LSTM, Dense\n",
"from sklearn.model_selection import train_test_split\n",
"from sklearn.preprocessing import MinMaxScaler,StandardScaler\n",
"from keras.callbacks import ModelCheckpoint\n",
"import tensorflow as tf"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"merged = pd.read_csv(r'../data/long_merge.csv')"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"zones = [72, 71, 63, 62, 60, 59, 58,57, 50, 49, 44, 43, 35, 34, 33, 32, 31, 30, 29, 28, ]\n",
"rtus = [2]\n",
"cols = []\n",
"\n",
"for zone in zones:\n",
" for column in merged.columns:\n",
" if f\"zone_0{zone}\" in column and 'co2' not in column and \"hw_valve\" not in column and \"cooling_sp\" not in column and \"heating_sp\" not in column:\n",
" cols.append(column)\n",
"\n",
"for zone in zones:\n",
" for column in merged.columns:\n",
" if f\"zone_0{zone}\" in column: \n",
" if \"cooling_sp\" in column or \"heating_sp\" in column:\n",
" cols.append(column)\n",
"# for rtu in rtus:\n",
"# for column in merged.columns:\n",
"# if f\"rtu_00{rtu}_fltrd_sa\" in column:\n",
"# cols.append(column)\n",
"cols =['date'] + cols + ['air_temp_set_1',\n",
" 'air_temp_set_2',\n",
" 'dew_point_temperature_set_1d',\n",
" 'relative_humidity_set_1',\n",
" 'solar_radiation_set_1']\n",
"input_dataset = merged[cols]"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"C:\\Users\\arbal\\AppData\\Local\\Temp\\ipykernel_38868\\1855433847.py:1: SettingWithCopyWarning: \n",
"A value is trying to be set on a copy of a slice from a DataFrame.\n",
"Try using .loc[row_indexer,col_indexer] = value instead\n",
"\n",
"See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy\n",
" input_dataset['date'] = pd.to_datetime(input_dataset['date'], format = \"%Y-%m-%d %H:%M:%S\")\n"
]
}
],
"source": [
"input_dataset['date'] = pd.to_datetime(input_dataset['date'], format = \"%Y-%m-%d %H:%M:%S\")\n",
"df_filtered = input_dataset[ (input_dataset.date.dt.date >date(2019, 3, 1)) & (input_dataset.date.dt.date< date(2021, 1, 1))]\n",
"\n",
"if df_filtered.isna().any().any():\n",
" print(\"There are NA values in the DataFrame columns.\")"
]
},
{
"cell_type": "code",
"execution_count": 5,
"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>date</th>\n",
" <th>zone_072_temp</th>\n",
" <th>zone_072_fan_spd</th>\n",
" <th>zone_071_temp</th>\n",
" <th>zone_071_fan_spd</th>\n",
" <th>zone_063_temp</th>\n",
" <th>zone_063_fan_spd</th>\n",
" <th>zone_062_temp</th>\n",
" <th>zone_062_fan_spd</th>\n",
" <th>zone_059_temp</th>\n",
" <th>...</th>\n",
" <th>zone_035_heating_sp</th>\n",
" <th>zone_032_cooling_sp</th>\n",
" <th>zone_032_heating_sp</th>\n",
" <th>zone_030_cooling_sp</th>\n",
" <th>zone_030_heating_sp</th>\n",
" <th>air_temp_set_1</th>\n",
" <th>air_temp_set_2</th>\n",
" <th>dew_point_temperature_set_1d</th>\n",
" <th>relative_humidity_set_1</th>\n",
" <th>solar_radiation_set_1</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>511205</th>\n",
" <td>2019-03-02 00:00:00</td>\n",
" <td>71.2</td>\n",
" <td>40.0</td>\n",
" <td>71.5</td>\n",
" <td>20.0</td>\n",
" <td>72.3</td>\n",
" <td>20.0</td>\n",
" <td>72.9</td>\n",
" <td>55.0</td>\n",
" <td>71.9</td>\n",
" <td>...</td>\n",
" <td>70.0</td>\n",
" <td>74.000000</td>\n",
" <td>68.0</td>\n",
" <td>73.0</td>\n",
" <td>67.0</td>\n",
" <td>11.590</td>\n",
" <td>11.130</td>\n",
" <td>3.00</td>\n",
" <td>55.87</td>\n",
" <td>120.3</td>\n",
" </tr>\n",
" <tr>\n",
" <th>511206</th>\n",
" <td>2019-03-02 00:01:00</td>\n",
" <td>71.2</td>\n",
" <td>40.0</td>\n",
" <td>71.5</td>\n",
" <td>20.0</td>\n",
" <td>72.3</td>\n",
" <td>20.0</td>\n",
" <td>72.9</td>\n",
" <td>55.0</td>\n",
" <td>71.9</td>\n",
" <td>...</td>\n",
" <td>70.0</td>\n",
" <td>74.000000</td>\n",
" <td>68.0</td>\n",
" <td>73.0</td>\n",
" <td>67.0</td>\n",
" <td>11.590</td>\n",
" <td>11.130</td>\n",
" <td>3.00</td>\n",
" <td>55.87</td>\n",
" <td>120.3</td>\n",
" </tr>\n",
" <tr>\n",
" <th>511207</th>\n",
" <td>2019-03-02 00:02:00</td>\n",
" <td>71.2</td>\n",
" <td>40.0</td>\n",
" <td>71.5</td>\n",
" <td>20.0</td>\n",
" <td>72.3</td>\n",
" <td>20.0</td>\n",
" <td>72.6</td>\n",
" <td>55.0</td>\n",
" <td>71.9</td>\n",
" <td>...</td>\n",
" <td>70.0</td>\n",
" <td>74.000000</td>\n",
" <td>68.0</td>\n",
" <td>73.0</td>\n",
" <td>67.0</td>\n",
" <td>11.590</td>\n",
" <td>11.130</td>\n",
" <td>3.00</td>\n",
" <td>55.87</td>\n",
" <td>120.3</td>\n",
" </tr>\n",
" <tr>\n",
" <th>511208</th>\n",
" <td>2019-03-02 00:03:00</td>\n",
" <td>71.2</td>\n",
" <td>40.0</td>\n",
" <td>71.5</td>\n",
" <td>20.0</td>\n",
" <td>72.3</td>\n",
" <td>20.0</td>\n",
" <td>72.9</td>\n",
" <td>55.0</td>\n",
" <td>71.9</td>\n",
" <td>...</td>\n",
" <td>70.0</td>\n",
" <td>74.000000</td>\n",
" <td>68.0</td>\n",
" <td>73.0</td>\n",
" <td>67.0</td>\n",
" <td>11.590</td>\n",
" <td>11.130</td>\n",
" <td>3.00</td>\n",
" <td>55.87</td>\n",
" <td>120.3</td>\n",
" </tr>\n",
" <tr>\n",
" <th>511209</th>\n",
" <td>2019-03-02 00:04:00</td>\n",
" <td>71.2</td>\n",
" <td>40.0</td>\n",
" <td>71.4</td>\n",
" <td>20.0</td>\n",
" <td>72.3</td>\n",
" <td>20.0</td>\n",
" <td>72.9</td>\n",
" <td>55.0</td>\n",
" <td>71.9</td>\n",
" <td>...</td>\n",
" <td>70.0</td>\n",
" <td>74.000000</td>\n",
" <td>68.0</td>\n",
" <td>73.0</td>\n",
" <td>67.0</td>\n",
" <td>11.590</td>\n",
" <td>11.130</td>\n",
" <td>3.00</td>\n",
" <td>55.87</td>\n",
" <td>120.3</td>\n",
" </tr>\n",
" <tr>\n",
" <th>...</th>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2072148</th>\n",
" <td>2020-12-31 23:57:00</td>\n",
" <td>69.5</td>\n",
" <td>40.0</td>\n",
" <td>71.2</td>\n",
" <td>20.0</td>\n",
" <td>68.0</td>\n",
" <td>20.0</td>\n",
" <td>67.6</td>\n",
" <td>40.0</td>\n",
" <td>67.5</td>\n",
" <td>...</td>\n",
" <td>68.0</td>\n",
" <td>72.714138</td>\n",
" <td>71.0</td>\n",
" <td>71.0</td>\n",
" <td>70.0</td>\n",
" <td>13.994</td>\n",
" <td>13.528</td>\n",
" <td>4.11</td>\n",
" <td>51.61</td>\n",
" <td>188.8</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2072149</th>\n",
" <td>2020-12-31 23:58:00</td>\n",
" <td>69.5</td>\n",
" <td>40.0</td>\n",
" <td>71.3</td>\n",
" <td>20.0</td>\n",
" <td>68.0</td>\n",
" <td>20.0</td>\n",
" <td>67.6</td>\n",
" <td>40.0</td>\n",
" <td>67.5</td>\n",
" <td>...</td>\n",
" <td>68.0</td>\n",
" <td>72.714138</td>\n",
" <td>71.0</td>\n",
" <td>71.0</td>\n",
" <td>70.0</td>\n",
" <td>13.994</td>\n",
" <td>13.528</td>\n",
" <td>4.11</td>\n",
" <td>51.61</td>\n",
" <td>188.8</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2072150</th>\n",
" <td>2020-12-31 23:58:00</td>\n",
" <td>69.5</td>\n",
" <td>40.0</td>\n",
" <td>71.3</td>\n",
" <td>20.0</td>\n",
" <td>68.0</td>\n",
" <td>20.0</td>\n",
" <td>67.6</td>\n",
" <td>40.0</td>\n",
" <td>67.5</td>\n",
" <td>...</td>\n",
" <td>68.0</td>\n",
" <td>72.714138</td>\n",
" <td>71.0</td>\n",
" <td>71.0</td>\n",
" <td>70.0</td>\n",
" <td>13.994</td>\n",
" <td>13.528</td>\n",
" <td>4.11</td>\n",
" <td>51.61</td>\n",
" <td>188.8</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2072151</th>\n",
" <td>2020-12-31 23:59:00</td>\n",
" <td>69.5</td>\n",
" <td>40.0</td>\n",
" <td>71.5</td>\n",
" <td>20.0</td>\n",
" <td>68.0</td>\n",
" <td>20.0</td>\n",
" <td>67.6</td>\n",
" <td>40.0</td>\n",
" <td>67.5</td>\n",
" <td>...</td>\n",
" <td>68.0</td>\n",
" <td>72.714138</td>\n",
" <td>71.0</td>\n",
" <td>71.0</td>\n",
" <td>70.0</td>\n",
" <td>13.994</td>\n",
" <td>13.528</td>\n",
" <td>4.11</td>\n",
" <td>51.61</td>\n",
" <td>188.8</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2072152</th>\n",
" <td>2020-12-31 23:59:00</td>\n",
" <td>69.5</td>\n",
" <td>40.0</td>\n",
" <td>71.5</td>\n",
" <td>20.0</td>\n",
" <td>68.0</td>\n",
" <td>20.0</td>\n",
" <td>67.6</td>\n",
" <td>40.0</td>\n",
" <td>67.5</td>\n",
" <td>...</td>\n",
" <td>68.0</td>\n",
" <td>72.714138</td>\n",
" <td>71.0</td>\n",
" <td>71.0</td>\n",
" <td>70.0</td>\n",
" <td>13.994</td>\n",
" <td>13.528</td>\n",
" <td>4.11</td>\n",
" <td>51.61</td>\n",
" <td>188.8</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"<p>1560948 rows × 56 columns</p>\n",
"</div>"
],
"text/plain": [
" date zone_072_temp zone_072_fan_spd zone_071_temp \\\n",
"511205 2019-03-02 00:00:00 71.2 40.0 71.5 \n",
"511206 2019-03-02 00:01:00 71.2 40.0 71.5 \n",
"511207 2019-03-02 00:02:00 71.2 40.0 71.5 \n",
"511208 2019-03-02 00:03:00 71.2 40.0 71.5 \n",
"511209 2019-03-02 00:04:00 71.2 40.0 71.4 \n",
"... ... ... ... ... \n",
"2072148 2020-12-31 23:57:00 69.5 40.0 71.2 \n",
"2072149 2020-12-31 23:58:00 69.5 40.0 71.3 \n",
"2072150 2020-12-31 23:58:00 69.5 40.0 71.3 \n",
"2072151 2020-12-31 23:59:00 69.5 40.0 71.5 \n",
"2072152 2020-12-31 23:59:00 69.5 40.0 71.5 \n",
"\n",
" zone_071_fan_spd zone_063_temp zone_063_fan_spd zone_062_temp \\\n",
"511205 20.0 72.3 20.0 72.9 \n",
"511206 20.0 72.3 20.0 72.9 \n",
"511207 20.0 72.3 20.0 72.6 \n",
"511208 20.0 72.3 20.0 72.9 \n",
"511209 20.0 72.3 20.0 72.9 \n",
"... ... ... ... ... \n",
"2072148 20.0 68.0 20.0 67.6 \n",
"2072149 20.0 68.0 20.0 67.6 \n",
"2072150 20.0 68.0 20.0 67.6 \n",
"2072151 20.0 68.0 20.0 67.6 \n",
"2072152 20.0 68.0 20.0 67.6 \n",
"\n",
" zone_062_fan_spd zone_059_temp ... zone_035_heating_sp \\\n",
"511205 55.0 71.9 ... 70.0 \n",
"511206 55.0 71.9 ... 70.0 \n",
"511207 55.0 71.9 ... 70.0 \n",
"511208 55.0 71.9 ... 70.0 \n",
"511209 55.0 71.9 ... 70.0 \n",
"... ... ... ... ... \n",
"2072148 40.0 67.5 ... 68.0 \n",
"2072149 40.0 67.5 ... 68.0 \n",
"2072150 40.0 67.5 ... 68.0 \n",
"2072151 40.0 67.5 ... 68.0 \n",
"2072152 40.0 67.5 ... 68.0 \n",
"\n",
" zone_032_cooling_sp zone_032_heating_sp zone_030_cooling_sp \\\n",
"511205 74.000000 68.0 73.0 \n",
"511206 74.000000 68.0 73.0 \n",
"511207 74.000000 68.0 73.0 \n",
"511208 74.000000 68.0 73.0 \n",
"511209 74.000000 68.0 73.0 \n",
"... ... ... ... \n",
"2072148 72.714138 71.0 71.0 \n",
"2072149 72.714138 71.0 71.0 \n",
"2072150 72.714138 71.0 71.0 \n",
"2072151 72.714138 71.0 71.0 \n",
"2072152 72.714138 71.0 71.0 \n",
"\n",
" zone_030_heating_sp air_temp_set_1 air_temp_set_2 \\\n",
"511205 67.0 11.590 11.130 \n",
"511206 67.0 11.590 11.130 \n",
"511207 67.0 11.590 11.130 \n",
"511208 67.0 11.590 11.130 \n",
"511209 67.0 11.590 11.130 \n",
"... ... ... ... \n",
"2072148 70.0 13.994 13.528 \n",
"2072149 70.0 13.994 13.528 \n",
"2072150 70.0 13.994 13.528 \n",
"2072151 70.0 13.994 13.528 \n",
"2072152 70.0 13.994 13.528 \n",
"\n",
" dew_point_temperature_set_1d relative_humidity_set_1 \\\n",
"511205 3.00 55.87 \n",
"511206 3.00 55.87 \n",
"511207 3.00 55.87 \n",
"511208 3.00 55.87 \n",
"511209 3.00 55.87 \n",
"... ... ... \n",
"2072148 4.11 51.61 \n",
"2072149 4.11 51.61 \n",
"2072150 4.11 51.61 \n",
"2072151 4.11 51.61 \n",
"2072152 4.11 51.61 \n",
"\n",
" solar_radiation_set_1 \n",
"511205 120.3 \n",
"511206 120.3 \n",
"511207 120.3 \n",
"511208 120.3 \n",
"511209 120.3 \n",
"... ... \n",
"2072148 188.8 \n",
"2072149 188.8 \n",
"2072150 188.8 \n",
"2072151 188.8 \n",
"2072152 188.8 \n",
"\n",
"[1560948 rows x 56 columns]"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df_filtered"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[]"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"testdataset_df = df_filtered[(df_filtered.date.dt.date >date(2020, 3, 1)) & (df_filtered.date.dt.date <date(2020,7, 1))]\n",
"\n",
"# traindataset_df = df_filtered[ (df_filtered.date.dt.date >date(2019, 11, 8))]\n",
"\n",
"traindataset_df = df_filtered[(df_filtered.date.dt.date >date(2019, 3, 1)) & (df_filtered.date.dt.date <date(2020, 3, 1)) | (df_filtered.date.dt.date >date(2020, 7, 1)) & (df_filtered.date.dt.date <date(2020, 12, 1))]\n",
"testdataset = testdataset_df.drop(columns=[\"date\"]).values\n",
"traindataset = traindataset_df.drop(columns=[\"date\"]).values\n",
"\n",
"columns_with_na = traindataset_df.columns[traindataset_df.isna().any()].tolist()\n",
"columns_with_na"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"Index(['date', 'zone_072_temp', 'zone_072_fan_spd', 'zone_071_temp',\n",
" 'zone_071_fan_spd', 'zone_063_temp', 'zone_063_fan_spd',\n",
" 'zone_062_temp', 'zone_062_fan_spd', 'zone_059_temp',\n",
" 'zone_059_fan_spd', 'zone_058_temp', 'zone_058_fan_spd',\n",
" 'zone_057_temp', 'zone_057_fan_spd', 'zone_049_temp',\n",
" 'zone_049_fan_spd', 'zone_044_temp', 'zone_044_fan_spd',\n",
" 'zone_043_temp', 'zone_043_fan_spd', 'zone_035_temp',\n",
" 'zone_035_fan_spd', 'zone_033_temp', 'zone_033_fan_spd',\n",
" 'zone_032_temp', 'zone_032_fan_spd', 'zone_030_temp',\n",
" 'zone_030_fan_spd', 'zone_028_temp', 'zone_028_fan_spd'],\n",
" dtype='object')"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"traindataset_df.columns[0:31]"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0 0\n"
]
}
],
"source": [
"print(traindataset_df.isna().sum().sum(), testdataset_df.isna().sum().sum())"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(1073512, 391818)"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"len(traindataset), len(testdataset)"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [],
"source": [
"traindataset = traindataset.astype('float32')\n",
"testdataset = testdataset.astype('float32')\n",
"\n",
"scaler = StandardScaler()\n",
"traindataset = scaler.fit_transform(traindataset)\n",
"testdataset = scaler.transform(testdataset)"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(1073512, 55)"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"traindataset.shape"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [],
"source": [
"train,test = traindataset,testdataset\n",
"\n",
"def create_dataset(dataset,time_step):\n",
" x = []\n",
" Y = []\n",
" for i in range(len(dataset) - time_step - 1):\n",
" x.append(dataset[i:(i+time_step),:])\n",
" Y.append(dataset[i+time_step,0:31])\n",
" x= np.array(x)\n",
" Y = np.array(Y)\n",
" return x,Y\n",
"time_step = 30\n",
"X_train, y_train = create_dataset(train, time_step)\n",
"X_test, y_test = create_dataset(test, time_step)\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"((1073481, 30, 55), (1073481, 31))"
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"X_train.shape, y_train.shape"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [],
"source": [
"\n",
"model = Sequential()\n",
"model.add(LSTM(units=50, return_sequences=True, input_shape=(X_train.shape[1], X_train.shape[2])))\n",
"model.add(LSTM(units=50, return_sequences=True))\n",
"model.add(LSTM(units=30))\n",
"model.add(Dense(units=y_train.shape[1]))\n",
"\n",
"model.compile(optimizer='adam', loss='mean_squared_error')\n",
"\n",
"checkpoint_path = \"lstm_vav_02.tf\"\n",
"checkpoint_callback = ModelCheckpoint(filepath=checkpoint_path, monitor='val_loss', verbose=1, save_best_only=True, mode='min')\n",
"# model.fit(X_train, y_train, validation_data=(X_test, y_test), epochs=5, batch_size=128, verbose=1, callbacks=[checkpoint_callback])"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"<tensorflow.python.checkpoint.checkpoint.CheckpointLoadStatus at 0x2b142b76250>"
]
},
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"model.load_weights(checkpoint_path)"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"12244/12244 [==============================] - 61s 5ms/step\n"
]
}
],
"source": [
"test_predict1 = model.predict(X_test)"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {},
"outputs": [],
"source": [
"%matplotlib qt\n",
"plt.figure()\n",
"var = 2\n",
"plt.plot(y_test[:,var], label='Original Testing Data', color='blue')\n",
"plt.plot(test_predict1[:,var], label='Predicted Testing Data', color='red',alpha=0.8)\n",
"anomalies = np.where(abs(test_predict1[:,var] - y_test[:,var]) > 0.38)\n",
"plt.scatter(anomalies,test_predict1[anomalies,var], color='black',marker =\"o\",s=100 )\n",
"\n",
"\n",
"plt.title('Testing Data - Predicted vs Actual')\n",
"plt.xlabel('Time')\n",
"plt.ylabel('Value')\n",
"plt.legend()\n",
"plt.show()"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
" 1254/33547 [>.............................] - ETA: 3:00"
]
},
{
"ename": "KeyboardInterrupt",
"evalue": "",
"output_type": "error",
"traceback": [
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[1;31mKeyboardInterrupt\u001b[0m Traceback (most recent call last)",
"Cell \u001b[1;32mIn[18], line 2\u001b[0m\n\u001b[0;32m 1\u001b[0m get_ipython()\u001b[38;5;241m.\u001b[39mrun_line_magic(\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mmatplotlib\u001b[39m\u001b[38;5;124m'\u001b[39m, \u001b[38;5;124m'\u001b[39m\u001b[38;5;124mqt\u001b[39m\u001b[38;5;124m'\u001b[39m)\n\u001b[1;32m----> 2\u001b[0m test_predict2 \u001b[38;5;241m=\u001b[39m \u001b[43mmodel\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mpredict\u001b[49m\u001b[43m(\u001b[49m\u001b[43mX_train\u001b[49m\u001b[43m)\u001b[49m\n",
"File \u001b[1;32md:\\Programs\\minconda3\\envs\\smartbuildings\\Lib\\site-packages\\keras\\src\\utils\\traceback_utils.py:65\u001b[0m, in \u001b[0;36mfilter_traceback.<locals>.error_handler\u001b[1;34m(*args, **kwargs)\u001b[0m\n\u001b[0;32m 63\u001b[0m filtered_tb \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;01mNone\u001b[39;00m\n\u001b[0;32m 64\u001b[0m \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[1;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[0;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[0;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[1;32md:\\Programs\\minconda3\\envs\\smartbuildings\\Lib\\site-packages\\keras\\src\\engine\\training.py:2554\u001b[0m, in \u001b[0;36mModel.predict\u001b[1;34m(self, x, batch_size, verbose, steps, callbacks, max_queue_size, workers, use_multiprocessing)\u001b[0m\n\u001b[0;32m 2552\u001b[0m \u001b[38;5;28;01mfor\u001b[39;00m step \u001b[38;5;129;01min\u001b[39;00m data_handler\u001b[38;5;241m.\u001b[39msteps():\n\u001b[0;32m 2553\u001b[0m callbacks\u001b[38;5;241m.\u001b[39mon_predict_batch_begin(step)\n\u001b[1;32m-> 2554\u001b[0m tmp_batch_outputs \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mpredict_function\u001b[49m\u001b[43m(\u001b[49m\u001b[43miterator\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m 2555\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m data_handler\u001b[38;5;241m.\u001b[39mshould_sync:\n\u001b[0;32m 2556\u001b[0m context\u001b[38;5;241m.\u001b[39masync_wait()\n",
"File \u001b[1;32md:\\Programs\\minconda3\\envs\\smartbuildings\\Lib\\site-packages\\tensorflow\\python\\util\\traceback_utils.py:150\u001b[0m, in \u001b[0;36mfilter_traceback.<locals>.error_handler\u001b[1;34m(*args, **kwargs)\u001b[0m\n\u001b[0;32m 148\u001b[0m filtered_tb \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;01mNone\u001b[39;00m\n\u001b[0;32m 149\u001b[0m \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[1;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[0;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[0;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[1;32md:\\Programs\\minconda3\\envs\\smartbuildings\\Lib\\site-packages\\tensorflow\\python\\eager\\polymorphic_function\\polymorphic_function.py:825\u001b[0m, in \u001b[0;36mFunction.__call__\u001b[1;34m(self, *args, **kwds)\u001b[0m\n\u001b[0;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[0;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[1;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[0;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[0;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[1;32md:\\Programs\\minconda3\\envs\\smartbuildings\\Lib\\site-packages\\tensorflow\\python\\eager\\polymorphic_function\\polymorphic_function.py:864\u001b[0m, in \u001b[0;36mFunction._call\u001b[1;34m(self, *args, **kwds)\u001b[0m\n\u001b[0;32m 861\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_lock\u001b[38;5;241m.\u001b[39mrelease()\n\u001b[0;32m 862\u001b[0m \u001b[38;5;66;03m# In this case we have not created variables on the first call. So we can\u001b[39;00m\n\u001b[0;32m 863\u001b[0m \u001b[38;5;66;03m# run the first trace but we should fail if variables are created.\u001b[39;00m\n\u001b[1;32m--> 864\u001b[0m results \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_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\n\u001b[0;32m 865\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_created_variables \u001b[38;5;129;01mand\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m ALLOW_DYNAMIC_VARIABLE_CREATION:\n\u001b[0;32m 866\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mValueError\u001b[39;00m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mCreating variables on a non-first call to a function\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[0;32m 867\u001b[0m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m decorated with tf.function.\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n",
"File \u001b[1;32md:\\Programs\\minconda3\\envs\\smartbuildings\\Lib\\site-packages\\tensorflow\\python\\eager\\polymorphic_function\\tracing_compiler.py:148\u001b[0m, in \u001b[0;36mTracingCompiler.__call__\u001b[1;34m(self, *args, **kwargs)\u001b[0m\n\u001b[0;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[0;32m 146\u001b[0m (concrete_function,\n\u001b[0;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[1;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[0;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[1;32md:\\Programs\\minconda3\\envs\\smartbuildings\\Lib\\site-packages\\tensorflow\\python\\eager\\polymorphic_function\\monomorphic_function.py:1349\u001b[0m, in \u001b[0;36mConcreteFunction._call_flat\u001b[1;34m(self, args, captured_inputs)\u001b[0m\n\u001b[0;32m 1345\u001b[0m possible_gradient_type \u001b[38;5;241m=\u001b[39m gradients_util\u001b[38;5;241m.\u001b[39mPossibleTapeGradientTypes(args)\n\u001b[0;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[0;32m 1347\u001b[0m \u001b[38;5;129;01mand\u001b[39;00m executing_eagerly):\n\u001b[0;32m 1348\u001b[0m \u001b[38;5;66;03m# No tape is watching; skip to running the function.\u001b[39;00m\n\u001b[1;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[0;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[0;32m 1351\u001b[0m args,\n\u001b[0;32m 1352\u001b[0m possible_gradient_type,\n\u001b[0;32m 1353\u001b[0m executing_eagerly)\n\u001b[0;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[1;32md:\\Programs\\minconda3\\envs\\smartbuildings\\Lib\\site-packages\\tensorflow\\python\\eager\\polymorphic_function\\atomic_function.py:196\u001b[0m, in \u001b[0;36mAtomicFunction.__call__\u001b[1;34m(self, *args)\u001b[0m\n\u001b[0;32m 194\u001b[0m \u001b[38;5;28;01mwith\u001b[39;00m record\u001b[38;5;241m.\u001b[39mstop_recording():\n\u001b[0;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[1;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[0;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[0;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[0;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[0;32m 200\u001b[0m \u001b[43m \u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m 201\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[0;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[1;32md:\\Programs\\minconda3\\envs\\smartbuildings\\Lib\\site-packages\\tensorflow\\python\\eager\\context.py:1457\u001b[0m, in \u001b[0;36mContext.call_function\u001b[1;34m(self, name, tensor_inputs, num_outputs)\u001b[0m\n\u001b[0;32m 1455\u001b[0m cancellation_context \u001b[38;5;241m=\u001b[39m cancellation\u001b[38;5;241m.\u001b[39mcontext()\n\u001b[0;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[1;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[0;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[0;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[0;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[0;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[0;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[0;32m 1463\u001b[0m \u001b[43m \u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m 1464\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[0;32m 1465\u001b[0m outputs \u001b[38;5;241m=\u001b[39m execute\u001b[38;5;241m.\u001b[39mexecute_with_cancellation(\n\u001b[0;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[0;32m 1467\u001b[0m num_outputs\u001b[38;5;241m=\u001b[39mnum_outputs,\n\u001b[1;32m (...)\u001b[0m\n\u001b[0;32m 1471\u001b[0m cancellation_manager\u001b[38;5;241m=\u001b[39mcancellation_context,\n\u001b[0;32m 1472\u001b[0m )\n",
"File \u001b[1;32md:\\Programs\\minconda3\\envs\\smartbuildings\\Lib\\site-packages\\tensorflow\\python\\eager\\execute.py:53\u001b[0m, in \u001b[0;36mquick_execute\u001b[1;34m(op_name, num_outputs, inputs, attrs, ctx, name)\u001b[0m\n\u001b[0;32m 51\u001b[0m \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[0;32m 52\u001b[0m ctx\u001b[38;5;241m.\u001b[39mensure_initialized()\n\u001b[1;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[0;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[0;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[0;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[1;31mKeyboardInterrupt\u001b[0m: "
]
}
],
"source": [
"%matplotlib qt\n",
"test_predict2 = model.predict(X_train)\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"plt.figure()\n",
"var = 3\n",
"plt.plot(y_train[:,var], label='Original Training Data', color='blue')\n",
"plt.plot(test_predict2[:,var], label='Predicted Training Data', color='red',alpha=0.8)\n",
"anomalies = np.where(abs(test_predict2[:,var] - y_train[:,var]) > 0.38)\n",
"plt.scatter(anomalies,test_predict2[anomalies,var], color='black',marker =\"o\",s=100 )\n",
"\n",
"\n",
"plt.title('Training Data - Predicted vs Actual')\n",
"plt.xlabel('Time')\n",
"plt.ylabel('Value')\n",
"plt.legend()\n",
"plt.show()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from sklearn.mixture import GaussianMixture\n",
"import numpy as np\n",
"import matplotlib.pyplot as plt\n",
"from sklearn.decomposition import PCA\n",
"\n",
"\n",
"# Generating random data for demonstration\n",
"np.random.seed(0)\n",
"X = test_predict1 - y_test\n",
"\n",
"\n",
"pca = PCA(n_components=2)\n",
"X = pca.fit_transform(X)\n",
"\n",
"\n",
"# Creating the GMM instance with desired number of clusters\n",
"gmm = GaussianMixture(n_components=2)\n",
"\n",
"# Fitting the model to the data\n",
"gmm.fit(X)\n",
"\n",
"# Getting the cluster labels\n",
"labels = gmm.predict(X)\n",
"\n",
"# Plotting the data points with colors representing different clusters\n",
"plt.scatter(X[:, 0], X[:, 1], c=labels, cmap='viridis', alpha=0.5)\n",
"plt.title('GMM Clustering')\n",
"plt.xlabel('Feature 1')\n",
"plt.ylabel('Feature 2')\n",
"plt.show()\n"
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {},
"outputs": [],
"source": [
"from sklearn.cluster import KMeans\n",
"import numpy as np\n",
"import matplotlib.pyplot as plt\n",
"from sklearn.decomposition import PCA\n",
"\n",
"# Generating random data for demonstration\n",
"np.random.seed(0)\n",
"X = (test_predict1 - y_test)\n",
"\n",
"k = 6\n",
"\n",
"kmeans = KMeans(n_clusters=k)\n",
"\n",
"kmeans.fit(X)\n",
"\n",
"pca = PCA(n_components=2)\n",
"X = pca.fit_transform(X)\n",
"\n",
"\n",
"\n",
"# Getting the cluster centers and labels\n",
"centroids = kmeans.cluster_centers_\n",
"centroids = pca.transform(centroids)\n",
"labels = kmeans.labels_\n",
"\n",
"# Plotting the data points and cluster centers\n",
"plt.scatter(X[:, 0], X[:, 1], c=labels, cmap='viridis', alpha=0.5)\n",
"plt.scatter(centroids[:, 0], centroids[:, 1], marker='x', c='red', s=200, linewidths=2)\n",
"plt.title('KMeans Clustering')\n",
"plt.xlabel('Feature 1')\n",
"plt.ylabel('Feature 2')\n",
"plt.show()\n"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {},
"outputs": [],
"source": [
"k = 60\n",
"X= test_predict1 - y_test\n",
"processed_data = []\n",
"feat_df = pd.DataFrame(columns=[\"mean\",\"std\",])\n",
"for i in range(0,len(X), 60):\n",
" mean = X[i:i+k].mean(axis = 0)\n",
" std = X[i:i+k].std(axis = 0)\n",
" max = X[i:i+k].max(axis = 0)\n",
" min = X[i:i+k].min(axis = 0)\n",
" iqr = np.percentile(X[i:i+k], 75, axis=0) - np.percentile(X[i:i+k], 25,axis=0)\n",
" data = np.concatenate([mean, std, max, min, iqr])\n",
" processed_data.append([data])\n",
"processed_data = np.concatenate(processed_data,axis=0) "
]
},
{
"cell_type": "code",
"execution_count": 23,
"metadata": {},
"outputs": [],
"source": [
"X = processed_data\n",
"\n",
"kmeans = KMeans(n_clusters=2, algorithm='elkan', max_iter=1000, n_init = 5)\n",
"\n",
"kmeans.fit(X)\n",
"\n",
"pca = PCA(n_components=2)\n",
"X = pca.fit_transform(X)\n",
"\n",
"\n",
"# Getting the cluster centers and labels\n",
"centroids = kmeans.cluster_centers_\n",
"centroids = pca.transform(centroids)\n",
"labels = kmeans.labels_\n",
"\n",
"# Plotting the data points and cluster centers\n",
"plt.scatter(X[:, 0], X[:, 1], c=labels, cmap='viridis', alpha=0.5)\n",
"plt.scatter(centroids[:, 0], centroids[:, 1], marker='x', c='red', s=200, linewidths=2)\n",
"plt.title('KMeans Clustering')\n",
"plt.xlabel('Feature 1')\n",
"plt.ylabel('Feature 2')\n",
"plt.show()\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from sklearn.mixture import GaussianMixture\n",
"import numpy as np\n",
"import matplotlib.pyplot as plt\n",
"from sklearn.decomposition import PCA\n",
"\n",
"# Generating random data for demonstration\n",
"np.random.seed(0)\n",
"X = processed_data\n",
"\n",
"# Creating the GMM instance with desired number of clusters\n",
"gmm = GaussianMixture(n_components=2, init_params='k-means++')\n",
"\n",
"# Fitting the model to the data\n",
"gmm.fit(X)\n",
"labels = gmm.predict(X)\n",
"\n",
"\n",
"pca = PCA(n_components=2)\n",
"X = pca.fit_transform(X)\n",
"\n",
"\n",
"# Getting the cluster labels\n",
"\n",
"# Plotting the data points with colors representing different clusters\n",
"plt.scatter(X[:, 0], X[:, 1], c=labels, cmap='viridis', alpha=0.5)\n",
"plt.title('GMM Clustering')\n",
"plt.xlabel('Feature 1')\n",
"plt.ylabel('Feature 2')\n",
"plt.show()\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from sklearn.cluster import KMeans\n",
"import numpy as np\n",
"import matplotlib.pyplot as plt\n",
"# Generating random data for demonstration\n",
"np.random.seed(0)\n",
"X = test_predict1 - y_test \n",
"\n",
"kmeans = KMeans(n_clusters=2)\n",
"\n",
"kmeans.fit(X)\n",
"\n",
"\n",
"pca = PCA(n_components=2)\n",
"X = pca.fit_transform(X)\n",
"\n",
"\n",
"\n",
"# Getting the cluster centers and labels\n",
"centroids = kmeans.cluster_centers_\n",
"centroids = pca.transform(centroids)\n",
"labels = kmeans.labels_\n",
"\n",
"# Plotting the data points and cluster centers\n",
"plt.figure()\n",
"plt.scatter(X[:, 0], X[:, 1], c=labels, cmap='viridis', alpha=0.5)\n",
"plt.scatter(centroids[:, 0], centroids[:, 1], marker='x', c='red', s=200, linewidths=2)\n",
"plt.text(centroids[0,0], centroids[0,1], 'Normal', fontsize=12, color='red')\n",
"plt.text(centroids[1,0], centroids[1,1], 'Anomaly', fontsize=12, color='red')\n",
"plt.title('KMeans Clustering')\n",
"plt.xlabel('Feature 1')\n",
"plt.ylabel('Feature 2')\n",
"plt.show()\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"329763"
]
},
"execution_count": 26,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"sum(labels==0)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "tensorflow",
"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.11.8"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
|