Up-/downsampling 使用 One vs. rest 分类器
Up-/downsampling with One vs. rest classifier
我有一个数据集(tf-idf 加权词),其中有多个 类 我试图预测。我的 类 不平衡。我想使用来自 sklearn 的 OneVsRestClassifier 对一些分类器(例如多项式朴素贝叶斯)使用 One vs. rest 分类方法。
此外,我想使用不平衡学习包(很可能是上采样和下采样的组合之一)来增强我的数据。使用不平衡学习的正常方法是:
from imblearn.combine import SMOTEENN
smote_enn = SMOTEENN(random_state=0)
X_resampled, y_resampled = smote_enn.fit_resample(X, y)
我现在有一个数据集,每个标签的案例数大致相同。然后我会在重采样数据上使用分类器。
from sklearn.multiclass import OneVsRestClassifier
from sklearn.naive_bayes import MultinomialNB
ovr = OneVsRestClassifier(MultinomialNB())
ovr.fit(X_resampled, y_resampled)
但是:现在每个标签在贴合的时候都存在很大的不平衡,因为我总共有50多个标签。正确的?我想我需要为每个标签应用 up-/downsampling 方法,而不是一开始就应用一次。如何对每个标签使用重采样?
根据评论中的讨论,你想要的可以这样完成:
from sklearn.naive_bayes import MultinomialNB
from imblearn.combine import SMOTEENN
# Observe how I imported Pipeline from IMBLEARN and not SKLEARN
from imblearn.pipeline import Pipeline
from sklearn.multiclass import OneVsRestClassifier
# This pipeline will resample the data and
# pass the output to MultinomialNB
pipe = Pipeline([('sampl', SMOTEENN()),
('clf', MultinomialNB())])
# OVR will transform the `y` as you know and
# then pass single label data to different copies of pipe
# multiple times (as many labels in data)
ovr = OneVsRestClassifier(pipe)
ovr.fit(X, y)
代码说明:
第 1 步:OneVsRestClassifier
将创建 y
的多个列。每个标签一个,其中该标签为正,所有其他标签为负。
第 2 步:对于每个标签,OneVsRestClassifier
将克隆提供的 pipe
估计器并将单独的数据传递给它。
第 3 步:
一个。 pipe
的每个副本都会得到一个不同版本的 y
,它会传递到其中的 SMOTEENN
,因此会进行不同的采样以平衡那里的 类。
b。 pipe
(clf
) 的第二部分将根据需要为每个标签获取平衡数据集。
第四步:在预测时间内,采样部分会被关闭,所以数据会原样到达clf
。 sklearn 管道不处理该部分,所以这就是我使用 imblearn.pipeline
.
的原因
希望这对您有所帮助。
我有一个数据集(tf-idf 加权词),其中有多个 类 我试图预测。我的 类 不平衡。我想使用来自 sklearn 的 OneVsRestClassifier 对一些分类器(例如多项式朴素贝叶斯)使用 One vs. rest 分类方法。
此外,我想使用不平衡学习包(很可能是上采样和下采样的组合之一)来增强我的数据。使用不平衡学习的正常方法是:
from imblearn.combine import SMOTEENN
smote_enn = SMOTEENN(random_state=0)
X_resampled, y_resampled = smote_enn.fit_resample(X, y)
我现在有一个数据集,每个标签的案例数大致相同。然后我会在重采样数据上使用分类器。
from sklearn.multiclass import OneVsRestClassifier
from sklearn.naive_bayes import MultinomialNB
ovr = OneVsRestClassifier(MultinomialNB())
ovr.fit(X_resampled, y_resampled)
但是:现在每个标签在贴合的时候都存在很大的不平衡,因为我总共有50多个标签。正确的?我想我需要为每个标签应用 up-/downsampling 方法,而不是一开始就应用一次。如何对每个标签使用重采样?
根据评论中的讨论,你想要的可以这样完成:
from sklearn.naive_bayes import MultinomialNB
from imblearn.combine import SMOTEENN
# Observe how I imported Pipeline from IMBLEARN and not SKLEARN
from imblearn.pipeline import Pipeline
from sklearn.multiclass import OneVsRestClassifier
# This pipeline will resample the data and
# pass the output to MultinomialNB
pipe = Pipeline([('sampl', SMOTEENN()),
('clf', MultinomialNB())])
# OVR will transform the `y` as you know and
# then pass single label data to different copies of pipe
# multiple times (as many labels in data)
ovr = OneVsRestClassifier(pipe)
ovr.fit(X, y)
代码说明:
第 1 步:
OneVsRestClassifier
将创建y
的多个列。每个标签一个,其中该标签为正,所有其他标签为负。第 2 步:对于每个标签,
OneVsRestClassifier
将克隆提供的pipe
估计器并将单独的数据传递给它。第 3 步:
一个。
pipe
的每个副本都会得到一个不同版本的y
,它会传递到其中的SMOTEENN
,因此会进行不同的采样以平衡那里的 类。b。
pipe
(clf
) 的第二部分将根据需要为每个标签获取平衡数据集。第四步:在预测时间内,采样部分会被关闭,所以数据会原样到达
clf
。 sklearn 管道不处理该部分,所以这就是我使用imblearn.pipeline
. 的原因
希望这对您有所帮助。