K折交叉验证

数据集分割、打分

import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.tree import DecisionTreeRegressor

#load  dataset
data = pd.read_csv('train.csv')
data = data.iloc[:,[1,3,4,80]]
data = data.fillna(0)

X_train,X_val,y_train,y_val = train_test_split(data.iloc[:,:3],data.SalePrice,test_size=0.3,random_state = 0)
#random_state代表随机状态

#fit
regr_1 = DecisionTreeRegressor(max_depth=2)
regr_2 = DecisionTreeRegressor(max_depth=5)
regr_3 = DecisionTreeRegressor(max_depth=10)
regr_1.fit(X_train,y_train)
regr_2.fit(X_train,y_train)
regr_3.fit(X_train,y_train)

#score
for col in [regr_1,regr_2,regr_3]:
    print(col.score(X_train,y_train),col.score(X_val,y_val))

由于原数据集特征数81,这里只选取了3个特征,没做必要的数据预处理,缺失值也是简单的补0,得分较低。
不过也可以得出随着max_depth的加深,训练集上拟合的越来越好,验证集上表现越来越差,过拟合严重。

max_depthtrain_scoreval_score
20.2401950912961910.23574989781877642
50.45874613204594660.2986072801772752
100.86035221748414580.12554005906320287

交叉验证的指标

最简单的方式:cross_val_score

from sklearn.model_selection import cross_val_score

def scores(regr,data,target):
    scores = cross_val_score(regr,data,target,cv = 5)
    print(scores,"Accuracy: %0.2f (+/- %0.2f)"%(scores.mean(),scores.std()*2))

for col in [regr_1,regr_2,regr_3]:
    scores(col,data.iloc[:,:3],data.SalePrice)

评分估计的平均得分和 95% 置信区间由此给出(这个95%我也不知道怎么来的。。。)

K折交叉验证

将所有的样例划分为 k 个组,称为折叠 (fold) 。预测函数学习时使用 k - 1 个折叠中的数据,最后一个剩下的折叠会用于测试。
作为一般规则,大多数作者和经验证据表明, 5或者 10交叉验证效果更优。

简单示例

在 4 个样例的数据集上使用 2-fold 交叉验证的示例:

import numpy as np
from sklearn.model_selection import KFold

X = ["a", "b", "c", "d"]
kf = KFold(n_splits=2)
for train, test in kf.split(X):
	print("%s  %s" % (train, test))

在这里插入图片描述
应用到自身数据集上

#K折交叉验证
from sklearn.model_selection import KFold

kf = KFold(n_splits=2)

X = np.array(data.iloc[:,:3])
y = np.array(data.SalePrice)
for train, test in kf.split(data):
    #print("%s  %s" % (train, test))
    X_train, X_test, y_train, y_test = X[train], X[test], y[train], y[test]
    print(X_train)

重复 K-折交叉验证

重复 K-Fold n 次。当需要运行时可以使用它 KFold n 次,在每次重复中产生不同的分割。
2折 K-Fold 重复 2 次的示例:

#2折重复二次的示例
from sklearn.model_selection import RepeatedKFold

random_state = 123455
rkf = RepeatedKFold(n_splits=2,n_repeats=2,random_state = random_state)
for train, test in rkf.split(data):
    print("%s  %s" % (train, test))
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
K交叉验证是一种常用的模型评估方法,广泛应用于机器学习和深度学习中。其基本思想是将原始的训练集数据划分为K个较小的子集,然后依次选取其中一份作为验证集,其余的K-1份作为训练集,进行多次训练和评估,最终得到模型的平均评估结果。 在使用TensorFlow进行K交叉验证时,一般有以下几个步骤: 1. 数据准备:将原始的训练集数据分为K个子集。 2. 模型搭建:使用TensorFlow构建模型,并设置好模型的超参数。 3. K循环:依次选取其中一份作为验证集,其余的K-1份作为训练集,进行模型训练和评估。 4. 评估指标:选择适当的评估指标来衡量模型的性能,比如准确率、精确度、召回率等。 5. 模型融合:将K次训练得到的模型评估结果进行平均,得到最终的模型评估结果。 K交叉验证的优点是可以更客观地评估模型的性能,减少了模型在特定数据集上过拟合的可能性。同时,由于采用了多次训练和验证,可以更充分地利用数据集,提高模型的泛化能力。 然而,K交叉验证也有一些缺点,比如需要进行K次训练,计算时间较长。此外,如果数据集不够大,划分出来的子集可能会比较小,导致评估结果的可靠性降低。 总之,TensorFlow提供了便捷的接口和功能支持,可以轻松地实现K交叉验证,并通过该方法更准确地评估模型的性能,提高模型的泛化能力。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值