交叉验证的作用
很多时候我们都在纠结,交叉验证法到底有啥用???我不用这个咋的??
交叉验证是一种模型验证技术,可用于评估统计分析(模型)结果在其它独立数据集上的泛化能力。它主要用于预测,我们可以用它来评估预测模型在实践中的准确度。
准确来说:叫擦很严重就是来来回回反复的对模型进行刷新,最后得到的模型评估结果就是这K次验证的均值
from sklearn.model_selection import KFold
kf = KFold(n_splits = K, shuffle=True, random_state=0)
for train_index, test_index in kf.split(data):
clt = model.fit(data[train_index], four_label[train_index])
curr_score = curr_score + clt.score(data[test_index], four_label[test_index])
print(clt.score(data[test_index], four_label[test_index]))
avg_score = curr_score / 5
print("平均准确率为:", avg_score)
留一法
如果当我们分出来的数据子集的数目和我们交叉验证的次数一样,那么就出现了留一法。
但是留一法在数据量较大的时候,大量的模型计算开销过于庞大。
自助法
采样源数据集D1得到D2,从D1中随机挑选出一个小样本,放入新数据集D2 ,但D1的数据集总量不变.这样可以提高小数据模型的准确性
lim
x
→
∞
(
1
−
1
m
)
m
=
1
e
≈
0.368
\lim_{x \to \infty} (1−\frac{1}{m})^m=\frac{1}{e}≈0.368
limx→∞(1−m1)m=e1≈0.368
#自助法
import numpy as np
#任意设置一个数据集
X = [1,4,3,23,4,6,7,8,9,45,67,89,34,54,76,98,43,52]
#通过产生的随机数获得抽取样本的序号
bootstrapping = []
for i in range(len(X)):
bootstrapping.append(np.floor(np.random.random()*len(X)))
#通过序号获得原始数据集中的数据
D_1 = []
for i in range(len(X)):
D_1.append(X[int(bootstrapping[i])])
'''结果
[45, 52, 34, 9, 6, 54, 34, 1, 4, 54, 6, 8, 67, 4, 43, 9, 4, 67]
'''