我使用 sklearn.cross_validation.KFold() 在 python 中使用 5 折交叉验证来查看我的模型如何执行。它在 4 折上表现良好,在某一特定折上表现非常差。由于我是数据科学的新手,我想知道如何从一个特定的折叠中检索数据,以便我可以查看该集合中的数据并弄清楚如何修复它。
问问题
309 次
2 回答
0
这很简单。Sklearn 文档中仅提供了一个 K-Folds 示例:
X = np.array([[1, 2], [3, 4], [1, 2], [3, 4]]) # create an array
y = np.array([1, 2, 3, 4]) # Create another array
kf = KFold(n_splits=2) # Define the split - into 2 folds
for train_index, test_index in kf.split(X):
print(“TRAIN:”, train_index, “TEST:”, test_index)
X_train, X_test = X[train_index], X[test_index]
y_train, y_test = y[train_index], y[test_index]
('TRAIN:', array([2, 3]), 'TEST:', array([0, 1]))
('TRAIN:', array([0, 1]), 'TEST:', array([2, 3]))
您还必须打印在每个步骤中计算的性能。
于 2019-12-11T11:52:36.743 回答
0
from pandas import ExcelWriter
from sklearn.model_selection import KFold
kf = KFold(n_splits=3)
fold = 0
writer = ExcelWriter('Kfoldcrossvalidation.xlsx')
for train_index, test_index in kf.split(X2):
fold += 1
print("Fold: %s" % fold)
X_train, X_test = X50.iloc[train_index], X50.iloc[test_index]
y_train, y_test = Y.iloc[train_index], Y.iloc[test_index]
print(y_test)
y_test.to_excel(writer,sheet_name='sheet ' + str(fold))
writer.save()
于 2019-12-16T07:06:36.327 回答