Last active
May 26, 2022 20:34
-
-
Save cpatdowling/1ff16170787b00232babcec72d1cfe27 to your computer and use it in GitHub Desktop.
This (inefficient) function accepts a python datetime and shows how to transform repeating features (day of the year, weekday, hour) on the numberline into continuous features, (ie days 1 and 365 are topologically close to one another at the top of a circle)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from datetime import datetime as dt | |
import calendar | |
import numpy as np | |
def num_to_circle(z): | |
#this functions accepts a number between 0 and 1 and projects it onto x, y coordinates on the unit circle | |
x = np.cos(2*np.pi*z) | |
y = np.sin(2*np.pi*z) | |
return((x, y)) | |
def transform_date_to_feature(y): | |
#this function accepts a datetime object and converts the following to x, y coordinates on the unit circle: | |
#minute of the day | |
#hour of the day | |
#day of the week | |
#day of the year | |
#month of the year | |
date_ints = np.array([y.minute, y.hour+1, y.weekday()+1, int(y.strftime('%j'))+1, y.month ]) | |
#normalize these intergers to between 0,1, probably some off by one errors here depend on | |
if calendar.isleap(y.year): | |
normed_ints = np.array([1/60.0, 1/24.0, 1/7.0, 1/366.0, 1/12.0])*date_ints | |
else: | |
normed_ints = np.array([1/60.0, 1/24.0, 1/7.0, 1/365.0, 1/12.0])*date_ints | |
output = [] | |
for i in normed_ints: | |
output.append(num_to_circle(i)) | |
return(output) | |
x = dt.now() | |
print(transform_date_to_feature(x)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment