<jupyter_start><jupyter_text>Data Processing Aruba<jupyter_code>import pandas as pd
import numpy
path = "/content/drive/My Drive/IP Data/data"
f = open(path)
f = f.read().split("\n")
st = "Meal_Preparation Relax Eating Work Sleeping Wash_Dishes Bed_to_Toilet Enter_Home Leave_Home Housekeeping Resperate".split(" ")
data = []
for i in range(len(f)):
    dataExample = f[i]
    
    a = dataExample.replace('\t', ' ').replace('\n', ' ').split(" ")
    ar = []
    for i in a:
        if i:
            ar.append(i)
    if len(ar) == 4:
        ar.append("NoAction")
    elif len(ar)>4:
        temp = " ".join(ar[4:])
        ar = ar[:4] + [temp]
    else:
        print(i,dataExample)
    
    if ar != []:
        data.append(ar)
loc = {
       "T001":"Bedroom","T002":"Living","T003":"Kitchen","T004":"Closet","T005":"Office",
       "M001":"Bedroom","M002":"Bedroom","M003":"Bedroom","M004":"Bathroom","M005":"Bedroom",
       "M006":"Bedroom","M007":"Bedroom","M008":"Living","M009":"Living","M010":"Living",
       "M011":"FrontDoor","M012":"Living","M013":"Living","M014":"Dining","M015":"Kitchen",
       "M016":"BackDoor","M017":"Kitchen","M018":"Kitchen","M019":"Kitchen","M020":"Living",
       "M021":"Path","M022":"Path","M023":"Bedroom","M024":"Bedroom","M025":"Office",
       "M026":"Office","M027":"Office","M028":"Office","M029":"Bathroom","M030":"GarageDoor",
       "M031":"Closet","D001":"FrontDoor","D002":"BackDoor","D003":"Closet","D004":"GarageDoor"  
      }
for i in range(len(data)):
    if data[i][3] == "ON":
        data[i].append(loc[data[i][2]])
    else:
        data[i].append(data[i-1][5])
DataNew = []
for i in data:
    if i[4] != "NoAction":
        DataNew.append([i[0],i[1],i[5],i[4]])
data = DataNew
data = numpy.array(DataNew)
data = pd.DataFrame(data)
data.to_csv("DataAruba.csv", index=False, header=False)<jupyter_output><empty_output><jupyter_text>Action Model Aruba<jupyter_code>import numpy
import pandas as pd
import keras

import tensorflow as tf
from tensorflow.keras.layers import *
from tensorflow.keras.optimizers import *
from tensorflow.keras.utils import to_categorical
from tensorflow.keras.callbacks import EarlyStopping
from tensorflow.keras.callbacks import ModelCheckpoint
from tensorflow.keras.models import Sequential, Model, load_model
from sklearn.metrics import classification_report, f1_score
from tensorflow.keras import backend as K
def to_one(arr):
  vals = list(set(arr))
  ar = []
  for i in range(len(arr)):
    ar.append(vals.index(arr[i]))
  return to_categorical(ar),vals
d = pd.read_csv("DataAruba.csv", header=None).to_numpy()
data = []
day = []
time = []
hour = []
minute = []
loct = []
prevAct = []
y = []
for i in range(1,len(d)):
  if d[i][3] != "NoAction":
    data.append( [d[i][1], d[i][2][:5], d[i][3], d[i-1][4]] )
    t = d[i][2][:5]
    if t[-1] == ":":
      t = t[:-1]
    day.append(d[i][1])
    time.append(t)
    colon = t.find(':')
    hour.append(int(t[:colon]))
    minute.append(int(t[colon+1:]))
    loct.append(d[i][3])
    if len(y)==0:
      prevAct.append("Sleeping begin")
    else:
      prevAct.append(y[-1])
    y.append(d[i][4])
hour = numpy.array(hour)
minute = numpy.array(minute)
print(hour.shape)
print(minute.shape)
time = hour*60 + minute
time = numpy.reshape(time,(time.shape[0],1))
print(time.shape)
day, _ = to_one(day)
loct, _ = to_one(loct)
prevAct, vals = to_one(prevAct)
hour, _ = to_one(hour)
minute, _ = to_one(minute)
Y = []
for i in range(len(y)):
  Y.append(vals.index(y[i]))
