python 创建用于交叉验证的样本

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python 创建用于交叉验证的样本相关的知识,希望对你有一定的参考价值。

# HELP: http://scikit-learn.org/stable/modules/generated/sklearn.cross_validation.KFold.html

from sklearn.cross_validation import KFold
import numpy as np

# data
X = np.array([2.3,4.2,5.3,3.1,4.5,6.2])
y = np.array([34543,24432,85432,32543,75433,21124])

# build object 
#kf = KFold(6, n_folds=2,shuffle=False,random_state=None) # not random selection
kf = KFold(6, n_folds=2,shuffle=True,random_state=None) # random selection
print(len(kf))
print(kf)  

# create samples for training and test for each iteration
for train_index, test_index in kf:
	print("indexes of samples for each iteration","TRAIN:", train_index, "TEST:", test_index)
	# x points of samples (training, test) for each iteration
	X_train, X_test = X[train_index], X[test_index]
	# y points of samples (training, test) for each iteration
	y_train, y_test = y[train_index], y[test_index]
	
	# here include validation for each iteration
	
# here calculate the average error of n_folds iterations

以上是关于python 创建用于交叉验证的样本的主要内容,如果未能解决你的问题,请参考以下文章

交叉验证、留一交叉验证、自助法

Python异常样本识别 交叉验证出现错误?

在带有分组约束的 sklearn (python 2.7) 中创建训练、测试和交叉验证数据集?

python实现jacknife交叉验证

在MATLAB中使用交叉验证函数的方法

机器学习100天(二十九):029 K折交叉验证