Predicting Building Energy Efficiency (Supervised Learning)

Asad Mujeeb
2 min readApr 11, 2024

You are working for an architecture firm, and your task is to build a model that predicts the energy efficiency rating of buildings based on features like wall area, roof area, overall height, etc.

# Import necessary libraries
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
import warnings
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestRegressor
from sklearn.metrics import mean_squared_error

warnings.filterwarnings('ignore')
# Generate synthetic dataset for building features and energy efficiency ratings
np.random.seed(0)
data_size = 500
data = {
'WallArea': np.random.randint(200, 400, data_size),
'RoofArea': np.random.randint(100, 200, data_size),
'OverallHeight': np.random.uniform(3, 10, data_size),
'GlazingArea': np.random.uniform(0, 1, data_size),
'EnergyEfficiency': np.random.uniform(10, 50, data_size) # Energy efficiency rating
}
df = pd.DataFrame(data)
# Data preprocessing
X = df.drop('EnergyEfficiency', axis=1)
y = df['EnergyEfficiency']

# Visualize the relationships between features and the target variable (Energy Efficiency)
sns.pairplot(df, x_vars=['WallArea', 'RoofArea', 'OverallHeight', 'GlazingArea']
, y_vars='EnergyEfficiency', height=4, aspect=1, kind='scatter')
plt.show()
# Split the data into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

# Train a Random Forest model
model = RandomForestRegressor()
model.fit(X_train, y_train)

# Predict and evaluate
predictions = model.predict(X_test)
mse = mean_squared_error(y_test, predictions)
print(f"Mean Squared Error: {mse}")
# Plot the True values vs Predicted values
plt.figure(figsize=(10, 6))
plt.scatter(y_test, predictions)
plt.xlabel("True Values")
plt.ylabel("Predictions")
plt.title("True Values vs Predicted Values")
plt.plot([y_test.min(), y_test.max()], [y_test.min(), y_test.max()], 'k--')
plt.show()

Output :

This is a comprehensive code to understand the supervised machine learning model using regression

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

Asad Mujeeb
Asad Mujeeb

Written by Asad Mujeeb

0 Followers

Proficient in machine learning algorithms, statistical analysis, and data visualization techniques

No responses yet

Write a response

Recommended from Medium

Lists

See more recommendations