Y = to_categorical(Y)
print(hour.shape)
trainEnd = 8000
valEnd = 10000
trainX = [day[:trainEnd],loct[:trainEnd],hour[:trainEnd],minute[:trainEnd],prevAct[:trainEnd],time[:trainEnd]]
#trainX = [day[:trainEnd],loct[:trainEnd],hour[:trainEnd],prevAct[:trainEnd],time[:trainEnd]]

trainY = Y[:trainEnd]
valX = [day[trainEnd:valEnd],loct[trainEnd:valEnd],hour[trainEnd:valEnd],minute[trainEnd:valEnd],prevAct[trainEnd:valEnd],time[trainEnd:valEnd]]
#valX = [day[trainEnd:valEnd],loct[trainEnd:valEnd],hour[trainEnd:valEnd],prevAct[trainEnd:valEnd],time[trainEnd:valEnd]]

valY = Y[trainEnd:valEnd]
testX = [day[valEnd:],loct[valEnd:],hour[valEnd:],minute[valEnd:],prevAct[valEnd:],time[valEnd:]]
#testX = [day[valEnd:],loct[valEnd:],hour[valEnd:],prevAct[valEnd:],time[valEnd:]]

testY = Y[valEnd:]
print(day.shape,loct.shape,hour.shape,minute.shape,prevAct.shape,Y.shape)
Days = Input(shape=(7,))
Locts = Input(shape=(11,))
PrevActs = Input(shape=(22,))
Hours = Input(shape=(24,))
Minutes = Input(shape=(60,))
Time = Input(shape=(1,))


Day = Dense(8, activation="relu", input_shape=(7,))(Days)

Loct = Dense(8, activation="relu", input_shape=(11,))(Locts)

PrevAct = Dense(8, activation="relu")(PrevActs)

Hour = Dense(8, activation="relu", input_shape=(24,))(Hours)
Minute = Dense(8, activation="relu", input_shape=(60,))(Minutes)
#Time = concatenate([Hour, Minute])
Time1 = Dense(8, activation="relu", input_shape=(1,))(Time)


X = concatenate([Day, Loct, Time1, PrevAct, Hour, Minute])
#X = concatenate([Day, Loct, Time1, PrevAct, Hour])

att = Attention()([X,X])
print(att.shape)

X = Dense(8, activation="relu")(X)
y = Dense(22, activation="softmax")(X)

model = Model(inputs=[Days, Locts, Hours, Minutes, PrevActs, Time], outputs=y)
#model = Model(inputs=[Days, Locts, Hours, PrevActs, Time], outputs=y)

opt = Adam(lr=0.001)
model.compile(loss="categorical_crossentropy", optimizer=opt, metrics=["accuracy"])
mc = ModelCheckpoint('Best_Model.h5', monitor='val_loss', mode='min', verbose=0, save_best_only=True)
history = model.fit(
    trainX, trainY, validation_data=(valX, valY), batch_size=64,
    epochs=100, callbacks=[mc], verbose=0)
model = load_model("Best_Model.h5")
Ypred1 = model.predict(valX)
Yp = numpy.argmax(Ypred1,axis=1)
Ya = numpy.argmax(valY,axis=1)
YP = []
YA = []
for i in range(len(Yp)):
  YP.append(vals[Yp[i]])
  YA.append(vals[Ya[i]])
print(classification_report(YA,YP))
print("Micro F1-Score:",f1_score(YA,YP,average='micro'))
print("Macro F1-Score:",f1_score(YA,YP,average='macro'))
print("Weighted F1-Score:",f1_score(YA,YP,average='weighted'))
model = load_model("Best_Model.h5")
Ypred1 = model.predict(testX)
Yp = numpy.argmax(Ypred1,axis=1)
Ya = numpy.argmax(testY,axis=1)
YP = []
YA = []
for i in range(len(Yp)):
  YP.append(vals[Yp[i]])
  YA.append(vals[Ya[i]])
print(classification_report(YA,YP))
print("Micro F1-Score:",f1_score(YA,YP,average='micro'))
print("Macro F1-Score:",f1_score(YA,YP,average='macro'))
print("Weighted F1-Score:",f1_score(YA,YP,average='weighted'))
import matplotlib.pyplot as plt

