CoolFace
Apppublic

Devina707/Building-Energy-Demand-Prediction

sourceHugging Faceupdated 5mo agoView on Hugging Face
0likes
m2_eda.cpython-310.pyc150 linesDownload Raw Back to __pycache__
1o

2�_�iXY�@szddlZddlZddlZddlmZddl	m3Zddlm
ZddlZddl	m4Zddlm
Zdd�Zedkr;e�dSdS)�Ncs5t�d�t�d�t�d�}tj|dd�t�d�t�d�t�d�t�d	�t�d6�t�d�t�d�}t�	|�t�d
�t�	|�7gd�gd�gd�gd�gd�gd�gd�gd�gd�gd�d�8��t�d�t�d�tjddd�\}}|d�
�}|jdd|ddgt|�d�t��t�|�t�d�t�d�d|d�d�|d�d �|d�d!�d"g}gd#�}t�|d�����t�d$�tj|d||d%d&�|d'<||d(d)k|d*��@|d'��@}|jdd'gd%d+�d*����jd*d,id-��d.�}|jdd'd,d/�}	t�|	�t�d0�tjd1d2�\}9}|	jd3d4d5|d6�|jd7d8d9�|jdd:d9�|j d;d:d9�|j!d<d=d>�|j"d?d@dAdB�t��t�|10�t�dC�t�dD�||dEdFkd*}||dEdGkd*}tjd.dHd�\}
}t#j$||d)dI�|d)�%d)g�|d)�&dJg�|d)�dE�|d)� d*�|d)jdKd:dLdM�t#j$||ddI�|d�%dg�|d�&dNg�|d�dE�|d� d*�|djdOd:dLdM�t�|
�t�dP�t�dQ�||d(d)kd*}||d(dRkd*}tjd.dHd�\}}t#j$||d)dI�|d)�%d)g�|d)�&dSg�|d)�d(�|d)� d*�|d)jdKd:dLdM�t#j$||ddI�|d�%dg�|d�&dTg�|d�d(�|d� d*�|djdOd:dLdM�t�|�t�dU�t�dV�tjddWd�\}}t#j'|gdX�j(d%dY�d%dZd[�|jd\d:dLdM�t�|�t�d]�tjd.d^d�\}}|d)j)|d|d*d_d`dad db�|d)�dc�|d)�d�|d)� d*�t*�+|d|d*d�\��|d)jt,|d���fddde�t,|d�D�dfd.dgdh�|d)�"�|dj)|di|d*d_d`dad db�|d�dj�|d�di�|d� d*�t*�+|di|d*d�\��|djt,|di���fdkde�t,|di�D�dfd.dgdh�|d�"�t�|�t�dl�t�dm�t�-dndo�}tjdd.dpd2�\}}t#j.||dqd%|d)d`dr�|d)�ds|���|d)�|�|dj)|||d*dtd`dad db�|d�|�du��|d�|�|d� d*�||d*g�/�}t*�+|||d*d�\��|djt,||���fdvde�t,||�D�dfd.dgdh�|d�"�t��t�|�dS)wNz5BuildSmart: ML-Based Energy Consumption Predictor AppzBML-powered meter reading predictions for smarter energy managementz/./src/parameter-speedometer-energy-free-png.pngzEnergy Meter Reading Predictor)�captionzNThis page is built by *Devina Agustina* as part of Hacktiv8 milestone project!u�## **Input your building details and get instant energy consumption predictions** — *powered by a hypertuned machine learning model trained on ASHRAE data*z8#### I. Exploratory Data Analysis (EDA) From ASHRAE Dataz##### A. Data Frame Loadz^###### The data used in this ML App is obtained from ASHRAE Great Energy Predictor III DatasetzC###### This app is used to predict energy demand in area Site ID 1.z=D:\Workspace\Phase1\M2-Energy-Demand-Predictor\src\df_raw.csvz##### B. Descriptive Analysis)�mean�median�std�skew�kurtosis�var)11�Building Area (sqft)�Floor Count�Meter Reading (kWh)�Air Temperature (°C)�Cloud Coverage (oktas)�Dew Temperature (°C)zPrecipitation Depth (mm/hr)�Sea Level Pressure (mbar/hPa)�Wind Direction (degree)�Wind Speed (m/s)u�12    This data can also be used for descriptive analysis. The skewness value for columns13    **'Air Temperature (°C)'**, **'Dew Temperature (°C)'**, and **'Wind Direction (degree)'**14    fall within the range of -0.5 to 0.5, which indicates symmetric skewness, meaning the mean15    and median are similar or nearly identical.16 17    For columns **'Building Area (sqft)'**, **'Floor Count'**, **'Meter Reading (kWh)'**,18    **'Cloud Coverage (oktas)'**, and **'Wind Speed (m/s)'**, the skewness is positive (> 0.5),19    meaning the mean is larger than the median. This indicates that the data is concentrated in20    lower values with few higher values, showing that the data is not normally distributed.21 22    For column **'Sea Level Pressure (mbar/hPa)'**, the skewness value is below -0.5, indicating23    negative skewness where the mean is less than the median. This means the data is concentrated24    in higher values with few lower values.25 26    Regarding kurtosis, columns with high kurtosis (> 3) are **'Meter Reading (kWh)'** and27    **'Cloud Coverage (oktas)'**, indicating leptokurtic distributions with a high presence of28    outliers. Columns **'Floor Count'**, **'Sea Level Pressure (mbar/hPa)'**, and29    **'Wind Speed (m/s)'** have kurtosis values near 3, indicating mesokurtic distributions that30    are close to normal with fewer outliers. Columns **'Building Area (sqft)'**,31    **'Air Temperature (°C)'**, **'Dew Temperature (°C)'**, and **'Wind Direction (degree)'**32    have negative kurtosis (< 0), indicating polykurtic distributions with very few outliers.33 34    Column **'Precipitation Depth (mm/hr)'** contains no values and is recorded as NaN throughout.35    This column will likely be dropped during feature engineering.36 37    In conclusion, the majority of the data in this dataframe does not follow a normal distribution,38    and this assumption will be carried forward in the descriptive and inferential analysis.39    z/#### C. EDA: Building Use Category Distribution�)��)�ncols�figsize�Building Use�piez%.2f%%�g{�G�z�?)�kindZautopct�axZylabel�explodea*40    From the figure shown above, it can be seen that the use of buildings for Education dominates with 41    47.62% of the total buildings in area site 1, followed by Office (28.58%), Lodging/Residential (15.87%), 42    Public Service (6.35%), and lastly Entertainment/Public Assembly as the least represented building use. 43    This shows that most buildings in area site 1 are used for education and office purposes, accounting for more 44    than 70% of the total building use. Therefore, it is safe to say that area site 1 is not a predominantly 45    residential area and is more likely characterized as a Central Business District (CBD), defined by a high 46    concentration of office and educational facilities. This type of area contributes significantly to the local 47    economy and community life.48    zS#### D. EDA: Effect of Building Use and Building Area to Energy (Electricity) Usagei�r	g�?g�?g�?i	�)zMin-25%z25%-50%z50%-75%z75%-MaxaIAccording to the data above, the minimum building area is 49             approximately 5,374 sqft while the maximum building area is 50             approximately 174,601 sqft. Therefore, we will examine whether 51             the minimum, 25th percentile, 50th percentile, 75th percentile, 52             and maximum building area across each building use category have 53             any difference in their meter readings. This would give us insight 54             into how much building area affects the meter reading for energy usage 55             (electricity) in these buildings.56 57    T)�bins�labelsZinclude_lowestZarea_percentile�58Meter Typerr)ZobservedZavg_meter_reading)�columns�)�indexr �valuesa=59             As seen from the data above, there is a clear trend of increasing electricity usage as 60             building area grows larger. However, the rate of increase differs significantly by building 61             use type. The visualization below provides a clearer picture of this relationship.62             )��)r�barZviridis�black)rZcolormapZ	edgecolorrzSAverage Electricity Meter Reading by Building Use & Building Area (sqft) Percentile�)�fontsize�zAvg Meter Reading (kWh)�x�-)�axisZrotationzArea Percentile)g�������?rz63upper left)�titleZbbox_to_anchor�locuo64             It can be seen that across each building use category, electricity usage generally increases as building area gets larger, as observed in buildings used for Education and Lodging/Residential. Education buildings show the steepest difference between the Min–25% and 75%–Max percentile ranges, with approximately 4x higher consumption in the latter quartile. This steep increase may be driven by high-powered equipment such as laboratories, lecture halls, and HVAC systems.65 66For Lodging/Residential buildings, they have the lowest absolute average electricity usage (kWh), but the increase in electricity consumption across building area percentiles is the most gradual and consistent. This suggests that building area is a reliable predictor of electricity usage for this building type.67 68For Office buildings, it can be observed that electricity usage does not follow a consistent upward trend. There is a notable decrease in average electricity usage from the 50%–75% quartile to the 75%–Max quartile. This means that once the building area exceeds approximately 91,149 sqft (75th percentile), electricity usage begins to decline. A possible explanation is that not all areas within larger buildings are frequently occupied, resulting in minimal electricity usage in rarely used spaces. Additionally, larger buildings tend to adopt more efficient energy management systems and are more likely to hold green building certifications, featuring low-watt appliances and heat exchange systems. These factors may explain the decrease in electricity usage beyond a certain building area.69 70For Entertainment/Public Assembly and Public Service buildings, the available data is insufficient to draw meaningful conclusions about the differences between each area percentile. These gaps are likely due to very few buildings of these types falling into each area percentile group.71             zU#### E. EDA: Effect of Building Age (Year Built) to Energy Usage (Meter Reading (kWh)�72Year Builti�i�)�73r)�datarzPre-Energy Code (Before 1981)zEnergy demand for electricity�bold)r)Z74fontweightzPost-Energy Code (After 1980)zEnergy demand for hot wateruR	75    The energy data can be analyzed based on era category, specifically Pre-Energy Code (1900–1980) and Post-Energy Code (1981–2007). These categories were chosen because during the Pre-Energy Code era there were no formal energy standards in buildings, which typically resulted in poor insulation and inefficient systems. As for the Post-Energy Code era, this is when the first energy regulation was implemented (ASHRAE 90-1975), triggered by the energy crisis, and where HVAC systems were introduced alongside better insulation and more efficient building systems.76 77    From the data, it can be seen that buildings built before 1981 have a higher average energy demand (meter reading) of 184.87 kWh compared to buildings built after 1980, which average 174.55 kWh. To determine whether this difference is statistically significant, a two-sample, two-sided test will be conducted using two independent samples — buildings built before 1981 and buildings built after 1980.78 79    The two-sided approach is chosen because we are only interested in whether a difference in meter readings (kWh) exists between the two groups, without considering the direction of the effect. Prior to this, a Spearman correlation test will be used to examine the relationship between the two categories. The Spearman method is chosen because the relationship between the data may be monotonic rather than strictly linear, yet still moves in one consistent direction. Additionally, this method is robust to outliers, making it well-suited for our dataset.80             81    From the hypothesis test, The p-value of 0.0031 is lower than 0.05, therefore the null hypothesis is rejected. This means that there is a statistically significant difference in energy usage/meter readings (kWh) between buildings built before 1981 and those built after 1980. Specifically, buildings built before 1981 have a higher average energy demand compared to those built after 1980.82    Some reasons that may explain this finding include the fact that older buildings constructed before 1981 generally have poor insulation, inefficient systems, and were built without energy regulations to govern their energy consumption. This could explain why energy usage in buildings from the pre-energy code era tends to be higher than that of more recently constructed buildings from the post-energy code era.        83             zG#### F. EDA: Effect of Meter Type to Energy Usage (Meter Reading) (kWh)�zEnergy type: ElectricityzEnergy type: Hot wateru�8485        From the data, it can be seen that electricity has a higher average energy demand (meter reading) of 161.97 kWh compared to hot water, which has an average energy demand of 91.50 kWh. To determine whether this difference is statistically significant, a two-sample, two-sided test will be conducted using two independent samples — buildings that use electricity and buildings that use hot water as their meter type.86 87        The two-sided approach is chosen because we are only interested in whether a difference in meter readings (kWh) exists between the two groups, without considering the direction of the effect. Prior to this, a Spearman correlation test will be used to examine the relationship between the two categories. The Spearman method is chosen because the relationship between the data may be monotonic rather than strictly linear, yet still moves in one consistent direction. Additionally, this method is robust to outliers, making it well-suited for our dataset.88 89        The p-value of **8.37 × 10⁻¹⁰⁷** is lower than 0.05, therefore the null hypothesis is rejected. This means that there is a statistically significant difference in energy usage/meter readings between buildings that use electricity and those that use hot water as their meter type. Specifically, buildings that use electricity as their energy meter type have a higher average meter reading/energy usage (kWh) compared to buildings that use hot water.90        91        Several reasons may explain this finding. Electricity supports a broader range of uses compared to hot water, as buildings powered by electricity often consume energy not just for heating, but also for cooling (air conditioning), lighting, appliances, plug loads, and ventilation systems. This results in a much higher cumulative energy demand compared to buildings that rely solely on hot water, which is typically used only for space heating and domestic hot water supply. Furthermore, when electricity is used for heating purposes, electric resistance systems are generally less efficient than hot water-based heating systems, resulting in greater energy (kWh) consumption. In addition, electrically-metered buildings often maintain a constant baseline load from systems such as servers, security systems, elevators, and refrigeration, contributing to a continuous 24/7 energy demand. Lastly, electricity-based energy systems operate throughout all seasons, with electrically-metered buildings facing high demand in both summer (cooling) and winter (heating), creating a year-round high energy profile — whereas hot water systems primarily experience demand spikes only during colder months. All of these factors contribute to an overall higher energy consumption (kWh) in electrically-metered buildings.92             zG#### G. EDA: Effect of Meter Type to Energy Usage (Meter Reading) (kWh))�r)�ID�Building IDrr	r0r93rz94Time Stamprrr
rrrr)Znumeric_onlyz.2f)�annot�fmtzCorrelation Matrixa�95        From the heatmap above, we can see that energy demand/meter readings (kWh) correlates most strongly with building area (0.55) and floor count (0.33). This makes sense because the larger the building area and the more floors there are to cover, the greater the demand on electrical systems, including lighting, heating/cooling, and other building services, this drives the overall energy consumption higher.     96             )��g�������?�	steelblue�w)�alpha�colorZ97edgecolors�	linewidthz+Building Area (sqft) vs Meter Reading (kWh)c�g|]}�|��qS�rB��.0r+��b�mrB�>D:\Workspace\Phase1\M2-Energy-Demand-Predictor\./src\m2_eda.py�98<listcomp>�zrun.<locals>.<listcomp>�redz99Trend Line)r?r@�labelr100z"Floor Count vs Meter Reading (kWh)crArBrBrCrErBrHrI$rJuV101             From the heatmap above, we can see that energy demand/meter readings (kWh) correlates most strongly with building area (0.55) and floor count (0.33). This makes sense because the larger the building area and the more floors there are to cover, the greater the demand on electrical systems — including lighting, heating/cooling, and other building services — driving overall energy consumption higher. However, these correlations will need to be validated further during feature engineering, as this heatmap serves only as a preliminary overview of what to expect in the subsequent analysis. The scatter plots visualises the correlation between meter readings and building area, floor count, and air temperature respectively. It can be seen that all three show a positive correlation, further supporting the findings above.102             z=#### H. Interactive Plot: Distribution vs Meter Reading (kWh)zChoose Column: )r7r	r0r103rrr
rrrr)r5r�)rZkderr?zDistribution of g333333�?z vs Meter Reading (kWh)crArBrBrCrErBrHrIIrJ)0�str.�	subheader�mpingZimread�image�write�pd�read_csv�	dataframe�agg�pltZsubplots�value_counts�plot�lenZtight_layout�pyplot�quantile�tableZdescribe�to_frameZcutZnotna�groupbyr�reset_index�rename�round�pivot�	set_titleZ104set_xlabelZ105set_ylabelZtick_params�legend�snsZboxplotZ106set_xticksZset_xticklabels�heatmapZcorr�scatter�npZpolyfit�sorted�	selectboxZhistplotZdropna)r2�dfZfig_1rZbuilding_use_countsZ	area_binsZarea_labelsZdf_electricityZarea_percentile_analysisZpivot_tableZfig_2Zenergy_before_1981Zenergy_after_1980Zfig_3Zenergy_electricityZenergy_hot_waterZfig_4Zfig_5Zfig_6Zaxes�optionZfig_7Zdf_cleanrBrErH�runsX107108109110111112113114115116117118119�120
121122123124
�125
126�127128�129��130����131�132133134135136 137138139140141142143 144��145146"�"�147148149"�rn�__main__)�	streamlitrN�pandasrS�seabornrfZplotly.express�expressZpx�matplotlib.pyplotr[rWZmatplotlib.imagerQrP�numpyrirn�__name__rBrBrBrH�<module>sH150