I'm new to machine learning and advanced statistics so please be patient with my basic question.

I am currently trying to model a data set I have with bunch of time data as independent variables (i.e. hour, day, month and year). With a continuous variable as the outcome, i.e. money.

My understanding is that you must convert categorical variables for knn to work.

Does that mean I must somehow convert my time data? The approach I found online was basically convert every level of a categorical variable to be a binary. But given the many possible levels, doesn't this create the dimensionality problem?

Is this a common problem with knn? Should I just use a different algorithm? Or should I seek to combine my time data into one continuous variable somehow?


1 Answer 1


Designing a distance metric is a common problem in KNN. A common distance metric for categorical variables is the Gower distance.

However, it's more efficient to merge your hour/day/month/year variables into a single date variable. A very common implementation is Excel date serial number (google details if you want to learn more), it's used in finance and many other applications.

If you were able to do the merging, you'd have a numeric variable representing your date. You can then use KNN easily with this date variable.

  • 1
    $\begingroup$ A couple good options here (+1). One thing to think about when deciding whether to merge the hour/day/etc. into a single value is how the output depends on these fields. For example, keeping a separate hour feature lets you treat all examples that occur near 8am as being similar along this dimension. If the output fluctuates hourly, this can be useful. Collapsing to a single time value would treat these examples as unique, obscuring this relationship. OTOH, if the output is a long-timescale trend, hours would be irrelevant and including them as a dimension would just add noise. $\endgroup$
    – user20160
    Commented Jan 30, 2017 at 6:49

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.