plt.plot(history.history['loss'], label='Training')
plt.plot(history.history['val_loss'], label='Validation')
plt.title('Categorical Cross Entropy')
plt.ylabel('Error')
plt.xlabel('Epoch')
plt.legend(loc="upper left")
plt.show()
print(len(trainX))
print(len(valX))
print(len(testX))
trainset = trainX[0]
for i in range(1,len(trainX)):
  print(trainset.shape)
  print(trainX[i].shape)
  trainset = numpy.concatenate((trainset,trainX[i]),axis=1)

valset = valX[0]
for i in range(1,len(valX)):
  print(valset.shape)
  print(valX[i].shape)
  valset = numpy.concatenate((valset,valX[i]),axis=1)

testset = testX[0]
for i in range(1,len(testX)):
  print(testset.shape)
  print(testX[i].shape)
  testset = numpy.concatenate((testset,testX[i]),axis=1)

print(trainset.shape)
print(valset.shape)
print(testset.shape)
X = Input(shape=(125,))

H1 = Dense(32, activation="relu", input_shape=(7,))(X)

H2 = Dense(12, activation="relu")(H1)
y = Dense(22, activation="softmax")(H2)

model = Model(inputs=X, outputs=y)

opt = Adam(lr=0.0005)
model.compile(loss="categorical_crossentropy", optimizer=opt, metrics=["accuracy"])
mc = ModelCheckpoint('Best_Model.h5', monitor='val_loss', mode='min', verbose=0, save_best_only=True)
history = model.fit(
    trainset, trainY, validation_data=(valset, valY), batch_size=256,
    epochs=100, callbacks=[mc], verbose=0)
model = load_model("Best_Model.h5")
Ypred1 = model.predict(valset)
Yp = numpy.argmax(Ypred1,axis=1)
Ya = numpy.argmax(valY,axis=1)
YP = []
YA = []
for i in range(len(Yp)):
  YP.append(vals[Yp[i]])
  YA.append(vals[Ya[i]])
print(classification_report(YA,YP))
print("Micro F1-Score:",f1_score(YA,YP,average='micro'))
print("Macro F1-Score:",f1_score(YA,YP,average='macro'))
print("Weighted F1-Score:",f1_score(YA,YP,average='weighted'))
model = load_model("Best_Model.h5")
Ypred1 = model.predict(testset)
Yp = numpy.argmax(Ypred1,axis=1)
Ya = numpy.argmax(testY,axis=1)
YP = []
YA = []
for i in range(len(Yp)):
  YP.append(vals[Yp[i]])
  YA.append(vals[Ya[i]])
print(classification_report(YA,YP))
print("Micro F1-Score:",f1_score(YA,YP,average='micro'))
print("Macro F1-Score:",f1_score(YA,YP,average='macro'))
print("Weighted F1-Score:",f1_score(YA,YP,average='weighted'))<jupyter_output>precision    recall  f1-score   support

   Bed_to_Toilet begin       0.90      1.00      0.95        27
     Bed_to_Toilet end       0.90      1.00      0.95        27
          Eating begin       0.87      0.85      0.86        39
            Eating end       0.79      0.79      0.79        39
      Enter_Home begin       1.00      0.96      0.98       118
        Enter_Home end       1.00      0.98      0.99       119
      Leave_Home begin       0.79      0.91      0.85       118
        Leave_Home end       0.99      1.00      1.00       118
Meal_Preparation begin       0.97      0.99      0.98       378
  Meal_Preparation end       0.96      0.97      0.97       378
           Relax begin       0.99      0.99      0.99       655
             Relax end       0.99      1.00      0.99       655
       Respirate begin       0.00      0.00      0.00         2
         Respirate end       0.00      0.00      0.00         2
        Sleeping begin       0.98      [...]<jupyter_text>Data Processing Milan<jupyter_code>import pandas as pd
import numpy
f = open("data")
f = f.read().split("\n")
data = []
for i in range(len(f)):
    dataExample = f[i]
    
    a = dataExample.replace('\t', ' ').replace('\n', ' ').split(" ")
    ar = []
    for i in a:
        if i:
            ar.append(i)
    if len(ar) == 4:
        ar.append("NoAction")
    elif len(ar)>4:
        temp = " ".join(ar[4:])
        ar = ar[:4] + [temp]
    else:
        print(i,dataExample)
    
    if ar != []:
        data.append(ar)
