3
$\begingroup$

I am trying to do a simple regression (either polynomial or support vector regression) for solar power prediction, as a project to learn machine learning. However the data I have is time series data of the following format:

| Year | Month of Year | Day of Month | Hour of Day | Temperature | Solar Power

Currently,
Month of Year = 1 to 12
Day of Month = 1 to 31
Hour of Day = 0 to 23

How do I incorporate the knowledge that January this year is as close to December last year as is it to February this year? Same goes for the hour of the day. Even if incorporating this knowledge may not be required for this particular case for solar power, I would still love to learn how.

I searched previous questions and found that, for something like an angle, we could incorporate the knowledge that 359 degree is close to 0 degree, by applying a sine function on angle data. I could possibly apply the same to my time variables by first scaling them to lie within 0 to 360 range, but I fear that it will introduce extra un-warranted knowledges (its slope is not constant, and it is symmetrical).

Any suggestions/comments on how to do this properly? Thanks.

Edit: More details

If I scale the hours from 0,24 to 0,180 and apply sine, then 0 will be close to 24. However, 1 will also be close to 23, 2 with 22, 3 with 21 and so on, which is unwarranted. Also, the rate of change is much faster around 0 and 180 degrees for the sine function, than around 90 degrees, which means, the difference between 23 and 0 hours is much more than between 11 and 12 hours.

Ideally, I would like to lay 0-24 in a ring (like in a watch), and have some function convert it into some numbers such that, the numerical difference between those numbers would be equal to distance between those numbers around the ring. I read the answer about using both cosine and sine, but its still over my head. Maybe the answer lies there. If yes, could you please explain a bit in this context, how that might solve the problem. Thanks.

$\endgroup$
3
  • 1
    $\begingroup$ You can clearly, for example, scale the hours of the day on the 0,23 interval in the same way as those other answers suggest to scale the degrees of the circle so I can't quite see where your difficulty is given you understand that technique. For example the second answer here - stats.stackexchange.com/questions/4783/… says exactly what to do, just make the wavelength variable 24 for hours of the day, etc. $\endgroup$ Commented May 10, 2016 at 11:31
  • $\begingroup$ @RobertdeGraaf Thanks for your reply. I have edited the question and added details, to explain what I am looking for. Thanks. $\endgroup$
    – rajendra
    Commented May 10, 2016 at 13:04
  • $\begingroup$ Use a periodic kernel. $\endgroup$
    – Sycorax
    Commented May 10, 2016 at 14:31

2 Answers 2

4
$\begingroup$

All I needed to do was convert the linear 0-24 (for hours of the day) and 0-365 for days of the year, from linear coordinates into polar coordinates by x = r*cos(theta), y=r*sin(theta) transformation. So, each of those linear features will be split into two features, their x-coordinate and y-coordinate such that they lie in a circle.

Hope this will help somebody having similar doubt in future.

$\endgroup$
0
$\begingroup$

If working with Python, this can be done automatically with the open-source library Feature-engine:

import pandas as pd
from feature_engine.creation import CyclicalFeatures

df = pd.DataFrame({
    'day': [6, 7, 5, 3, 1, 2, 4],
    'months': [3, 7, 9, 12, 4, 6, 12],
    })

cyclical = CyclicalFeatures(variables=None, drop_original=False)

X = cyclical.fit_transform(df)

print(X)

   day  months       day_sin   day_cos    months_sin    months_cos
0    6       3 -7.818315e-01  0.623490  1.000000e+00  6.123234e-17
1    7       7 -2.449294e-16  1.000000 -5.000000e-01 -8.660254e-01
2    5       9 -9.749279e-01 -0.222521 -1.000000e+00 -1.836970e-16
3    3      12  4.338837e-01 -0.900969 -2.449294e-16  1.000000e+00
4    1       4  7.818315e-01  0.623490  8.660254e-01 -5.000000e-01

More details in the CyclicalFeatures documentation.

$\endgroup$

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.