-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInfosys AI internship final project.py
286 lines (234 loc) · 9.84 KB
/
Infosys AI internship final project.py
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
# Import necessary libraries
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.ensemble import RandomForestRegressor
from sklearn.metrics import mean_absolute_error, mean_squared_error, r2_score
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, LSTM, Dropout
from tensorflow.keras.callbacks import EarlyStopping
import xgboost as xgb
import gradio as gr
# Set random seed for reproducibility
np.random.seed(42)
def load_and_preprocess_data(file_path):
"""
Load and preprocess the data from a CSV file.
Args:
file_path (str): Path to the CSV file.
Returns:
pd.DataFrame: Preprocessed DataFrame.
"""
# Load the data
df = pd.read_csv(file_path)
# Filter for India and select relevant columns
df = df[df['country'] == 'India']
selected_columns = [
'year', 'population', 'gdp', 'biofuel_electricity', 'coal_electricity',
'gas_electricity', 'hydro_electricity', 'nuclear_electricity', 'oil_electricity',
'other_renewable_electricity', 'solar_electricity', 'wind_electricity',
'biofuel_elec_per_capita', 'coal_elec_per_capita', 'gas_elec_per_capita',
'hydro_elec_per_capita', 'nuclear_elec_per_capita', 'oil_elec_per_capita',
'other_renewables_elec_per_capita', 'solar_elec_per_capita', 'wind_elec_per_capita'
]
df = df[selected_columns]
# Filter for years >= 1990
df = df[df['year'] >= 1990]
# Fill missing values
df['biofuel_elec_per_capita'] = df['biofuel_elec_per_capita'].fillna(0)
df['biofuel_electricity'] = df['biofuel_electricity'].fillna(0)
if 'population' in df.columns:
df['population'] = df['population'].fillna(df['population'].mean())
else:
print("Warning: 'population' column is missing from the dataset.")
# Create new features
df['total_electricity_production'] = df[[col for col in df.columns if col.endswith('_electricity')]].sum(axis=1)
df['total_electricity_consumption'] = df[[col for col in df.columns if col.endswith('_per_capita')]].sum(axis=1)
return df.reset_index(drop=True)
def plot_electricity_consumption(df):
"""
Plot the total electricity consumption over time.
Args:
df (pd.DataFrame): Preprocessed DataFrame.
"""
plt.figure(figsize=(12, 6))
plt.plot(df['year'], df['total_electricity_consumption'])
plt.title('Total Electricity Consumption in India (1990-2020)')
plt.xlabel('Year')
plt.ylabel('Total Electricity Consumed (TWh)')
plt.grid(True)
plt.show()
def prepare_data_for_modeling(df):
"""
Prepare the data for modeling by splitting into features and target,
and performing train-test split.
Args:
df (pd.DataFrame): Preprocessed DataFrame.
Returns:
tuple: X_train, X_test, y_train, y_test, scaler
"""
features = df.drop(columns=['total_electricity_consumption'])
target = df['total_electricity_consumption'].values.reshape(-1, 1)
X_train, X_test, y_train, y_test = train_test_split(features, target, test_size=0.2, random_state=42)
scaler_X = StandardScaler()
scaler_y = StandardScaler()
X_train_scaled = scaler_X.fit_transform(X_train)
X_test_scaled = scaler_X.transform(X_test)
y_train_scaled = scaler_y.fit_transform(y_train)
y_test_scaled = scaler_y.transform(y_test)
return X_train_scaled, X_test_scaled, y_train_scaled, y_test_scaled, scaler_X, scaler_y
def build_lstm_model(input_shape):
"""
Build and compile an LSTM model.
Args:
input_shape (tuple): Shape of the input data.
Returns:
tensorflow.keras.models.Sequential: Compiled LSTM model.
"""
model = Sequential([
LSTM(100, activation='relu', input_shape=input_shape, return_sequences=True),
Dropout(0.2),
LSTM(50, activation='relu'),
Dense(25, activation='relu'),
Dense(1)
])
model.compile(optimizer='adam', loss='mse')
return model
def train_lstm_model(X_train, y_train):
"""
Train the LSTM model.
Args:
X_train (np.array): Training features.
y_train (np.array): Training target.
Returns:
tensorflow.keras.models.Sequential: Trained LSTM model.
"""
X_train_reshaped = X_train.reshape((X_train.shape[0], 1, X_train.shape[1]))
model = build_lstm_model((1, X_train.shape[1]))
early_stopping = EarlyStopping(monitor='val_loss', patience=10, restore_best_weights=True)
model.fit(X_train_reshaped, y_train, epochs=100, batch_size=32, validation_split=0.2,
callbacks=[early_stopping], verbose=0)
return model
def train_random_forest(X_train, y_train):
"""
Train a Random Forest model.
Args:
X_train (np.array): Training features.
y_train (np.array): Training target.
Returns:
RandomForestRegressor: Trained Random Forest model.
"""
rf_model = RandomForestRegressor(n_estimators=100, random_state=42)
rf_model.fit(X_train, y_train)
return rf_model
def train_xgboost(X_train, y_train):
"""
Train an XGBoost model.
Args:
X_train (np.array): Training features.
y_train (np.array): Training target.
Returns:
xgboost.XGBRegressor: Trained XGBoost model.
"""
xgb_model = xgb.XGBRegressor(n_estimators=100, learning_rate=0.1, random_state=42)
xgb_model.fit(X_train, y_train)
return xgb_model
def evaluate_model(model, X_test, y_test, scaler_y, model_name):
"""
Evaluate the model and print performance metrics.
Args:
model: Trained model (LSTM, Random Forest, or XGBoost).
X_test (np.array): Test features.
y_test (np.array): Test target.
model_name (str): Name of the model being evaluated.
"""
if model_name == 'LSTM':
X_test_reshaped = X_test.reshape((X_test.shape[0], 1, X_test.shape[1]))
y_pred_scaled = model.predict(X_test_reshaped).flatten()
else:
y_pred_scaled = model.predict(X_test)
y_pred = scaler_y.inverse_transform(y_pred_scaled.reshape(-1, 1)).flatten()
y_true = scaler_y.inverse_transform(y_test).flatten()
mae = mean_absolute_error(y_true, y_pred)
mse = mean_squared_error(y_true, y_pred)
r2 = r2_score(y_true, y_pred)
print(f"{model_name} Model Performance:")
print(f"Mean Absolute Error: {mae:.2f}")
print(f"Mean Squared Error: {mse:.2f}")
print(f"R2 Score: {r2:.2f}\n")
def predict_energy_consumption(model, scaler_X, scaler_y, input_data):
"""
Predict energy consumption using the trained model.
Args:
model: Trained model (LSTM, Random Forest, or XGBoost).
scaler (StandardScaler): Fitted scaler for feature normalization.
input_data (dict): Dictionary containing input features.
Returns:
float: Predicted total electricity consumption.
"""
input_df = pd.DataFrame([input_data])
input_scaled = scaler_X.transform(input_df)
if isinstance(model, Sequential):
input_reshaped = input_scaled.reshape((1, 1, input_scaled.shape[1]))
prediction_scaled = model.predict(input_reshaped)
else:
prediction_scaled = model.predict(input_scaled.reshape(1, -1))
prediction = scaler_y.inverse_transform(prediction_scaled)
return prediction[0][0]
def main():
# Load and preprocess data
df = load_and_preprocess_data('energy_data.csv')
# Plot electricity consumption
plot_electricity_consumption(df)
# Prepare data for modeling
X_train, X_test, y_train, y_test, scaler_X, scaler_y = prepare_data_for_modeling(df)
# Train and evaluate LSTM model
lstm_model = train_lstm_model(X_train, y_train)
evaluate_model(lstm_model, X_test, y_test, scaler_y, 'LSTM')
# Train and evaluate Random Forest model
rf_model = train_random_forest(X_train, y_train)
evaluate_model(rf_model, X_test, y_test, scaler_y, 'Random Forest')
# Train and evaluate XGBoost model
xgb_model = train_xgboost(X_train, y_train)
evaluate_model(xgb_model, X_test, y_test, scaler_y, 'XGBoost')
# Create Gradio interface
input_components = [
gr.Number(label="Year"),
gr.Number(label="Population"),
gr.Number(label="GDP"),
gr.Number(label="Biofuel Electricity"),
gr.Number(label="Coal Electricity"),
gr.Number(label="Gas Electricity"),
gr.Number(label="Hydro Electricity"),
gr.Number(label="Nuclear Electricity"),
gr.Number(label="Oil Electricity"),
gr.Number(label="Other Renewable Electricity"),
gr.Number(label="Solar Electricity"),
gr.Number(label="Wind Electricity"),
gr.Number(label="Biofuel Electricity Per Capita"),
gr.Number(label="Coal Electricity Per Capita"),
gr.Number(label="Gas Electricity Per Capita"),
gr.Number(label="Hydro Electricity Per Capita"),
gr.Number(label="Nuclear Electricity Per Capita"),
gr.Number(label="Oil Electricity Per Capita"),
gr.Number(label="Other Renewables Electricity Per Capita"),
gr.Number(label="Solar Electricity Per Capita"),
gr.Number(label="Wind Electricity Per Capita")
]
output = gr.Number(label="Predicted Total Electricity Consumption")
description = """
This Energy Consumption Forecasting System predicts electricity consumption based on various factors.
Input the required information, and the model will provide a forecast of total electricity consumption.
"""
gr.Interface(
fn=lambda *args: predict_energy_consumption(lstm_model, scaler_X, scaler_y, dict(zip(df.columns[:-1], args))),
inputs=input_components,
outputs=output,
title="Energy Consumption Forecasting System",
description=description
).launch()
if __name__ == "__main__":
main()