DataNew = []
for ii in range(len(data)):
  i = data[ii]
  try:
    if i[4] != "NoAction":
      DataNew.append([i[0],i[1],i[2],i[4]])
  except:
    print(ii,i)
data = DataNew
data = numpy.array(DataNew)
data = pd.DataFrame(data)
data.to_csv("DataMilan.csv", index=False, header=False)<jupyter_output><empty_output><jupyter_text>Action Model Milan<jupyter_code>import numpy
import pandas as pd
import keras

import tensorflow as tf
from tensorflow.keras.layers import *
from tensorflow.keras.optimizers import *
from tensorflow.keras.utils import to_categorical
from tensorflow.keras.callbacks import EarlyStopping
from tensorflow.keras.callbacks import ModelCheckpoint
from tensorflow.keras.models import Sequential, Model, load_model
from sklearn.metrics import classification_report, f1_score
from tensorflow.keras import backend as K
def to_one(arr):
  vals = list(set(arr))
  ar = []
  for i in range(len(arr)):
    ar.append(vals.index(arr[i]))
  return to_categorical(ar),vals
d = pd.read_csv("DataMilan.csv", header=None).to_numpy()
data = []
day = []
time = []
hour = []
minute = []
loct = []
prevAct = []
y = []
for i in range(1,len(d)):
  if d[i][4] != "NoAction":
    data.append( [d[i][1], d[i][2][:5], d[i][3], d[i-1][4]] )
    t = d[i][2][:5]
    if t[-1] == ":":
      t = t[:-1]
    day.append(d[i][1])
    time.append(t)
    colon = t.find(':')
    hour.append(int(t[:colon]))
    minute.append(int(t[colon+1:]))
    loct.append(d[i][3])
    if len(y)==0:
      prevAct.append("Sleeping begin")
    else:
      prevAct.append(y[-1])
    y.append(d[i][4])
hour = numpy.array(hour)
minute = numpy.array(minute)
print(hour.shape)
print(minute.shape)
time = hour*60 + minute
time = numpy.reshape(time,(time.shape[0],1))
print(time.shape)
day, _ = to_one(day)
loct, _ = to_one(loct)
prevAct, vals = to_one(prevAct)
hour, _ = to_one(hour)
minute, _ = to_one(minute)
Y = []
for i in range(len(y)):
  Y.append(vals.index(y[i]))
Y = to_categorical(Y)
print(day.shape,loct.shape,hour.shape,minute.shape,prevAct.shape,Y.shape)
trainEnd = 350
valEnd = 400
trainX = [day[:trainEnd],loct[:trainEnd],hour[:trainEnd],minute[:trainEnd],prevAct[:trainEnd],time[:trainEnd]]
#trainX = [loct[:trainEnd],hour[:trainEnd],minute[:trainEnd],prevAct[:trainEnd],time[:trainEnd]]

trainY = Y[:trainEnd]
valX = [day[trainEnd:valEnd],loct[trainEnd:valEnd],hour[trainEnd:valEnd],minute[trainEnd:valEnd],prevAct[trainEnd:valEnd],time[trainEnd:valEnd]]
#valX = [loct[trainEnd:valEnd],hour[trainEnd:valEnd],minute[trainEnd:valEnd],prevAct[trainEnd:valEnd],time[trainEnd:valEnd]]

valY = Y[trainEnd:valEnd]
testX = [day[valEnd:],loct[valEnd:],hour[valEnd:],minute[valEnd:],prevAct[valEnd:],time[valEnd:]]
#testX = [loct[valEnd:],hour[valEnd:],minute[valEnd:],prevAct[valEnd:],time[valEnd:]]
testY = Y[valEnd:]
#Days = Input(shape=(7,))
Locts = Input(shape=(28,))
PrevActs = Input(shape=(27,))
Hours = Input(shape=(22,))
Minutes = Input(shape=(60,))
Time = Input(shape=(1,))

#Day = Dense(10, activation="relu", input_shape=(7,))(Days)

Loct = Dense(10, activation="relu", input_shape=(11,))(Locts)

PrevAct = Dense(10, activation="relu")(PrevActs)

Hour = Dense(8, activation="relu", input_shape=(22,))(Hours)
Minute = Dense(8, activation="relu", input_shape=(60,))(Minutes)
Time1 = Dense(8, activation="relu", input_shape=(1,))(Time)
#Time = concatenate([Hour, Minute])
#Time = Dense(10, activation="relu")(Time)

