Stable operation of electrical power grid requires balanced supply and demand. But, due to the increasing fraction of renewable energy sources strong fluctuations occur in power generation. Here, the major challenge is to regulate the consumer demand in order to match the fluctuating power generation. To achieve this goal, Decentral Smart Grid Control (DSGC) concept is introduced which suggests real-time pricing. In this, the consumer demand data is collected, then given the current power supply it is centrally evaluated and sends the price information back to customers for them to decide about usage.
Here, the objective is to build a model that can forecast the stability of the power grid. The local stability of the four-node star electrical grid with centralized production is analyzed by implementing Decentral Smart Grid Control concept.
Data Source: Electrical Grid Stability Simulated Dataset
Before we dive deeper into the coding part, let us briefly discuss about the popular python libraries which we will be using throughout.
Python libraries are a collection of functions and methods that allows us to perform many actions without writing the code.
NumPy: NumPy is a very popular python library for large multi-dimensional array and matrix processing, with the help of a large collection of high-level mathematical functions. It is very useful for fundamental scientific computations in Machine Learning.
Pandas: Pandas is a popular Python library for data analysis. Pandas is developed specifically for data extraction and preparation.
Matplotlib: Matplotlib is a very popular Python library for data visualization. It provides various kinds of graphs and plots for data visualization, viz., histogram, error charts, bar charts, etc.
Scikit-learn: Scikit-learn is one of the most popular ML libraries for classical ML algorithms. Scikit-learn supports most of the supervised and unsupervised learning algorithms. Scikit-learn can also be used for data-mining and data-analysis, which makes it a great tool who is starting out with ML.
Tensorflow and Keras: TensorFlow is an end-to-end open source platform for machine learning. It has a comprehensive, flexible ecosystem of tools, libraries and community resources that lets researchers push the state-of-the-art in ML and developers easily build and deploy ML powered applications.
Keras is an open-source neural-network library written in Python. It is capable of running on top of TensorFlow, Microsoft Cognitive Toolkit, R, Theano, or PlaidML. Designed to enable fast experimentation with deep neural networks, it focuses on being user-friendly, modular, and extensible.
import pandas as pd import numpy as np import matplotlib.pyplot as plt import seaborn as sns %matplotlib inline
Load the Dataset
# Reading a csv file and returning the first 10 rows of the dataframe.
Exploratory Data Analysis
Exploratory data analysis (EDA) is an approach to analyzing data sets to summarize their main characteristics, often with visual methods.
# Summary of the DataFrame.
# Checking the presence of missing values in each column.
# Descriptive statistics.
# The counts of observations in each category.
print(df['stabf'].value_counts()) sns.set_style('whitegrid') sns.countplot(x='stabf',data=df, palette='YlGnBu_r')
# Distribution of observations in column 'stab'.
plt.figure(figsize=(8,4)) sns.distplot(df['stab'], color='r')
Correlation states how the features are related to each other or the target variable. Correlation can be positive (increase in one value of feature increases the value of the target variable) or negative (increase in one value of feature decreases the value of the target variable).
# Correlation Heatmap
Train Test Split
It splits the dataset into a training set and a test set. Here, we split the dataset in the ratio 80:20 to create training and testing subsets, respectively.
X = df.drop(['stab', 'stabf'],axis=1) y = df['stab'] from sklearn.model_selection import train_test_split X_train, X_test, y_train, y_test = train_test_split(X,y,test_size=0.2,random_state=42)
Feature scaling is a method used to normalize the values of independent variables in a fixed range.
import joblib from joblib import dump, load from sklearn.preprocessing import StandardScaler scaler = StandardScaler() X_train= scaler.fit_transform(X_train) X_test = scaler.transform(X_test) joblib.dump(scaler, 'Scaler.joblib')
Build the Model
from tensorflow.keras.models import Sequential from tensorflow.keras.layers import Dense from tensorflow.keras.optimizers import Adam from tensorflow.keras.layers import Dropout
# Returns the number of dimensions in the array.
# Building the Model.
The "exponential linear unit" (ELU) is used as an activation function as it has negative values which allows it to push mean unit activations closer to zero. ELU speeds up learning in deep neural networks and leads to higher classification accuracies.
model = Sequential()model.add(Dense(12, activation='elu'))model.add(Dropout(0.5)) model.add(Dense(3,activation='elu')) model.add(Dense(1)) model.compile(optimizer='adam', loss='mse')
Early Stopping is a technique that terminates training when a monitored metric stops improving.
# This callback will stop the training when there is no improvement in the validation loss.
from tensorflow.keras.callbacks import EarlyStoppingearly_stop = EarlyStopping(monitor='val_loss', mode='min', verbose=1, patience=10)
Fraction of the training data is used as validation data. The model will set apart this fraction (10%) of the training data, will not train on it, and will evaluate the loss on this data at the end of each epoch. It selects the first 90% as train data and the remaining 10% as validation data.
# Save the model.
Training loss Vs. Validation loss
losses = pd.DataFrame(model.history.history)losses.plot()
Model Evaluation in Regression
from sklearn.metrics import mean_squared_error,mean_absolute_error predictions = model.predict(X_test)
There are 3 main metrics for model evaluation in regression:
Mean Absolute Error
Mean Squared Error
The mean squared error (MSE) measures the average squared difference between the estimated values and the actual value.
Root Mean Square Error (RMSE)
Electrical Grid Stability Prediction and Classification
from tensorflow.keras.models import load_modelmodel_grid = load_model('Electrical_Grid_Stability.h5') scaler=load('Scaler.joblib')
from sklearn.preprocessing import StandardScaler def foo(t1, t2, t3, t4, p1, p2, p3, p4, g1, g2, g3, g4): X_test = scaler.transform([[t1, t2, t3, t4, p1, p2, p3, p4, g1, g2, g3, g4]]) prediction = model_grid.predict(X_test) print(prediction) if prediction>=0: return "Oops! the system is linearly unstable." else: return "Great! the system is stable."
Note: The entire end to end code can be downloaded from, https://github.com/Harshita9511/Electrical-Grid-Stability-Predictor and the web application can be accessed by the given link - https://electrical-grid-stability.herokuapp.com/