我已经使用以下代码将列分配到其特定的 k 折叠:
from sklearn.model_selection import StratifiedKFold, train_test_split
# Stratified K-fold cross-validation
df['kfold'] = -1
df = df.sample(frac=1).reset_index(drop=True)
y = df.quality
kf = StratifiedKFold(n_splits=5)
for f, (t_,v_) in enumerate(kf.split(X=df, y=y)):
df.loc[v_, 'kfold'] = f
现在数据框符合预期:
alcohol volatile acidity sulphates citric acid quality kfold
1499 10.9 0.36 0.73 0.39 6 4
1500 9.5 0.65 0.55 0.10 5 4
1501 13.4 0.44 0.66 0.68 6 4
1502 9.6 0.59 0.67 0.24 5 4
1503 13.0 0.53 0.77 0.79 5 4
但是如何将其拆分为训练和测试拆分?