#X = concatenate([Day, Loct, Hour,Minute, PrevAct, Time1])
X = concatenate([Loct, Hour,Minute, PrevAct, Time1])

X = Dense(64, activation="relu")(X)
y = Dense(27, activation="softmax")(X)

#model = Model(inputs=[Days, Locts, Hours, Minutes, PrevActs,Time], outputs=y)
model = Model(inputs=[Locts, Hours, Minutes, PrevActs,Time], outputs=y)

opt = Adam(lr=0.005)
model.compile(loss="categorical_crossentropy", optimizer=opt, metrics=["accuracy"])
mc = ModelCheckpoint('Best_Model.h5', monitor='val_loss', mode='min', verbose=0, save_best_only=True)
history = model.fit(
    trainX, trainY, validation_data=(valX, valY), batch_size=64,
    epochs=200, callbacks=[mc], verbose=0)
model = load_model("Best_Model.h5")
Ypred1 = model.predict(valX)
Yp = numpy.argmax(Ypred1,axis=1)
Ya = numpy.argmax(valY,axis=1)
YP = []
YA = []
for i in range(len(Yp)):
  YP.append(vals[Yp[i]])
  YA.append(vals[Ya[i]])
print(classification_report(YA,YP))
print("Micro F1-Score:",f1_score(YA,YP,average='micro'))
print("Macro F1-Score:",f1_score(YA,YP,average='macro'))
print("Weighted F1-Score:",f1_score(YA,YP,average='weighted'))
model = load_model("Best_Model.h5")
Ypred1 = model.predict(testX)
Yp = numpy.argmax(Ypred1,axis=1)
Ya = numpy.argmax(testY,axis=1)
YP = []
YA = []
for i in range(len(Yp)):
  YP.append(vals[Yp[i]])
  YA.append(vals[Ya[i]])
print(classification_report(YA,YP))
print("Micro F1-Score:",f1_score(YA,YP,average='micro'))
print("Macro F1-Score:",f1_score(YA,YP,average='macro'))
print("Weighted F1-Score:",f1_score(YA,YP,average='weighted'))
import matplotlib.pyplot as plt

plt.plot(history.history['loss'], label='Training')
plt.plot(history.history['val_loss'], label='Validation')
plt.title('Categorical Cross Entropy')
plt.ylabel('Error')
plt.xlabel('Epoch')
plt.legend(loc="upper left")
plt.show()
trainset = trainX[0]
for i in range(1,len(trainX)):
  print(trainset.shape)
  print(trainX[i].shape)
  trainset = numpy.concatenate((trainset,trainX[i]),axis=1)

valset = valX[0]
for i in range(1,len(valX)):
  print(valset.shape)
  print(valX[i].shape)
  valset = numpy.concatenate((valset,valX[i]),axis=1)

testset = testX[0]
for i in range(1,len(testX)):
  print(testset.shape)
  print(testX[i].shape)
  testset = numpy.concatenate((testset,testX[i]),axis=1)

print(trainset.shape)
print(valset.shape)
print(testset.shape)
print(trainY.shape)
print(valY.shape)
X = Input(shape=(145,))

H1 = Dense(64, activation="relu", input_shape=(7,))(X)

H2 = Dense(32, activation="relu")(H1)
y = Dense(27, activation="softmax")(H2)

model = Model(inputs=X, outputs=y)

opt = Adam(lr=0.002)
model.compile(loss="categorical_crossentropy", optimizer=opt, metrics=["accuracy"])
mc = ModelCheckpoint('Best_Model.h5', monitor='val_loss', mode='min', verbose=0, save_best_only=True)
history = model.fit(
    trainset, trainY, validation_data=(valset, valY), batch_size=64,
    epochs=100, callbacks=[mc], verbose=0)
model = load_model("Best_Model.h5")
Ypred1 = model.predict(valset)
Yp = numpy.argmax(Ypred1,axis=1)
Ya = numpy.argmax(valY,axis=1)
YP = []
YA = []
for i in range(len(Yp)):
  YP.append(vals[Yp[i]])
  YA.append(vals[Ya[i]])
