我使用不平衡数据通过scikit-learn执行分类并提高模型的准确性,我使用SMOTE技术创建了更多的合成数据。我想知道用GridSearch实现超参数优化的最佳时机。我应该只使用原始数据还是使用original+synthetic数据?
发布于 2019-10-31 01:48:19
你是在说如何在sklearn的GridSearchCV中特别使用像SMOTE这样的过采样方法吗?我做这个假设是因为你在帖子上有一个scikit-learn标签。
如果是这样,您可以使用管道对象将过采样的SMOTE数据传递到GridSearchCV中。如果您希望通过GridSearchCV使用交叉验证方案来拟合模型,sklearn将自动正确地处理每个折叠的拟合/转换。请看这里的答案,它询问如何不将SMOTE应用于验证折叠:
Using Smote with Gridsearchcv in Scikit-learn
正如上面的链接所指出的,imblearn包有一个类似sklearn的管道专门用来处理这个问题:https://imbalanced-learn.readthedocs.io/en/stable/generated/imblearn.pipeline.Pipeline.html
如果不看你的代码样本和你想要做的事情,我很难知道,但这可能会有所帮助:
from imblearn.pipeline import Pipeline
from imblearn.over_sampling import SMOTE
from sklearn.ensemble import RandomForestClassifier
from sklearn.preprocessing import StandardScaler
pipe = Pipeline(
[('scaler', StandardScaler(copy=True),
('resample', SMOTE()),
('model', RandomForestClassifier()]
)
kf = StratifiedKFold(n_splits=5, shuffle=True)
p_grid = dict(model__n_estimators=[50,100,200])
grid_search = GridSearchCV(
estimator=pipe, param_grid=p_grid, cv=kf, refit=True
)
grid_search.fit(X_train, y_train)
# Adding below in as could be helpful to know how to get fitted scaler if used
# best = grid_search.best_estimator_
# X_val_scaled = best['scaler'].transform(X_val)
# grid_search.predict(X_val_scaled)https://stackoverflow.com/questions/58613854
复制相似问题