0
$\begingroup$

I have a multi-label dataset, whose label distribution looks something like this, with label on x-axis and number of rows it occurs in the dataset in y-axis.

## imports
import numpy as np
import pandas as pd
%matplotlib inline
from sklearn.datasets import make_multilabel_classification

## creating dummy data
X, y = make_multilabel_classification(n_samples=100_000, n_features=2,
                                      n_classes=100, n_labels=10, random_state=42)
X.shape, y.shape
   ((100000, 2), (100000, 100))

## making it a dataframe
final_df = pd.merge(left=pd.DataFrame(X), right=pd.DataFrame(y), left_index=True, right_index=True).copy()
final_df.rename(columns={'0_x':'input_1', '1_x':'input_2', '0_y':0, '1_y':1}, inplace=True)
final_df.columns = final_df.columns.astype(str)

## plotting the counts of each label:
labels = [str(i) for i in range(100)]
value_counts = final_df.loc[:, labels].sum(axis=0)
value_counts.plot(kind='line')

enter image description here
So, there are labels that have occurred only in couple hundred rows, while there are also labels, occurred in 19K+ rows.

I would now like to undersample it, to make the number of rows each label appear in the dataset, look something like this:
enter image description here
So, a label has to occur, at max in only around 2000 rows(+100 is acceptable), while all the under occurred labels has to be left as is.

I have gone through various under-sampling methods that imbalanced-learn provides, but none of them seemed to support multi-label datasets.

How do I do this?

$\endgroup$
13
  • 1
    $\begingroup$ Unbalanced classes are almost certainly not a problem, and oversampling will not solve a non-problem: Are unbalanced datasets problematic, and (how) does oversampling (purport to) help? $\endgroup$ Commented Oct 26, 2021 at 10:19
  • $\begingroup$ I don't have a class-imblance, i have imbalance in labels, as depicted in first plot in question. $\endgroup$ Commented Oct 26, 2021 at 10:32
  • $\begingroup$ @StephanKolassa, is it completely fine to have label-imbalance too? $\endgroup$ Commented Oct 26, 2021 at 10:44
  • 1
    $\begingroup$ Ah. So "labels" are not your target variable? No, imbalance in predictors ("labels", if I understand you correctly) is not a problem, either, except in some very specific circumstances, which probably do not apply to your situation (e.g., ANOVA with highly different variances between groups). $\endgroup$ Commented Oct 26, 2021 at 10:46
  • $\begingroup$ i presume you think "labels" is the name of my target variable. what i have is, one input text column and all my outputs are binary, indicating if the current row belongs to a variable or not. so, each row can belong to 1 or more labels. $\endgroup$ Commented Oct 26, 2021 at 10:49

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.