print(classification_report(YA,YP))
print("Micro F1-Score:",f1_score(YA,YP,average='micro'))
print("Macro F1-Score:",f1_score(YA,YP,average='macro'))
print("Weighted F1-Score:",f1_score(YA,YP,average='weighted'))
model = load_model("Best_Model.h5")
Ypred1 = model.predict(testset)
Yp = numpy.argmax(Ypred1,axis=1)
Ya = numpy.argmax(testY,axis=1)
YP = []
YA = []
for i in range(len(Yp)):
  YP.append(vals[Yp[i]])
  YA.append(vals[Ya[i]])
print(classification_report(YA,YP))
print("Micro F1-Score:",f1_score(YA,YP,average='micro'))
print("Macro F1-Score:",f1_score(YA,YP,average='macro'))
print("Weighted F1-Score:",f1_score(YA,YP,average='weighted'))<jupyter_output>WARNING:tensorflow:5 out of the last 13 calls to <function Model.make_predict_function.<locals>.predict_function at 0x7fee49d72290> triggered tf.function retracing. Tracing is expensive and the excessive number of tracings could be due to (1) creating @tf.function repeatedly in a loop, (2) passing tensors with different shapes, (3) passing Python objects instead of tensors. For (1), please define your @tf.function outside of the loop. For (2), @tf.function has experimental_relax_shapes=True option that relaxes argument shapes that can avoid unnecessary retracing. For (3), please refer to https://www.tensorflow.org/guide/function#controlling_retracing and https://www.tensorflow.org/api_docs/python/tf/function for  more details.
                        precision    recall  f1-score   support

   Bed_to_Toilet begin       0.00      0.00      0.00         6
     Bed_to_Toilet end       1.00      0.67      0.80         6
          Chores begin       0.00      0.00      0.00         2
      [...]<jupyter_text>Data Processing Tulum<jupyter_code>import pandas as pd
import numpy
f = open("data")
f = f.read().split("\n")
data = []
for ii in range(len(f)):
    dataExample = f[ii]
    
    a = dataExample.replace('\t', ' ').replace('\n', ' ').split(" ")
    ar = []
    for i in a:
        if i:
            ar.append(i)
    if len(ar) == 4:
        ar.append("NoAction")
    elif len(ar)>4:
        temp = " ".join(ar[4:])
        ar = ar[:4] + [temp]
    else:
        print(ii,dataExample)
    
    if ar != []:
        data.append(ar)
DataNew = []
for ii in range(len(data)):
  i = data[ii]
  try:
    if i[4] != "NoAction":
      DataNew.append([i[0],i[1],i[2],i[4]])
  except:
    print(ii,i)
data = DataNew
data = numpy.array(DataNew)
data = pd.DataFrame(data)
data.to_csv("DataTulum.csv", index=False, header=False)<jupyter_output><empty_output><jupyter_text>Action Model Tulum<jupyter_code>import numpy
import pandas as pd
import keras

import tensorflow as tf
from tensorflow.keras.layers import *
from tensorflow.keras.optimizers import *
from tensorflow.keras.utils import to_categorical
from tensorflow.keras.callbacks import EarlyStopping
from tensorflow.keras.callbacks import ModelCheckpoint
from tensorflow.keras.models import Sequential, Model, load_model
from sklearn.metrics import classification_report, f1_score
from tensorflow.keras import backend as K
def to_one(arr):
  vals = list(set(arr))
  ar = []
  for i in range(len(arr)):
    ar.append(vals.index(arr[i]))
  return to_categorical(ar),vals
d = pd.read_csv("DataTulum.csv", header=None).to_numpy()
data = []
day = []
time = []
hour = []
minute = []
loct = []
prevAct = []
y = []
for i in range(1,len(d)):
  if d[i][4] != "NoAction":
    data.append( [d[i][1], d[i][2][:5], d[i][3], d[i-1][4]] )
    t = d[i][2][:5]
    if t[-1] == ":":
      t = t[:-1]
    day.append(d[i][1])
    time.append(t)
    colon = t.find(':')
    hour.append(int(t[:colon]))
    minute.append(int(t[colon+1:]))
    loct.append(d[i][3])
    if len(y)==0:
      prevAct.append("Sleeping begin")
    else:
      prevAct.append(y[-1])
    y.append(d[i][4])
