forked from sourabh-swargam/CS418-Project
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path418-Project.py
More file actions
2542 lines (1479 loc) · 67.9 KB
/
418-Project.py
File metadata and controls
2542 lines (1479 loc) · 67.9 KB
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
#!/usr/bin/env python
# coding: utf-8
# ## Introduction
# * The Electricity Usage Forecasting & Price Prediction project aims to develop a machine learning model that can accurately forecast electricity consumption and predict its price.
# * The project will involve collecting historical data on electricity usage and pricing from data sources. This data will then be pre-processed and analyzed to identify patterns and trends in electricity consumption and pricing.
# * Next, various machine learning algorithms such as time-series analysis, regression analysis, and neural networks will be applied to the data to create a predictive model. The model will be trained and validated using historical data, and its accuracy will be tested against new, unseen data.
# * This could be used by energy companies, policymakers, and consumers to make informed decisions about energy usage, pricing, and resource planning.
#
#
# ## Data
# The source of the data is the following link: [LINK](https://data.world/houston/houston-electricity-bills)
#
# There are 4 files, they are:
# 1. July 2011 to June 2012 excel file - 57,430 rows and 24 columns
# 2. May 2012 to April 2013 excel file - 65,806 rows and 24 columns
# 3. July 2012 to June 2013 excel file - 66,776 rows and 24 columns
# 4. July 2013 to June 2014 excel file - 67,838 rows and 24 columns
#
# The following is a brief summary of the data cleaning steps we performed:
# * First, we identified missing data and decided how to handle it, either by imputing the missing values or excluding the observations entirely based on the respective columns.
# * Next, identified and corrected any errors and inconsistencies in the data, such as incorrect values, and formatting the date column.
# * We also removed duplicate data and standardized the format of data across different tables, since we were working with multiple tables and there was overlap between the time period of the datasets which we had to account for.
#
# The data tables contain information regarding the building address, location, service number, billing dates, total amount due.
#
# Description of each column
# 1. Reliant Contract No: A unique identifier for each contract.
# 2. Service Address: Address for the service location
# 3. Meter No: Meter number for the service location.
# 4. ESID: Electric Service Identifier for the service location.
# 5. Business Area: Business area code for the service location.
# 6. Cost Center: Cost center code for the service location.
# 7. Fund: Fund code for the service location.
# 8. Bill Type: Type of bill (e.g. "T" for "Total", "P" for "Partial", etc.).
# 9. Bill Date: Date the bill was generated.
# 10. Read Date: Date the meter was read.
# 11. Due Date: Due date for the bill.
# 12. Meter Read: Meter reading for the service location.
# 13. Base Cost: TBase cost for the service.
# 14. T&D Discretionary: Transmission and Distribution Discretionary charge for the service.
# 15. T&D Charges: Transmission and Distribution charge for the service.
# 16. Current Due: Current due amount for the service.
# 17. Index Charge: Index charge for the service.
# 18. Total Due: Total due amount for the service.
# 19. Franchise Fee: Franchise fee for the service.
# 20. Voucher Date: Date the voucher was issued for the service.
# 21. Billed Demand: Billed demand for the service in KVA.
# 22. kWh Usage: Kilowatt-hour usage for the service.
# 23. Nodal Cu Charge: Nodal Cu Charge for the service.
# 24. Adder Charge: Adder Charge for the service.
#
# Statistical Data Type of Each Column
# 1. Reliant Contract No: integer (ratio)
# 2. Service Address: string (nominal)
# 3. Meter No: integer (nominal)
# 4. ESID: integer (nominal)
# 5. Business Area: integer (ratio))
# 6. Cost Center: integer (ratio)
# 7. Fund: integer (ratio)
# 8. Bill Type: string (nominal)
# 9. Bill Date: date (nominal)
# 10. Read Date: date (nominal)
# 11. Due Date: date (nominal)
# 12. Meter Read: integer (ratio)
# 13. Base Cost: float (nominal)
# 14. T&D Discretionary: float (nominal)
# 15. T&D Charges: float (nominal)
# 16. Current Due: float (nominal)
# 17. Index Charge: float (nominal)
# 18. Total Due: float (nominal)
# 19. Franchise Fee: float (nominal)
# 20. Voucher Date: date (nominal)
# 21. Billed Demand (KVA): integer (nominal)
# 22. kWh Usage: integer (nominal)
# 23. Nodal Cu Charge: float (nominal)
# 24. Adder Charge: float (nominal)
#
# ## Problem
# The key issue in generating electricity is to determine how much capacity to generate in order to meet future demand.
#
# Electricity usage forecasting involves predicting the demand for electricity over a specific eriod. This process has several uses, including energy procurement, where it helps suppliers purchase the right amount of energy to ensure a steady supply.
#
# The advancement of smart infrastructure and integration of distributed renewable power has raised future supply, demand, and pricing uncertainties. This unpredictability has increased interest in price prediction and energy analysis.
#
# ## Research Questions
# 1. Previous electricity usage data can be used for predicting the usage for future (Time-Series) - Hyndavi
# 2. Group areas based on their energy consumption (Clustering) - Sunil
# 3. Electricity usage can be predicted by using correlated features (Regression) - Sourabh
# 4. Classification of bill type can be done using features in the data (Classification) - Sharmisha
# ## Import Statements
# In[45]:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
import seaborn as sns
from scipy import stats
from sklearn.linear_model import LinearRegression, LogisticRegression
from sklearn.ensemble import GradientBoostingRegressor
from sklearn.tree import DecisionTreeClassifier, DecisionTreeRegressor
from sklearn.ensemble import RandomForestClassifier
from sklearn.preprocessing import LabelEncoder
from sklearn.model_selection import train_test_split
from sklearn.metrics import r2_score, mean_squared_error
from sklearn.metrics import accuracy_score, f1_score
import requests,urllib,os,pickle
from tqdm import tqdm
from sklearn.preprocessing import LabelEncoder
from sklearn.cluster import KMeans, DBSCAN,AgglomerativeClustering
from sklearn.preprocessing import StandardScaler, normalize
from sklearn.decomposition import PCA
import scipy.cluster.hierarchy as shc
from IPython import display
from sklearn.metrics import mean_squared_error, mean_absolute_error
from sklearn.preprocessing import MinMaxScaler
from pmdarima.arima import auto_arima
from statsmodels.tsa.arima.model import ARIMA
from statsmodels.graphics.tsaplots import plot_acf, plot_pacf
from statsmodels.tsa.statespace.varmax import VARMAX
from statsmodels.tsa.api import VAR
from statsmodels.tsa.stattools import grangercausalitytests, adfuller
from statsmodels.tsa.seasonal import seasonal_decompose
import statsmodels.api as sm
from tqdm import tqdm_notebook
from itertools import product
import math
from statistics import mean
from keras.models import Sequential
from keras.layers import Dense
from keras.layers import LSTM
from keras.preprocessing.sequence import TimeseriesGenerator
pd.options.display.max_columns=25
import warnings
warnings.filterwarnings('ignore')
# ## Data FY 2012 - Hyndavi
# In[ ]:
data_2012 = pd.read_excel(
'houston-houston-electricity-bills/coh-fy2012-ee-bills-july2011-june2012.xls'
)
orig_shape_2012 = data_2012.shape[0]
data_2012.shape
# In[ ]:
data_2012.head(5)
# ### Checking Nulls
# In[ ]:
data_2012.isna().sum()
# ### Checking Adjustment ($) column
# In[ ]:
data_2012['Adjustment ($)'].value_counts(dropna=False)
# The column does not have any relevant information based on the above reported values. Electing to drop the column.
# In[ ]:
data_2012.drop(columns=['Adjustment ($)'], inplace=True)
# ### Checking Unique Number of Customers
# There are quite a few columns in the dataset that signify relating to a unique person/house/business. Checking the unique counts of such columns.
# In[ ]:
check_unique_columns = ['Reliant Contract No', 'Service Address ', 'Meter No',
'ESID', 'Business Area', 'Cost Center',]
for col in check_unique_columns:
print(f'Number of Unique Values in {col}: {data_2012[col].nunique()}')
# Based on the above reported values and further research online:
#
# ESID signifies a unique ID provided to each customer subscribed to the electricity board. It would be best to choose ESID and Service Address columns going forward as these would provide number of unique customers and the areas (streets) where higher usage of electricity occurs.
#
# Business Area signifies a grouping a number of buildings which covers a certain area. This would be useful usage patterns grouped by certain zones in the city.
# ### Checking Bill Type
# In[ ]:
data_2012['Bill Type'].value_counts(dropna=False)
# Bill Type could signify the type of the connection given. Since commercial, residential and government spaces would have different type of pricing and needs this column could be capturing that information.
# In[ ]:
(
data_2012['Service Address '].nunique(),
data_2012['Meter No'].nunique(),
data_2012['ESID'].nunique()
)
# The next 3 columns are: Bill Date, Read Date and Due Date. Of these it would be best to choose the Bill date across all the data files to keep the data consistent.
# ### Electricity Usage Statistics
# In[ ]:
data_2012[['Meter Read', 'Billed Demand ', 'kWh Usage']].describe()
# There are 3 columns that denote the amount of electricity: Meter Read, Billed Demand, kWh Usage.
#
# Using kWh Usage as a standard unit of measurement.
# In[ ]:
data_2012[[
'Base Cost ($)', 'T&D Discretionary ($)', 'T&D Charges ($)',
'Current Due ($)', 'Total Due ($)', 'Franchise Fee ($)',
'Nodal Cu Charge ($)', 'Reliability Unit Charge ($)'
]].describe()
# Reliability Unit Charge does not contain any useful information. Electing to drop that column.
#
# The columns other than Current Due or Total Due are adding up the value present in these two columns. Going forward choosing the column Total Due ($).
# Based on the above statistics the columns Current Due and Total Due represent the same value.
# ### Selecting and Filtering Columns
# In[ ]:
data_2012.columns
# Based on the above analysis of the dataset choosing the following columns:
#
# 1. ESID
# 2. Business Area
# 3. Service Address
# 3. Bill Type
# 4. Bill Date
# 5. Total Due ($)
# 6. kWh Usage
# In[ ]:
data_2012 = data_2012[[
'ESID', 'Business Area', 'Service Address ', 'Bill Type',
'Bill Date', 'Total Due ($)', 'kWh Usage'
]]
# In[ ]:
rename_cols = {
'ESID': 'esid',
'Business Area': 'business_area',
'Service Address ': 'service_address',
'Bill Type': 'bill_type',
'Bill Date': 'bill_date',
'Total Due ($)': 'total_due',
'kWh Usage': 'kwh_usage'
}
data_2012_main = data_2012.rename(columns=rename_cols)
# Checking for Nulls again and dtypes
# In[ ]:
data_2012_main.isna().sum()
# In[ ]:
data_2012_main.dtypes
# In[ ]:
data_2012_main.shape
# In[ ]:
zscore_2012 = stats.zscore(data_2012_main[['total_due', 'kwh_usage']])
zscore_2012
# Each zscore value signifies how many standard deviations away an individual value is from the mean. This is a good indicator to finding outliers in the dataframe.
#
# Usually z-score=3 is considered as a cut-off value to set the limit. Therefore, any z-score greater than +3 or less than -3 is considered as outlier which is pretty much similar to standard deviation method
# In[ ]:
# data_2012_main = data_2012_main[(np.abs(zscore_2012) < 3).all(axis=1)]
data_2012_main.shape
# The number of rows has decreased from 57,430 to 57,025. So 405 rows were outliers based on the data.
# In[ ]:
data_2012_main.head(5)
# In[ ]:
orig_shape_2012 - data_2012_main.shape[0]
# In[ ]:
data_2012.to_csv('electricity_usage_data_2012.csv', index=False)
# The trend graph of both the cost and energy usage is the same as the value of cost = energy usage times the cost per unit.
# ## Performing a Similar Analysis on FY 2013-1, FY 2013-2, and FY 2014 before merging datasets
# ## Data FY 2013-1 - Sourabh
# The code for the cleaning performed in this section is in the IPYNB: 'July 2012 to June 2013.ipynb'
# In[ ]:
data_2013 = pd.read_excel(
'houston-houston-electricity-bills/coh-fy2013-ee-bills-july2012-june2013.xlsx'
)
orig_shape_2013 = data_2013.shape[0]
data_2013.shape
# In[ ]:
data_2013.head(5)
# ### Checking Nulls
# In[ ]:
data_2013.isna().sum()
# ### Checking Index Charge ($) column - This was previously Adjustment
# In[ ]:
data_2013['Index Charge ($)'].value_counts(dropna=False)
# The column does not have any relevant information based on the above reported values. Electing to drop the column.
# In[ ]:
data_2013.drop(columns=['Index Charge ($)'], inplace=True)
# ### Checking Unique Number of Customers
# There are quite a few columns in the dataset that signify relating to a unique person/house/business. Checking the unique counts of such columns.
# In[ ]:
check_unique_columns = ['Reliant Contract No', 'Service Address ', 'Meter No',
'ESID', 'Business Area', 'Cost Center',]
for col in check_unique_columns:
print(f'Number of Unique Values in {col}: {data_2013[col].nunique()}')
# Based on the above reported values and further research online:
#
# ESID signifies a unique ID provided to each customer subscribed to the electricity board. It would be best to choose ESID and Service Address columns going forward as these would provide number of unique customers and the areas (streets) where higher usage of electricity occurs.
#
# Business Area signifies a grouping a number of buildings which covers a certain area. This would be useful usage patterns grouped by certain zones in the city.
# ### Checking Bill Type
# In[ ]:
data_2013['Bill Type'].value_counts(dropna=False)
# Bill Type could signify the type of the connection given. Since commercial, residential and government spaces would have different type of pricing and needs this column could be capturing that information.
# In[ ]:
(
data_2013['Service Address '].nunique(),
data_2013['Meter No'].nunique(),
data_2013['ESID'].nunique()
)
# The next 3 columns are: Bill Date, Read Date and Due Date. Of these it would be best to choose the Bill date across all the data files to keep the data consistent.
# ### Electricity Usage Statistics
# In[ ]:
data_2013[['Meter Read', 'Billed Demand (KVA)', 'kWh Usage']].describe()
# There are 3 columns that denote the amount of electricity: Meter Read, Billed Demand, kWh Usage.
#
# Using kWh Usage as a standard unit of measurement.
# In[ ]:
data_2013[[
'Base Cost ($)', 'T&D Discretionary ($)', 'T&D Charges ($)',
'Current Due ($)', 'Total Due ($)', 'Franchise Fee ($)',
'Nodal Cu Charge ($)', 'Adder Charge ($)'
]].describe()
# Adder Charge ($) does not contain any useful information. Electing to drop that column. Previously this column was Reliability Unit Charge.
#
# The columns other than Current Due or Total Due are adding up the value present in these two columns. Going forward choosing the column Total Due ($).
# Based on the above statistics the columns Current Due and Total Due represent the same value.
# Based on the above analysis of the dataset choosing the following columns:
#
# 1. ESID
# 2. Business Area
# 3. Service Address
# 3. Bill Type
# 4. Bill Date
# 5. Total Due ($)
# 6. kWh Usage
# ### Selecting and Filtering Columns
# In[ ]:
data_2013 = data_2013[[
'ESID', 'Business Area', 'Service Address ', 'Bill Type',
'Bill Date', 'Total Due ($)', 'kWh Usage'
]]
# In[ ]:
rename_cols = {
'ESID': 'esid',
'Business Area': 'business_area',
'Service Address ': 'service_address',
'Bill Type': 'bill_type',
'Bill Date': 'bill_date',
'Total Due ($)': 'total_due',
'kWh Usage': 'kwh_usage'
}
data_2013_main = data_2013.rename(columns=rename_cols)
# Checking for Nulls again and dtypes
# In[ ]:
data_2013_main.isna().sum()
# In[ ]:
data_2013_main.dropna(subset=['kwh_usage'], inplace=True)
# In[ ]:
data_2013_main.isna().sum()
# In[ ]:
data_2013_main.dtypes
# In[ ]:
data_2013_main.shape
# In[ ]:
zscore_2013 = stats.zscore(data_2013_main[['total_due', 'kwh_usage']])
zscore_2013
# Each zscore value signifies how many standard deviations away an individual value is from the mean. This is a good indicator to finding outliers in the dataframe.
#
# Usually z-score=3 is considered as a cut-off value to set the limit. Therefore, any z-score greater than +3 or less than -3 is considered as outlier which is pretty much similar to standard deviation method
# In[ ]:
# data_2013_main = data_2013_main[(np.abs(zscore_2013) < 3).all(axis=1)]
data_2013_main.shape
# The number of rows has decreased from 66,775 to 66,360. So 415 rows were outliers based on the data.
# In[ ]:
data_2013_main.head(5)
# In[ ]:
orig_shape_2013 - data_2013_main.shape[0]
# In[ ]:
data_2013_main.to_csv('electricity_usage_data_2013.csv', index=False)
# ## Data FY 2013-2 - Sunil
# In[ ]:
data_2013_2 = pd.read_excel(
'houston-houston-electricity-bills/coh-ee-bills-may2012-apr2013.xlsx'
)
orig_shape_2013_2 = data_2013_2.shape[0]
data_2013_2.shape
# In[ ]:
data_2013_2.head(5)
# ### Checking Nulls
# In[ ]:
data_2013_2.isna().sum()
# ### Checking Adjustment ($) column
# This column was named Index Charge in the other FY 2013 electricity usage data file
# In[ ]:
data_2013_2['Adjustment ($)'].value_counts(dropna=False)
# The column does not have any relevant information based on the above reported values. Electing to drop the column.
# In[ ]:
data_2013_2.drop(columns=['Adjustment ($)'], inplace=True)
# ### Checking Unique Number of Customers
# There are quite a few columns in the dataset that signify relating to a unique person/house/business. Checking the unique counts of such columns.
# In[ ]:
check_unique_columns = [
'Reliant Contract No', 'Service Address ', 'Meter No',
'ESID', 'Business Area', 'Cost Center',
]
for col in check_unique_columns:
print(f'Number of Unique Values in {col}: {data_2013_2[col].nunique()}')
# Based on the above reported values and further research online:
#
# ESID signifies a unique ID provided to each customer subscribed to the electricity board. It would be best to choose ESID and Service Address columns going forward as these would provide number of unique customers and the areas (streets) where higher usage of electricity occurs.
#
# Business Area signifies a grouping a number of buildings which covers a certain area. This would be useful usage patterns grouped by certain zones in the city.
# ### Checking Bill Type
# In[ ]:
data_2013_2['Bill Type'].value_counts(dropna=False)
# Bill Type could signify the type of the connection given. Since commercial, residential and government spaces would have different type of pricing and needs this column could be capturing that information.
# In[ ]:
data_2013_2['Service Address '].nunique(), data_2013_2['Meter No'].nunique(), data_2013_2['ESID'].nunique()
# The next 3 columns are: Bill Date, Read Date and Due Date. Of these it would be best to choose the Bill date across all the data files to keep the data consistent.
# ### Electricity Usage Statistics
# In[ ]:
data_2013_2[['Meter Read', 'Billed Demand (KVA)', 'kWh Usage']].describe()
# There are 3 columns that denote the amount of electricity: Meter Read, Billed Demand, kWh Usage.
#
# Using kWh Usage as a standard unit of measurement.
# In[ ]:
data_2013_2[[
'Base Cost ($)', 'T&D Discretionary ($)', 'T&D Charges ($)',
'Current Due ($)', 'Total Due ($)', 'Franchise Fee ($)',
'Nodal Cu Charge ($)', 'Reliability Unit Charge ($)'
]].describe()
# Reliability Unit Charge ($) does not contain any useful information. Electing to drop that column.
#
# The columns other than Current Due or Total Due are adding up the value present in these two columns. Going forward choosing the column Total Due ($).
# Based on the above statistics the columns Current Due and Total Due represent the same value.
# Based on the above analysis of the dataset choosing the following columns:
#
# 1. ESID
# 2. Business Area
# 3. Service Address
# 3. Bill Type
# 4. Bill Date
# 5. Total Due ($)
# 6. kWh Usage
# ### Selecting and Filtering Columns
# In[ ]:
data_2013_2 = data_2013_2[[
'ESID', 'Business Area', 'Service Address ', 'Bill Type',
'Bill Date', 'Total Due ($)', 'kWh Usage'
]]
# In[ ]:
rename_cols = {
'ESID': 'esid',
'Business Area': 'business_area',
'Service Address ': 'service_address',
'Bill Type': 'bill_type',
'Bill Date': 'bill_date',
'Total Due ($)': 'total_due',
'kWh Usage': 'kwh_usage'
}
data_2013_2_main = data_2013_2.rename(columns=rename_cols)
# Checking for Nulls again and dtypes
# In[ ]:
data_2013_2_main.isna().sum()
# In[ ]:
data_2013_2_main.dropna(subset=['kwh_usage'], inplace=True)
# In[ ]:
data_2013_2_main.isna().sum()
# In[ ]:
data_2013_2_main.dtypes
# In[ ]:
data_2013_2_main.shape
# In[ ]:
zscore_2013_2 = stats.zscore(data_2013_2_main[['total_due', 'kwh_usage']])
zscore_2013_2
# Each zscore value signifies how many standard deviations away an individual value is from the mean. This is a good indicator to finding outliers in the dataframe.
#
# Usually z-score=3 is considered as a cut-off value to set the limit. Therefore, any z-score greater than +3 or less than -3 is considered as outlier which is pretty much similar to standard deviation method
# In[ ]:
# data_2013_2_main = data_2013_2_main[(np.abs(zscore_2013_2) < 3).all(axis=1)]
data_2013_2_main.shape
# The number of rows has decreased from 65,805 to 65,388. So 417 rows were outliers based on the data.
# In[ ]:
data_2013_2_main.head(5)
# In[ ]:
orig_shape_2013_2 - data_2013_2_main.shape[0]
# In[ ]:
data_2013_2_main.to_csv('electricity_usage_data_2013_2.csv', index=False)
# ## Data FY 2014 - Sharmisha
# In[ ]:
data_2014 = pd.read_excel(
'houston-houston-electricity-bills/coh-fy2014-ee-bills-july2013-june2014.xlsx'
)
orig_shape_2014 = data_2014.shape[0]
data_2014.shape
# In[ ]:
data_2014.head(5)
# ### Checking Nulls
# In[ ]:
data_2014.isna().sum()
# ### Checking Index Charge ($) column - This was previously Adjustment
# In[ ]:
data_2014['Index Charge ($)'].value_counts(dropna=False)
# The column does does have information regarding a certain price. Since we are using the total due amount at the end, Index Charge ($) does not need to be present again, as it would be included in the total due amount.
# In[ ]:
data_2014.drop(columns=['Index Charge ($)'], inplace=True)
# ### Checking Unique Number of Customers
# There are quite a few columns in the dataset that signify relating to a unique person/house/business. Checking the unique counts of such columns.
# In[ ]:
check_unique_columns = [
'Reliant Contract No', 'Service Address ', 'Meter No',
'ESID', 'Business Area', 'Cost Center'
]
for col in check_unique_columns:
print(f'Number of Unique Values in {col}: {data_2014[col].nunique()}')
# NOTE: Compared to previous years, there is one less business area.
#
# Based on the above reported values and further research online:
#
# ESID signifies a unique ID provided to each customer subscribed to the electricity board. It would be best to choose ESID and Service Address columns going forward as these would provide number of unique customers and the areas (streets) where higher usage of electricity occurs.
#
# Business Area signifies a grouping a number of buildings which covers a certain area. This would be useful usage patterns grouped by certain zones in the city.
# ### Checking Bill Type
# In[ ]:
data_2014['Bill Type'].value_counts(dropna=False)
# Bill Type could signify the type of the connection given. Since commercial, residential and government spaces would have different type of pricing and needs this column could be capturing that information.
#
# Previously there were 3 types of Bills. T, P, and C. But in year 2014 there are only 2 types.
# In[ ]:
(
data_2014['Service Address '].nunique(),
data_2014['Meter No'].nunique(),
data_2014['ESID'].nunique()
)
# The next 3 columns are: Bill Date, Read Date and Due Date. Of these it would be best to choose the Bill date across all the data files to keep the data consistent.
# ### Electricity Usage Statistics
# In[ ]:
data_2014[['Meter Read', 'Billed Demand (KVA)', 'kWh Usage']].describe()
# There are 3 columns that denote the amount of electricity: Meter Read, Billed Demand, kWh Usage.
#
# Using kWh Usage as a standard unit of measurement.
# In[ ]:
data_2014[[
'Base Cost ($)', 'T&D Discretionary ($)', 'T&D Charges ($)',
'Current Due ($)', 'Total Due ($)', 'Franchise Fee ($)',
'Nodal Cu Charge ($)', 'Adder Charge ($)'
]].describe()
# Adder Charge ($) does not contain any useful information. Electing to drop that column. Previously this column was Reliability Unit Charge.
#
# The columns other than Current Due or Total Due are adding up the value present in these two columns. Going forward choosing the column Total Due ($).
# Based on the above statistics the columns Current Due and Total Due represent the same value.
# Based on the above analysis of the dataset choosing the following columns:
#
# 1. ESID
# 2. Business Area
# 3. Service Address
# 3. Bill Type
# 4. Bill Date
# 5. Total Due ($)
# 6. kWh Usage
# ### Selecting and Filtering Columns
# In[ ]:
data_2014 = data_2014[[
'ESID', 'Business Area', 'Service Address ', 'Bill Type',
'Bill Date', 'Total Due ($)', 'kWh Usage'
]]
# In[ ]:
rename_cols = {
'ESID': 'esid',
'Business Area': 'business_area',
'Service Address ': 'service_address',
'Bill Type': 'bill_type',
'Bill Date': 'bill_date',
'Total Due ($)': 'total_due',
'kWh Usage': 'kwh_usage'
}
data_2014_main = data_2014.rename(columns=rename_cols)
# Checking for Nulls again and dtypes
# In[ ]:
data_2014_main.isna().sum()
# In[ ]:
data_2014_main.dtypes
# In[ ]:
data_2014_main.shape
# In[ ]:
zscore_2014 = stats.zscore(data_2014_main[['total_due', 'kwh_usage']])
zscore_2014
# Each zscore value signifies how many standard deviations away an individual value is from the mean. This is a good indicator to finding outliers in the dataframe.
#
# Usually z-score=3 is considered as a cut-off value to set the limit. Therefore, any z-score greater than +3 or less than -3 is considered as outlier which is pretty much similar to standard deviation method
# In[ ]: