Accelerometer Data Analysis using Python

Accelerometer is a device used to measure the acceleration or vibrations of a motion. The data provided by an accelerometer is three-dimensional and can be used in data-driven applications for solving problems like fall detection and health monitoring. So, if you want to learn how to analyze accelerometer data, this article is for you. In this article, I will take you through the task of Accelerometer Data Analysis using Python.

Accelerometer Data Analysis

For the task of Accelerometer Data Analysis, we first need to collect data collected by an accelerometer. As an accelerometer collects three-dimensional data, it’s essential to have data about the x, y, and z axes in our dataset with respect to a particular time.

I found an ideal dataset for this task. You can download the dataset from here. In the section below, I will take you through how to analyze accelerometer data using the Python programming language.

Accelerometer Data Analysis using Python

I will start the task of accelerometer data analysis by importing the necessary Python libraries and the dataset:

import plotly.express as px
import pandas as pd
import plotly.graph_objects as go

data = pd.read_csv("accdata.csv")
print(data.head())
         Date      Time   accel_x   accel_y   accel_z
0  2022-09-03  23:35:16 -1.838747  3.543418  9.126697
1  2022-09-03  23:35:31  1.110910  1.810017  9.634268
2  2022-09-03  23:35:47  8.829816  0.833182  4.663905
3  2022-09-03  23:36:52 -0.852336 -0.124498  9.787497
4  2022-09-03  23:37:44 -0.900220 -0.095768  9.835381

Let’s start by visualizing a line plot with time on the x-axis and accelerometer data on the y-axis:

fig = px.line(data, x="Date", 
              y=["accel_x", "accel_y", "accel_z"], 
              title="Acceleration data over time")
fig.show()
Acceleration data over time

Now let’s have a look at the average acceleration values by the hour of day and day of the week, which can help us identify any patterns or trends in the data:

data["hour"] = pd.to_datetime(data["Time"]).dt.hour
data["day_of_week"] = pd.to_datetime(data["Date"]).dt.day_name()
agg_data = data.pivot_table(index="hour", columns="day_of_week", 
                            values=["accel_x", "accel_y", "accel_z"], 
                            aggfunc="mean")

# Create a heatmap
fig = go.Figure(go.Heatmap(x=agg_data.columns.levels[1], 
                           y=agg_data.index, 
                           z=agg_data.values,
                           xgap=1, ygap=1, 
                           colorscale="RdBu", 
                           colorbar=dict(title="Average Acceleration")))
fig.update_layout(title="Average Acceleration by Hour of Day and Day of Week")
fig.show()
accelerometer data analysis: Average Acceleration by Hour of Day

Now let’s create a new feature to represent the magnitude of the acceleration vector:

data['accel_mag'] = (data['accel_x'] ** 2 + data['accel_y'] ** 2 + data['accel_z'] ** 2) ** 0.5

Now let’s create a scatter plot of the magnitude of acceleration over time:

fig = px.scatter(data, x='Time', 
                 y='accel_mag', 
                 title='Magnitude of Acceleration over time')
fig.show()
Magnitude of Acceleration over time

We can also create a 3D scatter plot where the x, y, and z axes represent the acceleration in each respective direction:

fig = px.scatter_3d(data, x='accel_x', 
                    y='accel_y', 
                    z='accel_z', 
                    title='Acceleration in 3D space')
fig.show()
accelerometer data analysis: Acceleration in 3D space

At last, let’s create a histogram to visualize the distribution of the magnitude of acceleration:

fig = px.histogram(data, 
                   x='accel_mag', 
                   nbins=50, title='Acceleration magnitude histogram')
fig.show()
Acceleration magnitude histogram

So, this is how you can analyze and work with accelerometer data using the Python programming language.

Summary

Accelerometer is a device used to measure the acceleration or vibrations of a motion. The data provided by an accelerometer is three-dimensional and can be used in data-driven applications for solving problems like fall detection and health monitoring. I hope you liked this article on Accelerometer Data Analysis using Python. Feel free to ask valuable questions in the comments section below.

Aman Kharwal
Aman Kharwal

I'm a writer and data scientist on a mission to educate others about the incredible power of data📈.

Articles: 1431

Leave a Reply