from
sklearn.model_selection
import
KFold
import
numpy as np
X
=
np.array([[
1
,
2
],[
3
,
4
],[
1
,
3
],[
3
,
5
]])
Y
=
np.array([
1
,
2
,
3
,
4
])
KF
=
KFold(n_splits
=
2
)
for
train_index,test_index
in
KF.split(X):
print
(
"TRAIN:"
,train_index,
"TEST:"
,test_index)
X_train,X_test
=
X[train_index],X[test_index]
Y_train,Y_test
=
Y[train_index],Y[test_index]
print
(X_train,X_test)
print
(Y_train,Y_test)
import
numpy as np
from
sklearn.model_selection
import
KFold
Sam
=
np.array(np.random.randn(
1000
))
New_sam
=
KFold(n_splits
=
5
)
for
train_index,test_index
in
New_sam.split(Sam):
Sam_train,Sam_test
=
Sam[train_index],Sam[test_index]
print
(
'训练集数量:'
,Sam_train.shape,
'测试集数量:'
,Sam_test.shape)
from
sklearn.model_selection
import
StratifiedKFold
import
numpy as np
m
=
np.array([[
1
,
2
],[
3
,
5
],[
2
,
4
],[
5
,
7
],[
3
,
4
],[
2
,
7
]])
n
=
np.array([
0
,
0
,
0
,
1
,
1
,
1
])
skf
=
StratifiedKFold(n_splits
=
3
)
for
train_index,test_index
in
skf.split(m,n):
print
(
"train"
,train_index,
"test"
,test_index)
x_train,x_test
=
m[train_index],m[test_index]
from
sklearn.model_selection
import
StratifiedKFold
import
numpy as np
y1
=
np.array(
range
(
10
))
y2
=
np.array(
range
(
20
,
30
))
y3
=
np.array(np.random.randn(
10
))
m
=
np.append(y1,y2)
m1
=
np.append(m,y3)
n
=
[i
/
/
10
for
i
in
range
(
30
)]
skf
=
StratifiedKFold(n_splits
=
5
)
for
train_index,test_index
in
skf.split(m1,n):
print
(
"train"
,train_index,
"test"
,test_index)
x_train,x_test
=
m1[train_index],m1[test_index]