博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
KFold
阅读量:2051 次
发布时间:2019-04-28

本文共 15021 字,大约阅读时间需要 50 分钟。

转载于:

K折交叉验证:sklearn.model_selection.KFold(n_splits=3, shuffle=False, random_state=None)

思路:将训练/测试数据集划分n_splits个互斥子集,每次用其中一个子集当作验证集,剩下的n_splits-1个作为训练集,进行n_splits次训练和测试,得到n_splits个结果

注意点:对于不能均等份的数据集,其前n_samples % n_splits子集拥有n_samples // n_splits + 1个样本,其余子集都只有n_samples // n_splits样本

参数说明:

n_splits:表示划分几等份

shuffle:在每次划分时,是否进行洗牌

①若为Falses时,其效果等同于random_state等于整数,每次划分的结果相同

②若为True时,每次划分的结果都不一样,表示经过洗牌,随机取样的

random_state:随机种子数

属性:

①get_n_splits(X=None, y=None, groups=None):获取参数n_splits的值

②split(X, y=None, groups=None):将数据集划分成训练集和测试集,返回索引生成器

通过一个不能均等划分的栗子,设置不同参数值,观察其结果

①设置shuffle=False,运行两次,发现两次结果相同

In [     
1]:
from sklearn.model_selection
import KFold
...:
import numpy
as np
...: X = np.arange(
24).reshape(
12,
2)
...: y = np.random.choice([
1,
2],
12,p=[
0.4,
0.6])
...: kf = KFold(n_splits=
5,shuffle=
False)
...:
for train_index , test_index
in kf.split(X):
...: print(
'train_index:%s , test_index: %s ' %(train_index,test_index))
...:
...:
train_index:[
3
4
5
6
7
8
9
10
11] , test_index: [
0
1
2]
train_index:[
0
1
2
6
7
8
9
10
11] , test_index: [
3
4
5]
train_index:[
0
1
2
3
4
5
8
9
10
11] , test_index: [
6
7]
train_index:[
0
1
2
3
4
5
6
7
10
11] , test_index: [
8
9]
train_index:[
0
1
2
3
4
5
6
7
8
9] , test_index: [
10
11]
In [
2]:
from sklearn.model_selection
import KFold
...:
import numpy
as np
...: X = np.arange(
24).reshape(
12,
2)
...: y = np.random.choice([
1,
2],
12,p=[
0.4,
0.6])
...: kf = KFold(n_splits=
5,shuffle=
False)
...:
for train_index , test_index
in kf.split(X):
...: print(
'train_index:%s , test_index: %s ' %(train_index,test_index))
...:
...:
train_index:[
3
4
5
6
7
8
9
10
11] , test_index: [
0
1
2]
train_index:[
0
1
2
6
7
8
9
10
11] , test_index: [
3
4
5]
train_index:[
0
1
2
3
4
5
8
9
10
11] , test_index: [
6
7]
train_index:[
0
1
2
3
4
5
6
7
10
11] , test_index: [
8
9]
train_index:[
0
1
2
3
4
5
6
7
8
9] , test_index: [
10
11]
②设置shuffle=True时,运行两次,发现两次运行的结果不同

In [     
3]:
from sklearn.model_selection
import KFold
...:
import numpy
as np
...: X = np.arange(
24).reshape(
12,
2)
...: y = np.random.choice([
1,
2],
12,p=[
0.4,
0.6])
...: kf = KFold(n_splits=
5,shuffle=
True)
...:
for train_index , test_index
in kf.split(X):
...: print(
'train_index:%s , test_index: %s ' %(train_index,test_index))
...:
...:
train_index:[
0
1
2
4
5
6
7
8
10] , test_index: [
3
9
11]
train_index:[
0
1
2
3
4
5
9
10
11] , test_index: [
6
7
8]
train_index:[
2
3
4
5
6
7
8
9
10
11] , test_index: [
0
1]
train_index:[
0
1
3
4
5
6
7
8
9
11] , test_index: [
2
10]
train_index:[
0
1
2
3
6
7
8
9
10
11] , test_index: [
4
5]
In [
4]:
from sklearn.model_selection
import KFold
...:
import numpy
as np
...: X = np.arange(
24).reshape(
12,
2)
...: y = np.random.choice([
1,
2],
12,p=[
0.4,
0.6])
...: kf = KFold(n_splits=
5,shuffle=
True)
...:
for train_index , test_index
in kf.split(X):
...: print(
'train_index:%s , test_index: %s ' %(train_index,test_index))
...:
...:
train_index:[
0
1
2
3
4
5
7
8
11] , test_index: [
6
9
10]
train_index:[
2
3
4
5
6
8
9
10
11] , test_index: [
0
1
7]
train_index:[
0
1
3
5
6
7
8
9
10
11] , test_index: [
2
4]
train_index:[
0
1
2
3
4
6
7
9
10
11] , test_index: [
5
8]
train_index:[
0
1
2
4
5
6
7
8
9
10] , test_index: [
3
11]
③设置shuffle=True和random_state=整数,发现每次运行的结果都相同

