> For the complete documentation index, see [llms.txt](https://jgoodman8.gitbook.io/iron-data-science-notebook/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://jgoodman8.gitbook.io/iron-data-science-notebook/ml-datascience/frequent-questions/how-to-deal-with-imbalanced-datasets.md).

# How to deal with imbalanced datasets?

{% hint style="info" %}
*Sources:*

* [*Dealing with Imbalanced Data (Tara Boyle)*](https://towardsdatascience.com/methods-for-dealing-with-imbalanced-data-5b761be45a18)
* [*Having an Imbalanced Dataset? Here Is How You Can Fix I (Will Badr)*](https://towardsdatascience.com/having-an-imbalanced-dataset-here-is-how-you-can-solve-it-1640568947eb)
  {% endhint %}

## Using the correct performance metric

[**Accuracy**](/iron-data-science-notebook/ml-datascience/ml-techniques/metrics.md#accuracy) **is not the best metric** to use when evaluating imbalanced datasets as it can be very misleading. It's better to try:

* [**Confusion Matrix**](/iron-data-science-notebook/ml-datascience/ml-techniques/metrics.md#the-confusion-matrix)
* [**Precision**](/iron-data-science-notebook/ml-datascience/ml-techniques/metrics.md#precision)
* [**Recall**](/iron-data-science-notebook/ml-datascience/ml-techniques/metrics.md#recall)
* [**F1**](/iron-data-science-notebook/ml-datascience/ml-techniques/metrics.md#f-score)

## Resampling Techniques

{% hint style="danger" %}
Always **split into test and train sets BEFORE trying to resample** techniques! And applying resample **ONLY in the training set**.
{% endhint %}

**Oversample**: by adding more copies of the minority class.

**Undersample**: by removing observations from the majority class.&#x20;

## Create synthetic samples

* **SMOTE** (Synthetic Minority Oversampling Technique): uses a **kNN algorithm** to generate new and synthetic data we can use for training our model.

## Apply an ensemble algorithm

{% content-ref url="/pages/-LrGsetZnOqBsnUR8Ct\_" %}
[Ensemble methods](/iron-data-science-notebook/ml-datascience/ml-techniques/ensemble-methods.md)
{% endcontent-ref %}
