Hierarchical label encoding

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

Hierarchical label encoding is an AI agent skill from the GitHub repository wenmin-wu/ds-skills. Concatenate multi-level categorical fields into a compound label (Category:Subcategory) for flat multiclass classification

How to use this skill

Skill file

Hierarchical Label Encoding

Overview

When labels have a hierarchical structure (Category → Subcategory → Specific), concatenate levels into a single compound string and LabelEncode for flat multiclass classification. Simpler than hierarchical classifiers, and models can learn the structure implicitly. Decode back to original levels at inference via string splitting.

Quick Start

from sklearn.preprocessing import LabelEncoder
import numpy as np

# Encode: create compound labels
df['compound_label'] = df['Category'] + ':' + df['Subcategory']
le = LabelEncoder()
df['label'] = le.fit_transform(df['compound_label'])
n_classes = len(le.classes_)

# Train model with n_classes output neurons...

# Decode: convert top-K predictions back to original labels
def decode_top_k(probs, le, k=3):
    """Decode top-K softmax predictions to original compound labels."""
    top_k = np.argsort(-probs, axis=1)[:, :k]
    decoded = le.inverse_transform(top_k.flatten())
    return decoded.reshape(top_k.shape)

top3_labels = decode_top_k(probs, le, k=3)
# Each row: ["Category_A:Sub_1", "Category_B:Sub_3", "Category_A:Sub_2"]

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.