Created
February 15, 2021 21:09
-
-
Save ahue/e152154f6eab7bffc43b628a389f4ba9 to your computer and use it in GitHub Desktop.
Because I tend to forget... building cyclical features
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
| 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