hour = numpy.array(hour)
minute = numpy.array(minute)
print(hour.shape)
print(minute.shape)
time = hour*60 + minute
time = numpy.reshape(time,(time.shape[0],1))
print(time.shape)
loc = {
       "T001":"Kitchen","T002":"Living","T003":"Bedroom","T004":"Bedroom","T005":"Bath",
       "M001":"Entrance","M002":"Living","M003":"Kitchen","M004":"Stairs","M005":"Hallway",
       "M006":"Hallway","M007":"Living","M008":"Living","M009":"Living","M010":"Living",
       "M011":"Living","M012":"Living","M013":"Kitchen","M014":"Kitchen","M015":"Kitchen",
       "M016":"Kitchen","M017":"Bedroom","M018":"Bedroom","M019":"Bedroom","M020":"Bedroom",
       "M021":"Bedroom","M022":"Bedroom","M023":"Stairs","M024":"Door","M025":"Bath",
       "M026":"Bath","M027":"Bath","M028":"Bedroom","M029":"Bedroom","M030":"Bedroom",
       "M031":"Bedroom"  
      }
for i in range(len(loct)):
  loct[i] = loc[loct[i]]
day, _ = to_one(day)
loct, _ = to_one(loct)
prevAct, vals = to_one(prevAct)
hour, _ = to_one(hour)
minute, _ = to_one(minute)
Y = []
for i in range(len(y)):
  Y.append(vals.index(y[i]))
Y = to_categorical(Y)
print(day.shape,loct.shape,hour.shape,minute.shape,prevAct.shape,Y.shape,time.shape)
trainEnd = 16000
valEnd = 20000
trainX = [day[:trainEnd],loct[:trainEnd],hour[:trainEnd],minute[:trainEnd],prevAct[:trainEnd],time[:trainEnd]]
#trainX = [day[:trainEnd],loct[:trainEnd],hour[:trainEnd],minute[:trainEnd],prevAct[:trainEnd]]

trainY = Y[:trainEnd]
valX = [day[trainEnd:valEnd],loct[trainEnd:valEnd],hour[trainEnd:valEnd],minute[trainEnd:valEnd],prevAct[trainEnd:valEnd],time[trainEnd:valEnd]]
#valX = [day[trainEnd:valEnd],loct[trainEnd:valEnd],hour[trainEnd:valEnd],minute[trainEnd:valEnd],prevAct[trainEnd:valEnd]]

valY = Y[trainEnd:valEnd]
testX = [day[valEnd:],loct[valEnd:],hour[valEnd:],minute[valEnd:],prevAct[valEnd:],time[valEnd:]]
#testX = [day[valEnd:],loct[valEnd:],hour[valEnd:],minute[valEnd:],prevAct[valEnd:]]

testY = Y[valEnd:]
Days = Input(shape=(7,))
Locts = Input(shape=(8,))
PrevActs = Input(shape=(35,))
Hours = Input(shape=(24,))
Minutes = Input(shape=(60,))
#Time = Input(shape=(1,))

Day = Dense(16, activation="relu", input_shape=(7,))(Days)

Loct = Dense(16, activation="relu", input_shape=(11,))(Locts)

PrevAct = Dense(16, activation="relu")(PrevActs)

Hour = Dense(16, activation="relu", input_shape=(24,))(Hours)
Minute = Dense(16, activation="relu", input_shape=(60,))(Minutes)
#Time = concatenate([Hour, Minute])
#Time = Dense(16, activation="relu")(Time)
#Time1 = Dense(16, activation="relu", input_shape=(1,))(Time)

X = concatenate([Day, Loct, Hour, Minute, PrevAct])

X = Dense(64, activation="relu")(X)
y = Dense(35, activation="softmax")(X)

model = Model(inputs=[Days, Locts, Hours, Minutes, PrevActs], outputs=y)
opt = Adam(lr=0.01)
model.compile(loss="categorical_crossentropy", optimizer=opt, metrics=["accuracy"])
mc = ModelCheckpoint('Best_Model.h5', monitor='val_loss', mode='min', verbose=0, save_best_only=True)
history = model.fit(
    trainX, trainY, validation_data=(valX, valY), batch_size=64,
    epochs=200, callbacks=[mc], verbose=0)
model = load_model("Best_Model.h5")
Ypred1 = model.predict(valX)
Yp = numpy.argmax(Ypred1,axis=1)
Ya = numpy.argmax(valY,axis=1)
YP = []
YA = []
for i in range(len(Yp)):
  YP.append(vals[Yp[i]])
  YA.append(vals[Ya[i]])
