Human Activity Recognition using Smartphone Dataset- ML Python
Hey ML Enthusiasts, In this article, we are going to create a Human Activity Recognition Model using Machine Learning in Python. Before Proceeding further in the article, you are advised to download Dataset and human-activity-recognition (Notebook)
The Dataset contains various sensors data, related to various activities performed by different individuals.
Requirements:
Code Overview: Human Activity Recognition using Smartphone Dataset in Python
# Lets load Train CSV
df_train = pd.read_csv('/kaggle/input/human-activity-recognition-with-smartphones/train.csv')
df_train.head()
We are going to load the data frames and then do Feature Engineering.
We will separate features and Labels:
x_train = df_train.iloc[:,0:-2]
x_train = np.array(x_train)
x_train.shape
And now will encode the Labels into 0 and 1 format:
from sklearn.preprocessing import LabelEncoder
lb = LabelEncoder()
y_train = lb.fit_transform(y_train)
# Lets encode this
from keras.utils import to_categorical
y_train = to_categorical(y_train)
y_train
Using TensorFlow backend.
array([[0., 0., 1., 0., 0., 0.],
[0., 0., 1., 0., 0., 0.],
[0., 0., 1., 0., 0., 0.],
...,
[0., 0., 0., 0., 0., 1.],
[0., 0., 0., 0., 0., 1.],
[0., 0., 0., 0., 0., 1.]])
Now we will create our model:
# Lets Prepare up the model
model = Sequential()
model.add(Dense(256,input_shape=(x_train.shape[1],1)))
model.add(Dense(128))
model.add(Dropout(0.2))
model.add(Dense(256))
model.add(Dense(128))
model.add(Flatten())
model.add(Dense(y_train.shape[1]))
model.add(Activation('softmax'))
model.summary()
Model: "sequential_1"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
dense_1 (Dense) (None, 561, 256) 512
_________________________________________________________________
dense_2 (Dense) (None, 561, 128) 32896
_________________________________________________________________
dropout_1 (Dropout) (None, 561, 128) 0
_________________________________________________________________
dense_3 (Dense) (None, 561, 256) 33024
_________________________________________________________________
dense_4 (Dense) (None, 561, 128) 32896
_________________________________________________________________
flatten_1 (Flatten) (None, 71808) 0
_________________________________________________________________
dense_5 (Dense) (None, 6) 430854
_________________________________________________________________
activation_1 (Activation) (None, 6) 0
=================================================================
Total params: 530,182
Trainable params: 530,182
Non-trainable params: 0
We would train our model and found a 95% accuracy.
Congrats…We have successfully built the Human Activity Recognition model.
If you face any problem, drop down your feedback in the comments section
For More Projects, See Language Translator (RNN BiDirectional LSTMs and Attention) in Python.
Leave a Reply