什么是KFold?
三体船
在sklearn⽂档中说的是:
Provides train/test indices to split data in train/test sets. Split dataset into k consecutive folds (without shuffling by default).
Each fold is then used once as a validation while the k - 1 remaining folds form the training set.
翻译:提供训练/测试索引以分割训练/测试集中的数据。将数据集拆分为k个连续的折叠(默认情况下不进⾏洗牌)。 每⼀个折叠被使⽤⼀次作为验证,⽽k-1剩余的折叠形成训练集。
del_selection import KFold
kfold = KFold(n_splits=5)
for train_index,test_index in kfold.split(data,label):
x_train = data.iloc[train_index]
y_train = label[train_index]
x_test = data.iloc[test_index]
y_test = label[test_index]
所得到的就是训练集、训练标签,测试集和测试标签。在做的时候才知道⽤KFold的时候,都是其索引,那么要进⾏多次模型训练的时候。通过索引得到相对应的训练集和测试集。接着进⾏模型训练。所以⼀定要注意⾃⼰的数据格式,要防⽌出现训练集和测试集分开的时候导致运⾏错误。程序中的.iloc是在运⾏过程中想要通过索引得到相对应的数据才采⽤的。根据格式不同需要有些变化。海参软胶囊
model.fit(x_train,y_train)安息香乙醚
pre = model.predict(x_test)
pre1 = model.predict_proba(x_test)
accuracy = accuracy_score(y_test,pre)
红外对射模块
acc.append(accuracy)
草莓托print('accuracy score: {}'.format(accuracy))
车头时距通过model.fit进⾏训练然后预测,最终获得准确率。程序中pre1是为了画ROC曲线所写的。可忽略。
“全⽂均以本⼈的经验想法⽽⾔”