Free Shipping

Secure Payment

easy returns

24/7 support

  • Home
  • Blog
  • K-Nearest Neighbors Case Study 2

K-Nearest Neighbors Case Study 2

 July 7  | 0 Comments

Human Activity Recognition or HAR for short is the problem of predicting what a person is doing based on a trace of their movement using sensors.
Movements are often normal indoor activities such as standing, sitting, jumping, and going up stairs. Sensors are often located on the subject such as a smartphone or vest and often record accelerometer data in three dimensions (x, y, z).
The idea is that once the subject’s activity is recognized and known, an intelligent computer system can then offer assistance.
It is a challenging problem because there is no clear analytical way to relate the sensor data to specific actions in a general way. It is technically challenging because of the large volume of sensor data collected (e.g. tens or hundreds of observations per second) and the classical use of hand crafted features and heuristics from this data in developing predictive models

Problem Description

The dataset “Activity Recognition from Single Chest-Mounted Accelerometer Data Set” was collected and made available by Casale, Pujol et al. from the University of Barcelona in Spain. It is freely available from the UCI Machine Learning repository:

Free Step-by-step Guide To Become A Data Scientist

Subscribe and get this detailed guide absolutely FREE

The dataset is comprised of un-calibrated accelerometer data from 15 different subjects, each performing 7 activities. Each subject wore a custom-developed chest-mounted accelerometer and data was collected at 52 Hz(52 observations per second).

Data Description

Un-calibrated Accelerometer Data are collected from 15 participants performing 7 activities. The dataset provides challenges for identification and authentication of people using motion patterns.

Data Set Information:

— The dataset collects data from a wearable accelerometer mounted on the chest
— Sampling frequency of the accelerometer: 52 Hz
— Accelerometer Data are Un-calibrated
— Number of Participants: 15
— Number of Activities: 7
— Data Format: CSV

Attribute Information:

— Data are separated by participant
— Each file contains the following information
—- Sequential number, x acceleration, y acceleration, z acceleration, label
— Labels are codified by numbers
— 1: Working at Computer
— 2: Standing Up, Walking and Going updown stairs
— 3: Standing
— 4: Walking
— 5: Going UpDown Stairs
— 6: Walking and Talking with Someone
— 7: Talking while Standing

Data Reading

data_dir = ‘/home/prateek/Documents/LearnersHeaven/content_preparation/course_content/development/case_studies/ML/data/HumanActivityRecogmnition/Activity Recognition from Single Chest-Mounted Accelerometer_’

Import libraries and tools


