I would like to use the hour of the day (0-23) as a continuous feature, so the model will know that 12pm comes before 13:00, and that 8:00 is further from 21:00 than from 10:00. How do I engineer this feature so it will also understand that 0:00 comes after 23:00?


2 Answers 2


Great question.

I'd say you should either use fourier transforms or sine / cosine transforms as they should get 0 to be right next to 23. The below post is talking about transforming day features, but should be easily applicable here.

Transforming day features

Transforming hour features

  • $\begingroup$ Thanks :) but, won't the FT will cause different hours to have the same value? If the transform is f(x) -> X, then there could be several x with the same X $\endgroup$
    – shakedzy
    Commented Jan 14, 2018 at 22:57
  • $\begingroup$ replying to myself - maybe I'll use the FT and the derivative sign as a second feature to distinguish the ambiguous options.. $\endgroup$
    – shakedzy
    Commented Jan 14, 2018 at 22:58
  • $\begingroup$ @shakedzy - check my update. I'm not sure about the FT, but the sin / cos transform seems to be the way to go $\endgroup$ Commented Jan 14, 2018 at 23:16
  • 1
    $\begingroup$ plumbus_bouquet - a FT is a combination of sin and cos as real and imaginary parts :) $\endgroup$
    – shakedzy
    Commented Jan 15, 2018 at 8:32

Notice that if you want to establish an order relation that is cyclic you end up with a contradiction. In fact, you get: 0:00 < 23:59 < 0:00.

If you just want to compute distances and you give up with continuity you might proceed as follows.

First, you transform the domain hh:mm in minutes and then linearly map into an integer number between 0 and $2^k-1$. The transformation function might be $$f(h,m)=\mathrm{round}\left(\frac{60h+m}{60\cdot23+59}\times (2^k-1)\right)$$ You can choose $k=10$. It may be better to use less bits than those required to exactly represent time (exact time requires 11 bit if seconds are not used, but not all $2^{11}$ configurations are used).

Then, you encode $f(h,m)$ with a Gray code. Gray code assure that consecutive numbers are encoded in a way that at most one bit is changed; also, the code is cyclic in the sense that the encoding of $0$ and the encoding of $2^k-1$ differ of one bit only.

Finally, you can evaluate the distance between two time instants through bitwise Hamming distance, which counts the number of different bit between two codes.

If you want more precision, you might use seconds accordingly.

  • $\begingroup$ Great idea! About the contradiction you mentioned, I don't think of it this way - think of a circle, where 0:00 is the starting point. That makes 0:00 be 0 degrees, but also 360 degrees, so it's ok $\endgroup$
    – shakedzy
    Commented Jan 19, 2018 at 12:48
  • $\begingroup$ Actually you have 360 degrees, from 0 to 359. 360 is equivalent to 0, because the representation is modular. Unfortunately contradiction is not contradicted :) $\endgroup$
    – Corrado
    Commented Jan 19, 2018 at 15:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.