In [     
5]:
from sklearn.model_selection
import KFold
...:
import numpy
as np
...: X = np.arange(
24).reshape(
12,
2)
...: y = np.random.choice([
1,
2],
12,p=[
0.4,
0.6])
...: kf = KFold(n_splits=
5,shuffle=
True,random_state=
0)
...:
for train_index , test_index
in kf.split(X):
...: print(
'train_index:%s , test_index: %s ' %(train_index,test_index))
...:
...:
train_index:[
0
1
2
3
5
7
8
9
10] , test_index: [
4
6
11]
train_index:[
0
1
3
4
5
6
7
9
11] , test_index: [
2
8
10]
train_index:[
0
2
3
4
5
6
8
9
10
11] , test_index: [
1
7]
train_index:[
0
1
2
4
5
6
7
8
10
11] , test_index: [
3
9]
train_index:[
1
2
3
4
6
7
8
9
10
11] , test_index: [
0
5]
In [
6]:
from sklearn.model_selection
import KFold
...:
import numpy
as np
...: X = np.arange(
24).reshape(
12,
2)
...: y = np.random.choice([
1,
2],
12,p=[
0.4,
0.6])
...: kf = KFold(n_splits=
5,shuffle=
True,random_state=
0)
...:
for train_index , test_index
in kf.split(X):
...: print(
'train_index:%s , test_index: %s ' %(train_index,test_index))
...:
...:
train_index:[
0
1
2
3
5
7
8
9
10] , test_index: [
4
6
11]
train_index:[
0
1
3
4
5
6
7
9
11] , test_index: [
2
8
10]
train_index:[
0
2
3
4
5
6
8
9
10
11] , test_index: [
1
7]
train_index:[
0
1
2
4
5
6
7
8
10
11] , test_index: [
3
9]
train_index:[
1
2
3
4
6
7
8
9
10
11] , test_index: [
0
5]
④n_splits属性值获取方式

In [     
8]: kf.split(X)
Out[
8]:
In [
9]: kf.get_n_splits()
Out[
9]:
5
In [
10]: kf.n_splits
Out[
10]:
5

你可能感兴趣的文章
Redis学习笔记(二)— 在linux下搭建redis服务器
查看>>
Redis学习笔记(三)—— 使用redis客户端连接windows和linux下的redis并解决无法连接redis的问题
查看>>
Intellij IDEA使用(一)—— 安装Intellij IDEA(ideaIU-2017.2.3)并完成Intellij IDEA的简单配置
查看>>
Intellij IDEA使用(二)—— 在Intellij IDEA中配置JDK(SDK)
查看>>
Intellij IDEA使用(三)——在Intellij IDEA中配置Tomcat服务器
查看>>
Intellij IDEA使用(四)—— 使用Intellij IDEA创建静态的web(HTML)项目
查看>>
Intellij IDEA使用(五)—— Intellij IDEA在使用中的一些其他常用功能或常用配置收集
查看>>
Intellij IDEA使用(六)—— 使用Intellij IDEA创建Java项目并配置jar包
查看>>
Eclipse使用(十)—— 使用Eclipse创建简单的Maven Java项目
查看>>
Eclipse使用(十一)—— 使用Eclipse创建简单的Maven JavaWeb项目
查看>>
Intellij IDEA使用(十三)—— 在Intellij IDEA中配置Maven
查看>>
面试题 —— 关于main方法的十个面试题
查看>>
集成测试(一)—— 使用PHP页面请求Spring项目的Java接口数据
查看>>
使用Maven构建的简单的单模块SSM项目
查看>>
Intellij IDEA使用(十四)—— 在IDEA中创建包(package)的问题
查看>>
Redis学习笔记(四)—— redis的常用命令和五大数据类型的简单使用
查看>>
CentOS 8 都发布了,你还不会用 nftables?
查看>>
一点也不流氓的搜狗输入法皮肤
查看>>
Grafana 6.4 正式发布!
查看>>
etcd 性能测试与调优
查看>>