Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect multiple plateaus and ascents and descent in the time-series data using python

Analysing time series data of bike trails, I would like to know the time interval for each plateau ,ascent and descent.Sample csv file is uploaded here.

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from datetime import datetime
import matplotlib.dates as mdates


df = pd.read_csv(r'C:\Data\Sample.csv', parse_dates=['dateTime'])
feature_used='Cycle_Alt'
print("Eliminating null values..")
df=df[df[feature_used].notnull()]

plt.figure(figsize=(8,6))
x=df['dateTime']        
y=df['Cycle_Alt']

plt.plot(x,y,c='b',linestyle=':',label="Altitude")
plt.xticks(rotation='vertical')
plt.gcf().autofmt_xdate()   
plt.legend(loc='best', bbox_to_anchor=(1, 0.5))

This plot provides me with a cross-profile like this. enter image description here

What could be done to classify the time-series data to detect each plateau ,ascent and descent, with the assumption that one may have more variables than presented in the sample.

enter image description here

like image 876
addcolor Avatar asked Nov 30 '18 04:11

addcolor


1 Answers

If you are only interested in identify the plateaus, ascents, and descents in a series, the easy way is to use the numpy.diff function to calculate the n-th discrete difference. Then you can use the numpy.sign to convert the differences to either positive (ascents), zero (plateau), or negative (descents).

An example:

a = np.random.randint(1, 5, 10)
#array([1, 1, 1, 1, 3, 4, 2, 2, 2, 2])

diff = np.diff(a)
#array([ 0,  0,  0,  2,  1, -2,  0,  0,  0])

gradient = np.sign(diff)
#array([ 0,  0,  0,  1,  1, -1,  0,  0,  0])

Note that the final array gradient will have one fewer element than the original array, because the numpy.diff function will return (n-1) differences for an array of length n.

like image 159
Xiaoyu Lu Avatar answered Oct 25 '22 04:10

Xiaoyu Lu