Skip to content

Instantly share code, notes, and snippets.

@bkaankuguoglu
Last active April 6, 2021 19:44
Show Gist options
  • Select an option

  • Save bkaankuguoglu/a8df9ee3a6b89d7c958854bee364efd9 to your computer and use it in GitHub Desktop.

Select an option

Save bkaankuguoglu/a8df9ee3a6b89d7c958854bee364efd9 to your computer and use it in GitHub Desktop.
def generate_cyclical_features(df, col_name, period, start_num=0):
kwargs = {
f'sin_{col_name}' : lambda x: np.sin(2*np.pi*(df[col_name]-start_num)/period),
f'cos_{col_name}' : lambda x: np.cos(2*np.pi*(df[col_name]-start_num)/period)
}
return df.assign(**kwargs).drop(columns=[col_name])
df_features = generate_cyclical_features(df_features, 'hour', 24, 0)
# df_features = generate_cyclical_features(df_features, 'day_of_week', 7, 0)
# df_features = generate_cyclical_features(df_features, 'month', 12, 1)
# df_features = generate_cyclical_features(df_features, 'week_of_year', 52, 0)
df_features
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment