2

Does anyone know if/how one can get the indices of the selected samples after undersampling with imblearn's RandomUnderSampler? There used to be the argument "return_indices=True" which was now removed for the new version and supposingly was replaced with an attribute "sample_indices_". However, if I try to use that attribute, it doesn't work (see code below). I'm using imblearn version 0.6.2.

russs = RandomUnderSampler(random_state=0,sampling_strategy={6: 600}).fit(X_train_point,y_train_point)
russs.sample_indices_

AttributeError                            Traceback (most recent call last)
<ipython-input-78-8397ba40f19b> in <module>
      1 russs = RandomUnderSampler(random_state=0,sampling_strategy={6: 600}).fit(X_train_point,y_train_point)
----> 2 russs.sample_indices

AttributeError: 'RandomUnderSampler' object has no attribute 'sample_indices'
ramobal
  • 241
  • 2
  • 9

3 Answers3

4

I also found a workaround. As the undersampling is solely based on the y_vector, one can add a counter-variable instead of the the x-vector/array and write it as follows:

counter=range(0,len(y_train_point))
index,y_resampled=RandomUnderSampler(random_state=0,sampling_strategy={6:600}).fit(counter,y_train_point)
X_resampled=X_train_point[index]
ramobal
  • 241
  • 2
  • 9
1

Also facing this.. Despite the fact that the docs say Deprecated since version 0.4: return_indices is deprecated. Use the attribute sample_indices_ instead. I reverted to 0.5.0 and am able to use the old return_indices=True argument.

pip install imbalanced-learn==0.5.0
BarefootDev
  • 326
  • 3
  • 9
1

I had this problem yesterday and I could access the attribute all right in the end.

Make sure you're not forgetting that underscore in the end, from the error message it seems you have.

It should be

russs.sample_indices_

not

russs.sample_indices
degausser
  • 35
  • 4