Skip to content

Instantly share code, notes, and snippets.

@ahue
Created February 15, 2021 21:09
Show Gist options
  • Select an option

  • Save ahue/e152154f6eab7bffc43b628a389f4ba9 to your computer and use it in GitHub Desktop.

Select an option

Save ahue/e152154f6eab7bffc43b628a389f4ba9 to your computer and use it in GitHub Desktop.
Because I tend to forget... building cyclical features
import numpy as np
from typing import Union
def cyclical_feature(x: Union(float|np.array), max_x: float):
"""
Transforms a linear scale feature to a feature pair that represent a cyclical one.
Assumes that the feature is in interval [0, max_x]
See also https://towardsdatascience.com/cyclical-features-encoding-its-about-time-ce23581845ca
Parameters:
x (float): The value to transform
max_x (float): The maximum value x can take
Returns:
cyclical_x (float|np.array): The cyclical value of x
"""
return [np.sin(2 * np.pi * x / max_x),
np.cos(2 * np.pi * x / max_x)]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment