微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

RandomOverSampler 似乎不接受对数变换作为我的 y 目标变量

如何解决RandomOverSampler 似乎不接受对数变换作为我的 y 目标变量

我正在尝试对一个小数据集进行随机过采样以进行线性回归。然而,scikit 学习采样 API 似乎不适用于将浮点值作为其目标变量。有没有办法解决这个问题?

这是我的 y_train 值的示例,它们是对数转换的。

3.688879 3.828641 3.401197 3.091042 4.624973

from imblearn.over_sampling import RandomOverSampler
X_over,y_over = RandomOverSampler(random_state=42).fit_sample(X_train,y_train)
--------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-53-036424abd2bd> in <module>
      1 from imblearn.over_sampling import RandomOverSampler

~\Anaconda3\lib\site-packages\imblearn\base.py in fit_resample(self,X,y)
     73             The corresponding label of `X_resampled`.
     74         """
---> 75         check_classification_targets(y)
     76         arrays_transformer = ArraysTransformer(X,y)
     77         X,y,binarize_y = self._check_X_y(X,y)

~\Anaconda3\lib\site-packages\sklearn\utils\multiclass.py in check_classification_targets(y)
    170     if y_type not in ['binary','multiclass','multiclass-multIoUtput',171                       'multilabel-indicator','multilabel-sequences']:
--> 172         raise ValueError("UnkNown label type: %r" % y_type)
    173 
    174 

ValueError: UnkNown label type: 'continuous'

解决方法

重采样策略不适用于回归问题。因此,RandomOverSampler 将不接受 float 类型的目标。不过,有一些方法可以对具有连续目标的数据进行重新采样。一个例子是 reg_resample,它可以像下面这样使用:

from imblearn.over_sampling import RandomOverSampler
from sklearn.datasets import make_regression
from reg_resampler import resampler
import numpy as np


# Create some dummy data for demonstration
X,y = make_regression(n_features=10)
df = np.append(X,y.reshape(100,1),axis=1)

# Initialize the resampler object and generate pseudo-classes
rs = resampler()
y_classes = rs.fit(df,target=10)

# Now resample
X_res,y_res = rs.resample(
    sampler_obj=RandomOverSampler(random_state=27),trainX=df,trainY=y_classes
)

resampler 对象将根据您的目标值生成伪类,然后使用 imblearn 包中的经典重采样对象对您的数据进行重采样。请注意,您传递给 resampler 对象的数据应包含所有数据,包括目标。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。