Vivian
4/28/2019 - 3:22 PM

projecting the time onto a circle

# There also exist some more esoteric approaches to such data like projecting the time onto a circle and using the two coordinates.
def make_harmonic_features(value, period=24):
    value *= 2 * np.pi / period 
    return np.cos(value), np.sin(value)
#This transformation preserves the distance between points, which is important for algorithms that estimate distance (kNN, SVM, k-means ...)
In : from scipy.spatial import distance

In : euclidean(make_harmonic_features(23), make_harmonic_features(1)) 
Out: 0.5176380902050424

In : euclidean(make_harmonic_features(9), make_harmonic_features(11)) 
Out: 0.5176380902050414

In : euclidean(make_harmonic_features(9), make_harmonic_features(21)) 
Out: 2.0