我有一个数据集,其中响应值不平衡,我有更多合格的拒绝值与非拒绝值,因此我希望平衡我的数据集。为此,有一个代码可以使用现已弃用的代码cross_validation.StratifiedKFold,但现在我需要对其进行调整,但我并不完全理解它,所以我正在寻求帮助。原来的代码是:def stratified_cv(X, y, clf_class, shuffle=True, n_folds=10, **kwargs): stratified_k_fold = cross_validation.StratifiedKFold(y, n_folds=n_folds, shuffle=shuffle) y_pred = y.copy() # ii -> train # jj -> test indices for ii, jj in stratified_k_fold: X_train, X_test = X[ii], X[jj] y_train = y[ii] clf = clf_class(**kwargs) clf.fit(X_train,y_train) y_pred[jj] = clf.predict(X_test) return y_pred数据集 fit_transformed在哪里X,转换为 numpy float 数组并缩放,y“拒绝”与“未拒绝”分类转换为 int 数组(当然是 0 或 1)。最后clf_class(**kwargs)可以是分类器,例如ensemble.GradientBoostingClassifier,svm.SVC和ensemble.RandomForestClassifiery = np.array([0,0,0,0,0,1,1])
1 回答
Qyouu
TA贡献1786条经验 获得超11个赞
StratifiedKFold已搬进model_selection. 所以你应该这样做:
from sklearn.model_selection import StratifiedKFold
def stratified_cv(X, y, clf_class, shuffle=True, n_folds=10, **kwargs):
stratified_k_fold = StratifiedKFold(n_splits=n_folds, shuffle=shuffle)
y_pred = y.copy()
# ii -> train
# jj -> test indices
for ii, jj in stratified_k_fold.split(X,y):
X_train, X_test = X[ii], X[jj]
y_train = y[ii]
clf = clf_class(**kwargs)
clf.fit(X_train,y_train)
y_pred[jj] = clf.predict(X_test)
return y_pred
添加回答
举报
0/150
提交
取消