- Dataset ### Davide Anguita, Alessandro Ghio, Luca Oneto, Xavier Parra and Jorge L. Reyes-Ortiz. A Public Domain Dataset for Human Activity Recognition Using Smartphones. 21th European Symposium on Artificial Neural Networks, Computational Intelligence and Machine Learning, ESANN 2013. Bruges, Belgium 24-26 April 2013. https://archive.ics.uci.edu/ml/datasets/human+activity+recognition+using+smartphones

- Project Team

### MD Sarwar Zahan

### Gernot Manfred Rischner

(reset - Deleting all variables, just if necessary)

In [1]:

```
%reset
```

In [2]:

```
import pandas as pd #pandas package to read data
import tensorflow as tf #tensorflow
import numpy as np #numpy package
from scipy import stats
import matplotlib.pyplot as plt
```

In [3]:

```
train_x=pd.read_csv("C:\MLProject\X_train.csv");
train_y=pd.read_csv(r'C:\MLProject\train\y_train.txt', delim_whitespace=True, header=None);
test_x=pd.read_csv("C:\MLProject\X_test.csv");
test_y=pd.read_csv(r'C:\MLProject\test\y_test.txt', delim_whitespace=True, header=None);
```

In [4]:

```
train_x=train_x.iloc[:,0:-2]; #select features from training data set
test_x=test_x.iloc[:,0:-2]; #select features from test data set
```

In [5]:

```
numberOfChannels=train_x.shape[1]
```

In [6]:

```
train_x=np.array(train_x); #save training data in numpy array format for better handling
train_y=np.array(train_y); #save training data in numpy array format for better handling
test_x=np.array(test_x); #save training data in numpy array format for better handling
test_y=np.array(test_y); #save training data in numpy array format for better handling
```

In [7]:

```
blockSize=30; #number of instances per block
blockTR=np.empty((0,numberOfChannels,blockSize)); #init empty block
blockTRLabel=np.empty((0)); #init empty label of block
start=0; #temporary starting index
end=0; #temporary ending index
for i in range(1,train_x.shape[0]): #loop to go through dataset
if(i%blockSize==0): #if blockSize is reached, assign indices
end=i;
tempTR=train_x[start:end]; #temporary array with cooresponding block size
blockTR=np.vstack([blockTR,np.dstack(tempTR)]) #stack blocks
blockTRLabel=np.append(blockTRLabel,stats.mode(train_y[start:end])[0][0]) #check for the highest numbers
#of corresponding labels in the
#temporary block and assign this
#label to the block
start=i;
```

In [8]:

```
blockTS=np.empty((0,numberOfChannels,blockSize)); #init empty block
blockTSLabel=np.empty((0)); #init empty label of block
start=0; #temporary starting index
end=0; #temporary ending index
for i in range(1,test_x.shape[0]): #loop to go through dataset
if(i%blockSize==0): #if blockSize is reached, assign indices
end=i;
tempTS=test_x[start:end]; #temporary array with cooresponding block size
blockTS=np.vstack([blockTS,np.dstack(tempTS)]) #stack blocks
blockTSLabel=np.append(blockTSLabel,stats.mode(test_y[start:end])[0][0]) #check for the highest numbers
#of corresponding labels in the
#temporary block and assign this
#label to the block
start=i;
```

In [9]:

```
blockTRLabel=np.asarray(pd.get_dummies(blockTRLabel), dtype = np.int8)
blockTR=blockTR.reshape(len(blockTR),1,blockSize,numberOfChannels)
blockTSLabel=np.asarray(pd.get_dummies(blockTSLabel), dtype = np.int8)
blockTS=blockTS.reshape(len(blockTS),1,blockSize,numberOfChannels)
```

In [10]:

```
train_x = blockTR
train_y = blockTRLabel
test_x = blockTS
test_y = blockTSLabel
```

In [11]:

```
kernelSize=10
numberOfLabels=6
batch_size = 15
depth=10
num_hidden=300
dropout=0.6
```

In [12]:

```
tf.reset_default_graph() #reset, if necessary
```

In [13]:

```
#Placeholders
x=tf.placeholder(tf.float32,shape=[None,1,blockSize,numberOfChannels]);
y=tf.placeholder(tf.float32,shape=[None,numberOfLabels]);
keep_prob=tf.placeholder(tf.float32);
def conv(x,W):
return tf.nn.depthwise_conv2d(x,W,strides=[1,1,1,1], padding='VALID')
def max_poll(x):
return tf.nn.max_pool(x, ksize=[1,1,4,1], strides=[1,1,2,1], padding='VALID')
```

In [14]:

