Home > Back-end >  how to detect a braking process in python
how to detect a braking process in python

Time:07-31

I have some trips, and for each trip contains different steps, the data frame looks like following:

tripId  duration (s)    distance (m)    speed Km/h
1819714 NaN              NaN            NaN
1819714 6.0              8.511452       5.106871
1819714 10.0             6.908963       2.487227
1819714 5.0              15.960625      11.491650
1819714 6.0              26.481649      15.888989
... ... ... ... ...
1865507 6.0              16.280313      9.768188
1865507 5.0              17.347482      12.490187
1865507 5.0              14.266625      10.271970
1865507 6.0              22.884008      13.730405
1865507 5.0              21.565655      15.527271

I want to know if, on a trip X, the cyclist has braked (speed has decreased by at least 30%). The problem is that the duration between every two steps is each time different. For example, in 6 seconds, the speed of a person X has decreased from 28 km/h to 15 km/h.. here we can say, he has braked, but if the duration was high, we will not be able to say that My question is if there is a way to apply something to know if there is a braking process, for all data frame in a way that makes sense

CodePudding user response:

The measure of braking is the "change in speed" relative to "change in time". From your data, I created a column 'acceleration', which is change in speed (Km/h) divided by duration (seconds). Then the final column to detect braking if the value is less than -1 (Km/h/s).

Note that you need to determine if a reduction of 1km/h per second is good enough to be considered as braking.

df['speedChange'] = df['speedKm/h'].diff()
df['acceleration'] = df['speedChange'] / df['duration(s)']
df['braking'] = df['acceleration'].apply(lambda x: 'yes' if x<-1 else 'no')
print(df)

Output:

      tripId  duration(s)  distance(m)  speedKm/h  speedChange  acceleration  braking
0  1819714.0          6.0     8.511452   5.106871          NaN           NaN       no
1  1819714.0         10.0     6.908963   2.487227    -2.619644     -0.261964       no
2  1819714.0          5.0    15.960625  11.491650     9.004423      1.800885       no 
3  1819714.0          6.0    26.481649  15.888989     4.397339      0.732890       no 
4  1865507.0          6.0    16.280313   9.768188    -6.120801     -1.020134      yes 
5  1865507.0          5.0    17.347482  12.490187     2.721999      0.544400       no 
6  1865507.0          5.0    14.266625  10.271970    -2.218217     -0.443643       no 
7  1865507.0          6.0    22.884008  13.730405     3.458435      0.576406       no 
  • Related