<jupyter_start><jupyter_text>Power Quality Classification using CNN This notebook focusses on developing a Convolutional Neural Network which classifies a particular power signal into its respective power quality condition. The dataset used here contains signals which belong to one of the 5 classes(power quality condition). The sampling rate of this data is 128. This means that each signal is characterized by 128 data points. Here the signals provided are in time domain.The power quality condition with respect to the output class value is as follows: 1 - Normal2 - 3rd harmonic wave3 - 5th harmonic wave4 - Voltage dip5 - transient<jupyter_code>#importing the required libraries
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
import datetime
import visualkeras
from scipy.fft import fft,fftfreq
from sklearn.preprocessing import StandardScaler
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Dropout, Activation
from tensorflow.keras.optimizers import Adam
#loading the dataset using pandas
data1 = pd.read_csv("../Dataset/Train/Voltage_L1_DataSet1.csv")
out1 = pd.read_csv("../Dataset/Train/OutputFor_DataSet1.csv")
data2 = pd.read_csv("../Dataset/Test/Voltage_L1_DataSet2.csv")
out2 = pd.read_csv("../Dataset/Test/OutputFor_DataSet2.csv")
print("data1",data1.shape)
print("out1",out1.shape)
print("data2",data2.shape)
print("out2",out2.shape)<jupyter_output>data1 (11899, 128)
out1 (5999, 1)
data2 (5999, 128)
out2 (5999, 1)<jupyter_text>Data Preprocessing This segment of notebook contains all the preprocessing steps which are performed on the data.  Data cleaning<jupyter_code>#dropna() function is used to remove all those rows which contains NA values
data1.dropna(axis=0,inplace=True)
#shape of the data frame after dropping the rows containing NA values
data1.shape
#here we are constructing the array which will finally contain the column names
header =[]
for i in range(1,data1.shape[1]+1):
    header.append("Col"+str(i))
#assigning the column name array to the respectinve dataframes
data1.columns = header
data2.columns = header
data1.head()
data2.head()
#now we are combining the two dataframes to make a final dataframe
data = data1.append(data2, ignore_index = True)
data.head()
data.shape
#here we are giving a name to the output column
header_out = ["output"]
out1.columns = header_out
out2.columns = header_out
out2.head()
#now we are combining the output columns
output = out1.append(out2, ignore_index = True)
output.head()
output.shape
#now we are appending the output column to the original dataframe which contains the power signals
data['output'] = output
data.head()
data_arr = data.to_numpy()<jupyter_output><empty_output><jupyter_text>Data transformation The data transformation steps employed here are as follows:1) Fourier Transform2) Normalization<jupyter_code>#In this segment we are plotting one wave from each class after applying fourier transformation 
yf = np.abs(fft(data_arr[0][0:128]))
xf = fftfreq(128,1/128)
plt.plot(xf, yf)
plt.show()
print("class",data_arr[0][128], "Normal wave")

yf = np.abs(fft(data_arr[1][0:128]))
xf = fftfreq(128,1/128)
plt.plot(xf, yf)
plt.show()
print("class",data_arr[1][128], "3rd harmonic wave")

yf = np.abs(fft(data_arr[3][0:128]))
xf = fftfreq(128,1/128)
plt.plot(xf, yf)
plt.show()
print("class",data_arr[3][128], "5th harmonic wave")

yf = np.abs(fft(data_arr[6][0:128]))
xf = fftfreq(128,1/128)
plt.plot(xf, yf)
plt.show()
print("class",data_arr[6][128], "Voltage dip")

