liveBook · Manning

One-hot encoding

As explained in Fight Fraud with Machine Learning

One-hot encoding represents each category of a categorical variable as a separate binary feature.

One-hot encoding turns each category into a separate binary feature, letting categorical transaction types enter a classical fraud-detection model without imposing a numerical order. section 3.3.3

A practical workflow is:

  • Compare encoding choices. Ordinal encoding is unsuitable when transaction types have no inherent order; target encoding may miss nuanced differences between CASH_OUT and TRANSFER. section 3.3.3
  • Create one column per transaction type, such as CASH_IN, CASH_OUT, DEBIT, PAYMENT, and TRANSFER, using integer binary values. section 3.3.3
  • Append the generated columns to the dataframe, select relevant features, discard the original type column, and sort feature names for consistent training and inference. section 3.3.3
  • Engineer a categorical feature from the first character of nameDest, inspect its fraud distribution, then encode categoryDest the same way. section 3.6.1

Encoding transaction type with pandas

Use pd.get_dummies(df["type"]).astype(int) to create integer indicator columns, then concatenate them with df using pd.concat(..., axis=1).

How Fight Fraud with Machine Learning covers it

How deep it goes
Compares encoding choices and provides pandas implementation, while deferring deeper encoding theory and model internals.
Examples
Uses transaction types and a nameDest-derived categoryDest feature as encoding examples.
What it assumes
Assumes a dataframe containing categorical values and teaches conversion from engineered categories to numerical features.
What you can do after
Apply it via pandas to append binary category columns and produce consistently ordered model features.
How it connects
Connects categorical variables with ordinal encoding, target encoding, binary encoding, feature engineering, and model inference.

Also explained in

Compare how 10 books explain one-hot encoding →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Fight Fraud with Machine Learning. The section links point to the parts of the book it draws on.