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
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值