yf = np.abs(fft(data_arr[8][0:128]))
xf = fftfreq(128,1/128)
plt.plot(xf, yf)
plt.show()
print("class",data_arr[8][128], "Transient wave")
#here we are splitting the dataset in the ratio of 60%,20%,20% (training set,validation set, test set)
from sklearn.model_selection import train_test_split
x_train, x_test, y_train, y_test = train_test_split(data.loc[:,data.columns != 'output'],data['output'],test_size=0.2)
x_train, x_val, y_train, y_val = train_test_split(x_train, y_train, test_size=0.25, random_state=42)
#here we are overwritting the dataframe with the waves which we obtained after doing fourier transformation
x_train = x_train.to_numpy()
x_test = x_test.to_numpy()
x_val = x_val.to_numpy()
#here we are performing normalization
transform = StandardScaler()
x_train_tr = transform.fit_transform(x_train)
x_test_tr = transform.fit_transform(x_test)
x_val_tr = transform.fit_transform(x_val)<jupyter_output><empty_output><jupyter_text>Model creation and training<jupyter_code># get_dummies function is used here to perform one hot encoding of the y_* numpy arrays
y_train_hot = pd.get_dummies(y_train)
y_test_hot = pd.get_dummies(y_test)
y_val_hot = pd.get_dummies(y_val)
print("Training",x_train.shape)
print(y_train_hot.shape)
print("Validation",x_val.shape)
print(y_val_hot.shape)
print("Test",x_test.shape)
print(y_test_hot.shape)
#Reshaping the Data so that it could be used in 1D CNN
x_train_re = x_train.reshape(x_train_tr.shape[0],x_train_tr.shape[1], 1)
x_test_re = x_test.reshape(x_test_tr.shape[0],x_test_tr.shape[1], 1)
x_val_re = x_val.reshape(x_val_tr.shape[0],x_val_tr.shape[1], 1)
x_train_re.shape
#importing required modules for working with CNN
import tensorflow as tf
from tensorflow.keras.layers import Conv1D
from tensorflow.keras.layers import Convolution1D, ZeroPadding1D, MaxPooling1D, BatchNormalization, Activation, Dropout, Flatten, Dense
from tensorflow.keras.regularizers import l2
#initializing required parameters for the model
batch_size = 64
num_classes = 5
epochs = 20
input_shape=(x_train.shape[1], 1)
model = Sequential()

model.add(Conv1D(128, kernel_size=3,padding = 'same',activation='relu', input_shape=input_shape))
model.add(BatchNormalization())
model.add(MaxPooling1D(pool_size=(2)))

model.add(Conv1D(128,kernel_size=3,padding = 'same', activation='relu'))
model.add(BatchNormalization())
model.add(MaxPooling1D(pool_size=(2)))

model.add(Flatten())
model.add(Dense(16, activation='relu'))
model.add(Dense(num_classes, activation='softmax'))
model.summary()
#compiling the model

log_dir = "logs1/fit/" + datetime.datetime.now().strftime("%Y%m%d-%H%M%S")
tensorboard_callback = tf.keras.callbacks.TensorBoard(log_dir=log_dir, histogram_freq=1)

model.compile(loss=tf.keras.losses.categorical_crossentropy,
              optimizer='adam',
              metrics=['accuracy'])
#training the model
history = model.fit(x_train_re, y_train_hot, batch_size=batch_size, epochs=epochs, validation_data=(x_val_re, y_val_hot), callbacks=[tensorboard_callback])

%load_ext tensorboard
%tensorboard --logdir logs1/fit
print(model.metrics_names)<jupyter_output>['loss', 'accuracy']<jupyter_text>Model evaluation<jupyter_code>np.mean(history.history['accuracy']) 
pred_acc = model.evaluate(x_test_re,y_test_hot)
print("Test accuracy is {}".format(pred_acc))
#model.save('CNN_model_data1.h5')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11592097617664149, 'fraction_numerical': 0.031812899477048226, 'mean_word_length': 6.868571428571428, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '202752', 'license': 'MIT', 'n_tokens_mistral': 2485, 'n_tokens_neox': 2365, 'n_words': 610, 'path': 'CNN/CNN_implementation_1.ipynb', 'repo_name': 'aswarth123/Amrita_Honeywell_Hackathon', 'size': '6884'}