# Target-encode a CSV categorical column

Replace a CSV categorical column with the per-category mean of a numeric target. Supports smoothing, leave-one-out, replace or append output, and unknown handling.

## Run it

- **CLI:** `gizza tool target-mean-encoder "city,churn
A,1
B,1
A,0
B,1
C,1
B,0" 'category=city' 'target=churn'`
- **Web:** https://gizza.ai/tools/target-mean-encoder/
- **Agents:** machine-readable descriptor (parameters JSON Schema) at https://gizza.ai/tools/target-mean-encoder/tool.json

## Inputs

- `data` — CSV data _(field)_
- `category` — Category column (name or 1-based number) _(field)_
- `target` — Target column (name or 1-based number) _(field)_
- `smoothing` — Smoothing (m) _(field)_
- `leave_one_out` — Leave-one-out (exclude own row) _(field)_
- `output` — Output _(field)_
- `unknown` — Unknown / blank category _(field)_
- `decimals` — Decimal places _(field)_
- `has_header` — First row is a header _(field)_
- `delimiter` — CSV delimiter _(field)_

## Output

- Encoded CSV (text)

## Query parameters

Open the tool pre-filled and auto-run via URL:

- `data` — CSV data
- `category` — Category column (name or 1-based number)
- `target` — Target column (name or 1-based number)
- `smoothing` — Smoothing (m)
- `leave_one_out` — Leave-one-out (exclude own row)
- `output` — Output
- `unknown` — Unknown / blank category
- `decimals` — Decimal places
- `has_header` — First row is a header
- `delimiter` — CSV delimiter

Example: `https://gizza.ai/tools/target-mean-encoder/?data=city%2Cchurn%0AA%2C1%0AB%2C1%0AA%2C0%0AB%2C1%0AC%2C1%0AB%2C0&category=city&target=churn&smoothing=0&leave_one_out=true&output=replace&unknown=global-mean&decimals=4&has_header=true&delimiter=comma`

---

## About this tool

Target Mean Encoder replaces a categorical CSV column with the mean of a numeric
target for each category — also called mean, impact, or likelihood encoding. It
turns a high-cardinality text column (city, product, merchant, ZIP) into a single
model-ready numeric feature without one-hot exploding your column count.

Pick the category column and the numeric target column, and each category becomes
the average target value for its rows. Optional **m-estimate smoothing** blends
rare categories toward the overall mean, and **leave-one-out** excludes each row's
own target so an in-place encoding leaks less signal into the model.

### Worked example

CSV:

```csv
city,churn
A,1
B,1
A,0
B,1
C,1
B,0
```

With category `city`, target `churn`, replace output, and 3 decimals, each city
becomes its churn rate — A = 1/2 = 0.500, B = 2/3 = 0.667, C = 1/1 = 1.000:

```text
city,churn
0.500,1
0.667,1
0.500,0
0.667,1
1.000,1
0.667,0
```

Switch output to **append** to keep the original column and add
`city_target_enc` instead of overwriting `city`.

## Limits & edge cases

- The target column must be numeric. Non-numeric target cells are ignored when
  computing a category's mean; a column with no numbers at all is an error.
- Smoothing uses the m-estimate blend `(sum + m·prior) / (count + m)`, where
  `prior` is the global target mean. `m = 0` gives the raw category mean.
- With leave-one-out on, a category that appears in only one row has no other
  rows to average, so that row falls back to your unknown-category setting.
- Blank category cells (and categories with no usable statistics) use the
  **Unknown / blank category** option: the global mean, `NaN`, or zero.
- Columns can be chosen by header name, or by 1-based number when the header
  checkbox is off. The category and target columns must be different.
- This tool encodes one category column against one target per run.

## FAQ

<details>
<summary>What is target (mean) encoding and when should I use it?</summary>

Target encoding replaces each category with the average of a numeric target for
that category's rows. It is handy for high-cardinality categorical features where
one-hot encoding would add too many columns, giving models a compact numeric
signal instead.

</details>

<details>
<summary>How does smoothing help with rare categories?</summary>

A category seen only once or twice has a noisy mean. Smoothing (the m-estimate)
pulls those means toward the global target mean: the higher the smoothing weight
`m`, the more a small category is shrunk toward the overall average, which reduces
overfitting to rare values.

</details>

<details>
<summary>What does leave-one-out do?</summary>

Leave-one-out excludes each row's own target value from its category statistics
before encoding that row. This reduces target leakage when you encode a training
set in place, because a row can no longer "see" its own label through the encoded
value.

</details>

<details>
<summary>What happens to blank or unseen categories?</summary>

Rows whose category cell is blank — or whose category has no usable target
statistics — take the value from the **Unknown / blank category** setting. You can
use the global mean (a safe default), `NaN` (to flag them for later handling), or
zero.

</details>

## Related tools

- [Rebalance an imbalanced CSV label column](https://gizza.ai/tools/class-rebalancer/): Rebalance an imbalanced CSV label column by seeded random over- or under-sampling of whole rows toward a target class ratio. Reproducible, in-browser, no synthetic rows.
- [Base32 Encoder / Decoder](https://gizza.ai/tools/base32-codec/): Free Base32 encoder and decoder in your browser — RFC 4648, base32hex, Crockford and z-base-32 variants, hex byte I/O, optional padding. Private, no sign-up.
- [Base58 Encoder / Decoder](https://gizza.ai/tools/base58-codec/): Encode text or bytes to Base58 and decode Base58 back in your browser — Bitcoin/IPFS, Ripple (XRP) and Flickr alphabets, hex I/O. Free, private, no sign-up.
- [Base62 Encoder / Decoder](https://gizza.ai/tools/base62-codec/): Encode text, hex bytes, or numbers to Base62 (0-9A-Za-z) and decode back — URL-safe, no padding, arbitrary-precision numbers. Free, private, in your browser.
- [Base85 Encoder / Decoder](https://gizza.ai/tools/base85-codec/): Encode text or hex bytes to Base85 and decode Base85 back. Supports Ascii85, ZeroMQ Z85, and RFC 1924 variants with optional Adobe framing. Free, private, browser-local.
