Machine Learning Tip : Using Rotational Data

Zack Akil
Towards Data Science
2 min readFeb 12, 2018

--

A time might arise were you’re going to need to predict using rotational data, either as a feature or as a target. Plugging degrees straight into your model might seem to work, but be wary, it’s not doing what you want.

Why machine learning algorithms hate degrees

Simply put, they are not smooth! What I mean is that the degrees scale teleports from 359 back to 0 degrees as it progresses. Look:

In this format a gradient decent algorithm won’t know 350 degrees is 10 degrees away from 0 degrees and therefore will never produce a robust model.

Sin and Cos to the rescue!

To overcome these issues we just convert the degrees to SIN and COS and use those as our features. Now there is no teleporting and distances are appropriately measured:

The important thing to remember is that we can get back to our degrees value because for each set of SIN and COS values there is only one equivalent degrees value. In the next section I’ll show you the magical atan2 function which will do this conversion for you.

One Gotcha!

One downside is that now if you want to predict a direction (i.e the direction is your target feature) you’ll have to create 2 predictors: one for predicting SIN and one for COS, and then combine them using the atan2 function.

import numpy as npdegrees_array = np.arctan2(sin_array, cos_array) * 180 / np.pi# will be in the range -180 : 180, 
# use modulo to wrap it to the range 0 : 360
degrees_array_fixed_range = np.mod(degrees_array, 360)

See a full example of predicting a rotational value using 2 predictors:

https://github.com/ZackAkil/optimising-training-data-meetup/blob/master/CL-DS-PN%20Optimising%20data%20-%20My%20Solution.ipynb

--

--