我对scikit学习/机器学习比较陌生 . 我必须使用Titanic数据集创建决策树,并且需要使用5倍的KFold交叉验证 . 这是我到目前为止所拥有的:
cv = KFold(n_splits=5)
tree_model = tree.DecisionTreeClassifier(max_depth=3)
print(titanic_train.describe())
fold_accuracy = []
for train_index, valid_index in cv.split(X_train):
train_x,test_x = X_train.iloc[train_index],X_train.iloc[valid_index]
train_y,test_y= y_train.iloc[train_index], y_train.iloc[valid_index]
model = tree_model.fit(train_x,train_y)
valid_acc = model.score(test_x,test_y)
fold_accuracy.append(valid_acc)
print(confusion_matrix(y_test,model.predict(X_test)))
print("Accuracy per fold: ", fold_accuracy, "\n")
print("Average accuracy: ", sum(fold_accuracy)/len(fold_accuracy))
dot_data = StringIO()
我的问题是,我的拟合模型是否只存在于循环中?我需要从测试训练集中准确预测,其中“幸存”未标记(在混淆矩阵中,X_Test是测试数据集X值,y_test是实际存活率),我不确定通过训练使用此方法,我的主分类器(tree_model)正在使用折叠中的每个集合进行训练 .