Skip to content
Back to skills

Frequency Encoding

ASecurity

Adds each feature's value-count frequency as a new column, enabling tree models to split on how common or rare a value is.

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

Security analysis

A100/100

Scanned September 12, 2026

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

Installs into .claude/skills of the current project.

Are you the author of Frequency Encoding?

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

Security grade badge for Frequency Encoding
[![Security: A — Skills Directory](https://www.skillsdirectory.com/api/skills/wenmin-wu-frequency-encoding/badge)](https://www.skillsdirectory.com/skills/wenmin-wu-frequency-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-frequency-encoding
description: >
  Adds each feature's value-count frequency as a new column, enabling tree models to split on how common or rare a value is.
---
# Frequency Encoding

## Overview

For categorical or high-cardinality numeric features, the raw value itself may be less informative than how often it appears. Frequency encoding replaces (or augments) each value with its count in the dataset. Tree models can then split on "values appearing fewer than N times" — a pattern that captures rarity without target leakage. Compute counts on train + real test combined for consistency.

## Quick Start

```python
import pandas as pd

def frequency_encode(train, test, col):
    # Count on combined data for consistent frequencies
    combined = pd.concat([train[[col]], test[[col]]], axis=0)
    freq = combined[col].value_counts()
    train[col + "_FE"] = train[col].map(freq)
    test[col + "_FE"]  = test[col].map(freq).fillna(0)

for col in feature_cols:
    frequency_encode(train, test, col)
```

## Workflow

1. Concatenate train and test (or real test only if synthetic rows exist)
2. Compute `value_counts()` for each feature
3. Map counts back as a new `_FE` column
4. Fill missing values with 0 (unseen values in test)
5. Optionally downcast to `uint8`/`uint16` for memory savings

## Key Decisions

- **Combined vs train-only**: Combined gives consistent counts; train-only avoids test leakage in strict setups
- **Exclude synthetics**: If test has fake rows, exclude them from frequency counts
- **Log transform**: `np.log1p(freq)` compresses skewed distributions
- **Pair with original**: Keep the raw feature alongside the frequency feature
- **Memory**: Downcast to smallest uint type fitting `max(freq)`

## References

- [200 Magical Models - Santander - [0.920]](https://www.kaggle.com/code/cdeotte/200-magical-models-santander-0-920)

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.