我正在使用不平衡学习来过度采样我的数据。我想知道在使用过采样方法之后,每个类中有多少个条目。 这段代码运行良好:
import imblearn.over_sampling import SMOTE
from collections import Counter
def oversample(x_values, y_values):
oversampler = SMOTE(random_state=42, n_jobs=-1)
x_oversampled, y_oversampled = oversampler.fit_resample(x_values, y_values)
print("Oversampling training set from {0} to {1} using {2}".format(dict(Counter(y_values)), dict(Counter(y_over_sampled)), oversampling_method))
return x_oversampled, y_oversampled
但是我改用了管道,这样我就可以使用GridSearchCV来找到最佳的过采样方法(在ADASYN、SMOTE和BorderlineSMOTE之外)。因此,我从未实际调用fit\u resample,并使用以下内容丢失输出:
from imblearn.pipeline import Pipeline
from sklearn.preprocessing import MinMaxScaler
from sklearn.ensemble import RandomForestClassifier
pipe = Pipeline([('scaler', MinMaxScaler()), ('sampler', SMOTE(random_state=42, n_jobs=-1)), ('estimator', RandomForestClassifier())])
pipe.fit(x_values, y_values)
上采样是有效的,但是我会丢失训练集中每个类有多少个条目的输出。你知道吗
有没有一种方法可以获得与第一个使用管道的示例类似的输出?你知道吗
理论上是的。当安装过采样器时,将创建一个属性
sampling_strategy_
,其中包含调用fit_resample
时要生成的少数类的样本数。您可以使用它获得与上述示例类似的输出。以下是基于您的代码修改的示例:相关问题 更多 >
编程相关推荐