pythondata-processingimbalanced-data

Get error: unexpected keyword argument 'random_state' when using TomekLinks


My code is:

  undersample = TomekLinks(sampling_strategy='majority', n_jobs= -1,  random_state = 42)
  X_tl, y_tl = undersample.fit_resample(X, y)

When I run it, I get this error:

TypeError: __init__() got an unexpected keyword argument 'random_state'

My package version is:

 imbalanced-learn==0.9.0

although in the documentation this parameter exists:

random_state : int, RandomState instance or None, optional (default=None)

when I check the constructor in _tomek_links.py, I don't see the random state field:

  @_deprecate_positional_args
    def __init__(self, *, sampling_strategy="auto", n_jobs=None):
        super().__init__(sampling_strategy=sampling_strategy)
        self.n_jobs = n_jobs

Solution

  • I think, you're looking at the wrong documentation. That one is for version 0.3.0-dev, so I checked: https://imbalanced-learn.org/stable/references/generated/imblearn.under_sampling.TomekLinks.html -- this parameter has been deprecated in a newer version 0.9.0.

    Also as the documentation goes, seems you have to specify it in make_classification function as below:

    X, y = make_classification(n_classes=2, class_sep=2,
                               weights=[0.1, 0.9], 
                               n_informative=3, n_redundant=1, 
                               flip_y=0, n_features=20, 
                               n_clusters_per_class=1, 
                               n_samples=1000, random_state=10
                              )