
Appendix B
Google Colab code for the neural network training
%pip uninstall tensorflow
%pip install tensorflow==2.4.0
from google.colab import files
uploaded = files.upload()
import pandas as pd
import numpy as np
import tensorflow as tf
from sklearn.preprocessing import StandardScaler, MinMaxScaler
from sklearn.model_selection import train_test_split , StratifiedShuffleSplit
import os
import random
from sklearn.metrics import confusion_matrix
import seaborn as sn
import matplotlib.pyplot as plt
os.environ['PYTHONHASHSEED']=str(1)
tf.random.set_seed(2021)
np.random.seed(2021)
random.seed(2021)
Y_LABELS={'P':0,'N':1,'B':2,'S':3}
TIMESERIES_LEN = 50
def trip_framing(trip,label,frame_size,db_x,db_y):
a = np.array( trip )
for i in np.arange( 0, a.shape[0]-frame_size, frame_size ):
x = a[i:i+frame_size]
db_x.append( x )
db_y.append( Y_LABELS[label] )
db = pd.read_csv('Diff_profile.csv',sep=',')
print(db.keys())
states = db['Status'].value_counts()
scaler = MinMaxScaler()
ts_status = db.Status
ts_diff_Ax = (db.Acc_x.to_numpy().reshape(-1,1))/9.81
ts_diff_Ay = (db.Acc_y.to_numpy().reshape(-1,1))/9.81
ts_diff_Az = (db.Acc_z.to_numpy().reshape(-1,1))/9.81
ts_time = db['Time']
rows = ts_status.shape[0]
db_x = []
db_y = []
for states_id in states.keys():
trip = []
cnt = 0
for i in range(rows):
if ts_time[i] == 100:
if len(trip) > 0:
trip_framing( trip, states_id, TIMESERIES_LEN, db_x, db_y)
trip=[]
if ts_status[i] == states_id and cnt < 7500:
trip.append( [ts_diff_Ax[i],ts_diff_Ay[i],ts_diff_Az[i]] )
cnt += 1
x_train, x_test, y_train, y_test = train_test_split(db_x, db_y, test_size=0.4,
random_state=21,stratify=db_y)
x_train = np.asarray(x_train)[:,:,:,0]
x_test = np.asarray(x_test)[:,:,:,0]
y_train = np.asarray( y_train )
y_test = np.asarray( y_test )
db_stats = pd.Series( y_test )
UM3053
Google Colab code for the neural network training
UM3053
-
Rev 1
page 31/39