Cyclical feature encoding

From wenmin-wu/ds-skills · View interactive version

Cyclical feature encoding is an AI agent skill from the GitHub repository wenmin-wu/ds-skills. Encodes cyclical features (hour, month, day-of-week) using sine/cosine transforms to preserve circular distance.

How to use this skill

Skill file

Cyclical Feature Encoding

Overview

Features like hour-of-day or month wrap around (23→0, Dec→Jan). Standard ordinal encoding creates a false distance between the endpoints. Sine/cosine transforms map cyclical features onto a unit circle, preserving the true circular distance between values.

Quick Start

import numpy as np

def cyclical_encode(values, period):
    """Encode cyclical feature using sin/cos pair.

    Args:
        values: array of numeric values (e.g., hours 0-23)
        period: full cycle length (24 for hours, 12 for months, 7 for weekdays)
    """
    sin = np.sin(2 * np.pi * values / period)
    cos = np.cos(2 * np.pi * values / period)
    return sin, cos

# Usage
df['hour_sin'], df['hour_cos'] = cyclical_encode(df['hour'], 24)
df['month_sin'], df['month_cos'] = cyclical_encode(df['month'], 12)
df['dow_sin'], df['dow_cos'] = cyclical_encode(df['dayofweek'], 7)

Workflow

  1. Identify cyclical features (hour, month, day-of-week, minute, week-of-year)
  2. Apply sin/cos transform with the correct period for each
  3. Drop the original integer column — keep only the sin/cos pair
  4. Use both sin AND cos features (one alone is ambiguous)

Key Decisions

References

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.