Anomaly flag imputation

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

Anomaly flag imputation is an AI agent skill from the GitHub repository wenmin-wu/ds-skills. Detects sentinel anomaly values in numeric columns, creates a boolean flag feature, then replaces the sentinel with NaN for proper imputation.

How to use this skill

Skill file

Anomaly Flag Imputation

Overview

Real-world datasets often encode missing or special-status values as sentinel numbers (e.g., 365243 for "not employed", 999 for "unknown", -1 for "missing"). These break statistical features (mean, std) and mislead models. This pattern: (1) creates a binary flag column indicating the anomaly, (2) replaces the sentinel with NaN so imputers and models handle it correctly. The flag preserves the information that the value was special while the NaN lets downstream code treat it as missing.

Quick Start

import numpy as np
import pandas as pd

def flag_and_replace(df, col, sentinel):
    """Flag sentinel value as boolean feature, replace with NaN."""
    flag_col = f'{col}_ANOMALY'
    df[flag_col] = (df[col] == sentinel).astype(int)
    df[col] = df[col].replace({sentinel: np.nan})
    return df

# Apply to both train and test identically
for df in [train, test]:
    df = flag_and_replace(df, 'DAYS_EMPLOYED', 365243)
    df = flag_and_replace(df, 'DAYS_LAST_PHONE_CHANGE', 0)

Workflow

  1. Inspect numeric columns for suspicious spikes (histogram or value_counts)
  2. Identify sentinel values — often round numbers, max/min outliers, or documented codes
  3. Create a boolean flag column (col_ANOMALY) before replacing
  4. Replace sentinel with np.nan
  5. Apply identical transform to train and test

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.