```
def cnn(x,dropout):
#Define weights for the two convolutional layers using Xavier initializer
weights={'w_convolution1': tf.get_variable(name='w_convolution1', shape=[1,kernelSize,numberOfChannels,depth],initializer=tf.contrib.layers.xavier_initializer()),
'w_convolution2': tf.get_variable(name='w_convolution2', shape=[1,6,depth*numberOfChannels,depth//10],initializer=tf.contrib.layers.xavier_initializer())}
#Define biases for the two convolutional layers using Zero initializer
biases={'b_convolution1':tf.get_variable(name='b_convolution1', shape=[numberOfChannels*depth], initializer=tf.zeros_initializer()),
'b_convolution2':tf.get_variable(name='b_convolution2', shape=[numberOfChannels*depth*(depth//10)], initializer=tf.zeros_initializer())}
#Convolutinal layer 1 with max polling
conv1=conv(x,weights['w_convolution1'])+biases['b_convolution1']
conv1=max_poll(conv1)
#Convolutional layer 2
conv2=conv(conv1,weights['w_convolution2'])+biases['b_convolution2']
#Get shape of convolutional layer 2, to correctly size the follwing layers(fullyConnectet and output)
shape=conv2.get_shape().as_list()
#Define weights for the fully connected and the output layer
weights={'w_fullyConnected': tf.get_variable(name='w_fullyConnected', shape=[shape[1]*shape[2]*depth*numberOfChannels*(depth//10),num_hidden],initializer=tf.contrib.layers.xavier_initializer()),
'w_output': tf.get_variable(name='w_output', shape=[num_hidden,numberOfLabels],initializer=tf.contrib.layers.xavier_initializer())}
#Define biases for the fully connected and the output layer
biases={'b_fullyConnected':tf.get_variable(name='b_fullyConnected', shape=[num_hidden], initializer=tf.zeros_initializer()),
'b_output':tf.get_variable(name='b_output', shape=[numberOfLabels], initializer=tf.zeros_initializer())}
#Fully connected
fullyC=tf.reshape(conv2,[-1,shape[1]*shape[2]*shape[3]])
fullyC=tf.nn.relu(tf.matmul(fullyC,weights['w_fullyConnected'])+biases['b_fullyConnected'])
#DropOut
fullyC=tf.nn.dropout(fullyC,dropout)
#Output
output=tf.matmul(fullyC,weights['w_output'])+biases['b_output']
return output
```

In [15]:

```
lr=0.0001 #learning rate
```

In [16]:

```
def train_neural_network(x):
prediction = cnn(x,keep_prob)
cost = tf.reduce_mean( tf.nn.softmax_cross_entropy_with_logits(logits=prediction, labels=y))
optimizer = tf.train.AdamOptimizer(learning_rate=lr).minimize(cost)
hm_epochs = 100
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
for epoch in range(hm_epochs):
epoch_loss = 0
for b in range(int(len(train_y)/batch_size)):
offset = (b * batch_size) % (train_y.shape[0] - batch_size)
epoch_x = train_x[offset:(offset + batch_size), :, :, :]
epoch_y = train_y[offset:(offset + batch_size), :]
_, c = sess.run([optimizer, cost], feed_dict={x: epoch_x, y: epoch_y, keep_prob: dropout})
epoch_loss += c
correct = tf.equal(tf.argmax(prediction, 1), tf.argmax(y, 1))
accuracyTR = tf.reduce_mean(tf.cast(correct, 'float'))
print('Epoch', epoch+1, 'completed out of',hm_epochs,'loss:',epoch_loss, 'training accuracy:',accuracyTR.eval({x:train_x, y:train_y, keep_prob: 1.0}))
accuracyTR = tf.reduce_mean(tf.cast(correct, 'float'))
accuracyTR = accuracyTR.eval({x:train_x, y:train_y, keep_prob: 1.0})
accuracyTS = tf.reduce_mean(tf.cast(correct, 'float'))
accuracyTS = accuracyTS.eval({x:test_x, y:test_y, keep_prob: 1.0})
confusionMatrix=tf.contrib.metrics.confusion_matrix(tf.argmax(y, 1),tf.argmax(prediction, 1),dtype=tf.float64)
confusionMatrix=confusionMatrix.eval({x:test_x, y:test_y, keep_prob: 1.0})
print('Training finished')
return [confusionMatrix, accuracyTR, accuracyTS]
```

(Function prints loss and training accuracy for each epoch. Furthermore, returns accuracy and confusion matrix for the test data set)

In [17]:

```
[cm, acctr, accts]=train_neural_network(x)
```

In [18]:

```
print('Training Accuracy:',acctr)
print('')
print('Testing Accuracy:',accts)
print('')
print("Confusion Matrix:")
print('')
print(cm)
print('')
normalised_confusion_matrix = (np.array(cm, dtype=np.float32)/(np.sum(cm)))*100
print("Confusion matrix (normalised to % of total test data):")
print('')
print(normalised_confusion_matrix)
LABELS = [
"WALKING",
"WALKING_UPSTAIRS",
"WALKING_DOWNSTAIRS",
"SITTING",
"STANDING",
"LAYING"
]
# Plot Results:
width = 10
height = 10
plt.figure(figsize=(width, height))
plt.imshow(
normalised_confusion_matrix,
interpolation='nearest',
cmap=plt.cm.Blues
)
plt.title("Confusion matrix \n(normalised to % of total test data)")
plt.colorbar()
tick_marks = np.arange(numberOfLabels)
plt.xticks(tick_marks, LABELS, rotation=90)
plt.yticks(tick_marks, LABELS)
plt.ylabel('True label')
plt.xlabel('Predicted label')
plt.show()
```

In [ ]:

```
```