# ClusterBasedNormalizer

**Compatibility:** `numerical` data

The `ClusterBasedNormalizer` performs a statistical transformation on numerical data. It approximates the overall column as a mixture of different shapes (components). Then, it normalizes the values and clusters them into the closest component.

![](https://2225246359-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FVGX92M819eIp0rMg5elc%2Fuploads%2FnyB3IinTHkQwoEXElPR4%2Frdt_cluster-based-normalizer_June%2002%202025.png?alt=media\&token=b814e333-0d08-4169-92b8-165274c3e43d)

```python
from rdt.transformers.numerical import ClusterBasedNormalizer
transformer = ClusterBasedNormalizer()
```

## Parameters

**`missing_value_replacement`**: Add this argument to replace missing values during the transform phase

<table data-header-hidden><thead><tr><th width="212"></th><th></th></tr></thead><tbody><tr><td>(default) <code>'mean'</code></td><td>Replace all missing values with the average value.</td></tr><tr><td><code>'random'</code></td><td>Replace missing values with a random value. The value is chosen uniformly at random from the min/max range.</td></tr><tr><td><code>'mode'</code></td><td>Replace all missing values with the most frequently occurring value</td></tr><tr><td><code>&#x3C;number></code></td><td>Replace all missing values with the specified number (<code>0</code>, <code>-1</code>, <code>0.5</code>, etc.)</td></tr><tr><td><code>None</code></td><td>Do not replace missing values. The transformed data will continue to have missing values.</td></tr></tbody></table>

*(deprecated) `model_missing_values`: Use the `missing_value_generation` parameter instead.*

**`missing_value_generation`**: Add this argument to determine how to recreate missing values during the reverse transform phase

<table data-header-hidden><thead><tr><th width="203"></th><th></th></tr></thead><tbody><tr><td>(default) <code>'random'</code></td><td>Randomly assign missing values in roughly the same proportion as the original data.</td></tr><tr><td><code>'from_column'</code></td><td>Create a new column to store whether the value should be missing. Use it to recreate missing values. <em>Note: Adding extra columns uses more memory and increases the RDT processing time.</em></td></tr><tr><td><code>None</code></td><td>Do not recreate missing values.</td></tr></tbody></table>

**`max_clusters`**: The maximum number of components to create when estimating the shape of the overall column.

<table data-header-hidden><thead><tr><th width="199.5"></th><th></th></tr></thead><tbody><tr><td>(default) 10</td><td>Cap the number of clusters to 10</td></tr><tr><td><code>&#x3C;number></code></td><td>Cap the number of clusters to the specified value (eg. <code>5</code>, <code>20</code>, etc.). This must be a whole number (integer).</td></tr></tbody></table>

**`weight_threshold`**: The minimum weight that is needed to possibly form a new component. Note that the total number of components is still capped by the **`max_clusters`** argument above.

<table data-header-hidden><thead><tr><th width="202.5"></th><th></th></tr></thead><tbody><tr><td>(default) <code>0.005</code></td><td>Create a new component when the weight is <code>0.005</code> or above.</td></tr><tr><td><code>&#x3C;number></code></td><td>Create a new component when the weight is at or above <code>&#x3C;number></code> (eg. <code>0.001</code>, <code>0.01</code>, etc.)</td></tr></tbody></table>

**`enforce_min_max_values`**: Add this argument to allow the transformer to learn the min and max allowed values from the data.

<table data-header-hidden><thead><tr><th width="192.68148746968473"></th><th></th></tr></thead><tbody><tr><td>(default) <code>False</code></td><td>Do not learn any min or max values from the dataset. When reverse transforming the data, the values may be above or below what was originally present.</td></tr><tr><td><code>True</code></td><td>Learn the min and max values from the input data. When reverse transforming the data, any out-of-bounds values will be clipped to the min or max value.</td></tr></tbody></table>

**`learn_rounding_scheme`**: Add this argument to allow the transformer to learn about rounded values in your dataset.

<table data-header-hidden><thead><tr><th width="199.5"></th><th></th></tr></thead><tbody><tr><td>(default) <code>False</code></td><td>Do not learn or enforce any rounding scheme. When reverse transforming the data, there may be many decimal places present.</td></tr><tr><td><code>True</code></td><td>Learn the rounding rules from the input data. When reverse transforming the data, round the number of digits to match the original.</td></tr></tbody></table>

## FAQ

<details>

<summary>When should I use this transformer?</summary>

Your decision to use this transformer is based on how you plan to use the transformed data. For example, some data science algorithms work better on normalized data. If you're planning to use such an algorithm, this transformer might be a good pre-processing step.

</details>

<details>

<summary>Which algorithm does this transformer use to cluster the data?</summary>

This transformer uses Bayesian Gaussian Mixture Models. Read more about them [here](https://en.wikipedia.org/wiki/Mixture_model#Gaussian_mixture_model).

</details>

<details>

<summary>How do the <code>max_clusters</code> or <code>weight_threshold</code> parameters affect the transformation?</summary>

Changing these values affects the accuracy and performance of the transformation.

* Setting a high number of `max_clusters` or a low `weight_threshold` will create more components. This may make the clustering more accurate but will take more time. (In the extreme case, it may over-fit to the original data.)
* Setting a lower number of `max_clusters` or a high `weight_threshold` will create fewer components. This may make clustering less accurate but will improve the time it takes for the algorithm to compete.

</details>

<details>

<summary>When are the min/max values and rounding scheme enforced?</summary>

Using these options will enforce the min/max values or rounding scheme when reverse transforming your data. Use these parameters if you want to recover data in the same format as the original.

</details>

<details>

<summary>When is it necessary to model missing values?</summary>

When setting the `missing_value_generation` parameter, consider whether the "missingness" of the data is something important. For example, maybe the user opted out of supplying the info on purpose, or maybe a missing value is highly correlated with another column your dataset. If "missingness" is something you want to account for, you should model missing values.

</details>
