Skip to content
Back to skills

Cyclical Feature Encoding

ASecurity

Encodes cyclical features (hour, month, day-of-week) using sine/cosine transforms to preserve circular distance.

  • 61 stars
  • 0 votes
  • 0 copies
  • 1 view
  • Added September 12, 2026
developmentpythongo

Works with

  • cli

Security analysis

A100/100

Scanned September 12, 2026

npx -y skills add wenmin-wu/ds-skills --skill cyclical-feature-encoding --agent claude-code

Installs into .claude/skills of the current project.

Are you the author of Cyclical Feature Encoding?

Add the live security badge to your README. It updates with every re-scan.

Security grade badge for Cyclical Feature Encoding
[![Security: A — Skills Directory](https://www.skillsdirectory.com/api/skills/wenmin-wu-cyclical-feature-encoding/badge)](https://www.skillsdirectory.com/skills/wenmin-wu-cyclical-feature-encoding)

More formats (shields.io, HTML) on the badges page. Keep it an A: scan every change in CI with Pro.

Download with Pro
SKILL.md
---
name: tabular-cyclical-feature-encoding
description: >
  Encodes cyclical features (hour, month, day-of-week) using sine/cosine transforms to preserve circular distance.
---
# 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

```python
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

- **Always use both sin and cos**: sin(0) = sin(π) — without cos, midnight and noon look identical
- **Period must be exact**: hours → 24, months → 12, weekdays → 7, minutes → 60
- **Tree models**: May not benefit much — trees can learn splits on integer features directly
- **Linear/NN models**: Significant improvement — they cannot learn circular boundaries otherwise

## References

- 2019 Data Science Bowl (Kaggle)
- Source: [quick-and-dirty-regression](https://www.kaggle.com/code/artgor/quick-and-dirty-regression)

Attribution

Is this your skill, or is something wrong with this listing? Request removal or report an issue. Author removals are honored within 72 hours.

Comments

Loading comments…

Newsletter

Get the weekly list of safe new skills, and the ones we pulled.