from glob import glob
import pandas as pd
%matplotlib inline
all_data = glob(data_dir+“/*.csv”)

[‘/home/ubuntu/Documents/LearnersHeaven/content_preparation/course_content/development/case_studies/ML/data/HumanActivityRecogmnition/Activity Recognition from Single Chest-Mounted Accelerometer_/4.csv’,

‘/home/ubuntu/Documents/LearnersHeaven/content_preparation/course_content/development/case_studies/ML/data/HumanActivityRecogmnition/Activity Recognition from Single Chest-Mounted Accelerometer_/2.csv’,

‘/home/ubuntu/Documents/LearnersHeaven/content_preparation/course_content/development/case_studies/ML/data/HumanActivityRecogmnition/Activity Recognition from Single Chest-Mounted Accelerometer_/8.csv’

Load data set
def load_dataset(all_data):
subjects = pd.DataFrame()
for i,name in enumerate(all_data):
df = pd.read_csv(name, header=None)
df[‘subject_id’] = i+1
subjects = subjects.append(df.iloc[:,1:])
return subjects
subjects_df = load_dataset(all_data)
subjects_df.columns = [‘x’, ‘y’, ‘z’, ‘label’,‘subject_id’]
print(‘Loaded %d subjects’ % len(subjects_df.subject_id.unique()))

Output: Loaded 15 subjects

Plot a subject
from matplotlib import pyplot
#plot the x, y, z acceleration and activities for a single subject
def plot_subject(subject):
#create a plot for each column
for col in range(subject.shape[1]):
pyplot.subplot(subject.shape[1], 1, col+1
#plot activities for a single subject

Running the example creates a line plot for each variable for the first loaded subject. We can see some very large movement in the beginning of the sequence that may be an outlier or unusual behaviour that could be removed. We can also see that the subject performed some actions multiple times. For example, a closer look at the plot of the class variable (bottom plot) suggests the subject performed activities in the following order, 1, 2, 0, 3, 0, 4, 3, 5, 3, 6, 7. Note that activity 3 was performed twice.

Plot Total Activity Durations
subjects = []
for k,values in subjects_df.groupby(‘subject_id’):
# returns a list of dict, where each dict has one sequence per activity
def group_by_activity(subjects, activities):
grouped = [{a:s[s[:,-1]==a] for a in activities} for s in subjects]
return grouped
# calculate total duration in sec for each activity per subject and plot
def calculate_durations(grouped, activities):
# calculate the lengths for each activity for each subject
freq = 52
durations = [[len(s[a])/freq for s in grouped] for a in activities]
return durations
def plot_durations(grouped, activities):
durations = calculate_durations(grouped, activities)
pyplot.boxplot(durations, labels=activities)
# grouped
activities = [i for i in range(0,8)]
grouped = group_by_activity(subjects, activities)
# plot durations
plot_durations(grouped, activities)

We can see that there is relatively fewer observations for activities 0 (no activity), 2 (standing up, walking and going up/down stairs), 5 (going up/down stairs) and 6 (walking and talking). We can also see that each subject spent a lot of time on activity 1 (standing Up, walking and going up/down stairs) and activity 7 (talking while standing).

calculate_durations(grouped, activities)
#plot the x, y, z acceleration for each subject
def plot_subjects(subjects):
#create a plot for each subject
xaxis = None
for i in range(len(subjects)):
ax = pyplot.subplot(len(subjects), 1, i+1, sharex=xaxis)
if i == 0:
xaxis = ax
#plot a histogram of x data
for j in range(subjects[i].shape[1]-1):
pyplot.hist(subjects[i][:,j], bins=100)

Running the example creates a single figure with 15 plots, one for each subject, and 3 histograms on each plot for each of the 3 axis of accelerometer data. The three colors blue, orange and green represent the x, y and z axes. This plot suggests that the distribution of each axis of accelerometer is Gaussian or really close to Gaussian. This may help with simple outlier detection and removal along each axis of the accelerometer data. The plot really helps to show both the relationship between the distributions within a subject and differences in the distributions between the subjects.
Within each subject, a common pattern is for the x (blue) and z (green) are grouped together to the left and y data (orange) is separate to the right. The distribution of y is often sharper whereas the distributions of x and z are flatter.
Across subjects, we can see a general clustering of values around 2,000 (whatever the units are), although with a lot of spread. This marked difference in distributions does suggest the need to at least standardize (shift to zero mean and unit variance) the data per axis and per subject before any cross-subject modeling is performed.

Defining the variables
X = subjects_df[[‘x’,‘y’,‘z’]]
y = subjects_df[‘label’]
Train data split
#evaluate the model by splitting into train and test sets
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.30, random_state=12)
##Import the Classifier.
from sklearn import metrics
from sklearn.neighbors import KNeighborsClassifier
##Instantiate the model with 5 neighbors.
model = KNeighborsClassifier(n_neighbors=5)
##Fit the model on the training data., y_train)

KNeighborsClassifier(algorithm=’auto’, leaf_size=30, metric=’minkowski’,
metric_params=None, n_jobs=None, n_neighbors=5, p=2,

#use the model to make predictions with the test data
Y_pred = model.predict(X_test)
#how did our model perform?
count_misclassified = (y_test != y_pred).sum()
print(‘Misclassified samples: {}’.format(count_misclassified))
accuracy = metrics.accuracy_score(y_test, y_pred)
print(‘Accuracy: {:.2f}’.format(accuracy))

Misclassified samples: 160845
Accuracy: 0.72

How to decide the value of n-neighbors

Choosing a large value of K will lead to greater amount of execution time & under-fitting. Selecting the small value of K will lead to over-fitting. There is no such guaranteed way to find the best value of K.

from sklearn.metrics import accuracy_score
accuracy = []
for K in range(25):
K_value = K+1
neigh = KNeighborsClassifier(n_neighbors = K_value), y_train)
y_pred = neigh.predict(X_test)
print(“Accuracy is “, accuracy_score(y_test,y_pred)*100,“% for K-Value:”,K_value)

Accuracy is 65.9405711082933 % for K-Value: 1
Accuracy is 65.22024187423993 % for K-Value: 2
Accuracy is 69.8665730215597 % for K-Value: 3
Accuracy is 71.19980486758502 % for K-Value: 4
Accuracy is 72.17546694252762 % for K-Value: 5
Accuracy is 72.69357118267888 % for K-Value: 6
Accuracy is 73.21721109417734 % for K-Value: 7
Accuracy is 73.55160024149366 % for K-Value: 8
Accuracy is 73.79499679104052 % for K-Value: 9
Accuracy is 73.97490610982426 % for K-Value: 10
Accuracy is 74.16242697671039 % for K-Value: 11
Accuracy is 74.28075195175663 % for K-Value: 12
Accuracy is 74.40409362896125 % for K-Value: 13
Accuracy is 74.47311653107155 % for K-Value: 14
Accuracy is 74.55113489912104 % for K-Value: 15
Accuracy is 74.62794233906332 % for K-Value: 16
Accuracy is 74.69298647739284 % for K-Value: 17
Accuracy is 74.73761782762958 % for K-Value: 18
Accuracy is 74.81096547297987 % for K-Value: 19
Accuracy is 74.84400651133343 % for K-Value: 20
Accuracy is 74.84054671674143 % for K-Value: 21
Accuracy is 74.89538446102455 % for K-Value: 22
Accuracy is 74.9305013761333 % for K-Value: 23
Accuracy is 74.95420096908848 % for K-Value: 24
Accuracy is 74.97478674691084 % for K-Value: 25