print(classification_report(YA,YP))
print("Micro F1-Score:",f1_score(YA,YP,average='micro'))
print("Macro F1-Score:",f1_score(YA,YP,average='macro'))
print("Weighted F1-Score:",f1_score(YA,YP,average='weighted'))
model = load_model("Best_Model.h5")
Ypred1 = model.predict(testX)
Yp = numpy.argmax(Ypred1,axis=1)
Ya = numpy.argmax(testY,axis=1)
YP = []
YA = []
for i in range(len(Yp)):
  YP.append(vals[Yp[i]])
  YA.append(vals[Ya[i]])
print(classification_report(YA,YP))
print("Micro F1-Score:",f1_score(YA,YP,average='micro'))
print("Macro F1-Score:",f1_score(YA,YP,average='macro'))
print("Weighted F1-Score:",f1_score(YA,YP,average='weighted'))
import matplotlib.pyplot as plt

plt.plot(history.history['loss'], label='Training')
plt.plot(history.history['val_loss'], label='Validation')
plt.title('Categorical Cross Entropy')
plt.ylabel('Error')
plt.xlabel('Epoch')
plt.legend(loc="upper left")
plt.show()
trainset = trainX[0]
for i in range(1,len(trainX)):
  print(trainset.shape)
  print(trainX[i].shape)
  trainset = numpy.concatenate((trainset,trainX[i]),axis=1)

valset = valX[0]
for i in range(1,len(valX)):
  print(valset.shape)
  print(valX[i].shape)
  valset = numpy.concatenate((valset,valX[i]),axis=1)

testset = testX[0]
for i in range(1,len(testX)):
  print(testset.shape)
  print(testX[i].shape)
  testset = numpy.concatenate((testset,testX[i]),axis=1)

print(trainset.shape)
print(valset.shape)
print(testset.shape)
print(trainY.shape)
print(valY.shape)
X = Input(shape=(135,))

H1 = Dense(36, activation="relu", input_shape=(7,))(X)

H2 = Dense(16, activation="relu")(H1)
y = Dense(35, activation="softmax")(H2)

model = Model(inputs=X, outputs=y)

opt = Adam(lr=0.0001)
model.compile(loss="categorical_crossentropy", optimizer=opt, metrics=["accuracy"])
mc = ModelCheckpoint('Best_Model.h5', monitor='val_loss', mode='min', verbose=0, save_best_only=True)
history = model.fit(
    trainset, trainY, validation_data=(valset, valY), batch_size=128,
    epochs=100, callbacks=[mc], verbose=0)
model = load_model("Best_Model.h5")
Ypred1 = model.predict(valset)
Yp = numpy.argmax(Ypred1,axis=1)
Ya = numpy.argmax(valY,axis=1)
YP = []
YA = []
for i in range(len(Yp)):
  YP.append(vals[Yp[i]])
  YA.append(vals[Ya[i]])
print(classification_report(YA,YP))
print("Micro F1-Score:",f1_score(YA,YP,average='micro'))
print("Macro F1-Score:",f1_score(YA,YP,average='macro'))
print("Weighted F1-Score:",f1_score(YA,YP,average='weighted'))
model = load_model("Best_Model.h5")
Ypred1 = model.predict(testset)
Yp = numpy.argmax(Ypred1,axis=1)
Ya = numpy.argmax(testY,axis=1)
YP = []
YA = []
for i in range(len(Yp)):
  YP.append(vals[Yp[i]])
  YA.append(vals[Ya[i]])
print(classification_report(YA,YP))
print("Micro F1-Score:",f1_score(YA,YP,average='micro'))
print("Macro F1-Score:",f1_score(YA,YP,average='macro'))
print("Weighted F1-Score:",f1_score(YA,YP,average='weighted'))<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.19239940387481372, 'fraction_numerical': 0.035171385991058124, 'mean_word_length': 5.354403409090909, 'pattern_counts': {'":': 89, '<': 25, '<?xml version=': 0, '>': 28, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '462550', 'license': 'MIT', 'n_tokens_mistral': 11074, 'n_tokens_neox': 10418, 'n_words': 1729, 'path': 'ipynb notebooks/IP_Actions.ipynb', 'repo_name': 'anmolsinghal98/FFHARNet', 'size': '26840'}