python小知识

一、StratifiedKFold交叉验证使用举例

import numpy as np
from sklearn.model_selection import StratifiedKFold
x = np.array([[1,2], [3,4], [1,2], [3,4],[5,6],[7,8]])
y = np.array([0, 0, 1, 1,1,0])
#n_splits代表几折交叉,要能被样本个数整除才行,返回的是所有的分割结果的下标,分别轮着做测试集
skf = StratifiedKFold(n_splits=3).split(x, y)
for train_index,test_index in skf:
    print('输出索引下标:')
    print(train_index,test_index)
    print('输出分割后结果:')
    print(x[train_index],y[train_index])
    print(x[test_index],y[test_index])
    print('---------------------------------')

OUT:

输出索引下标:
[1 3 4 5] [0 2]
输出分割后结果:
[[3 4]
 [3 4]
 [5 6]
 [7 8]] [0 1 1 0]
[[1 2]
 [1 2]] [0 1]
---------------------------------
输出索引下标:
[0 2 4 5] [1 3]
输出分割后结果:
[[1 2]
 [1 2]
 [5 6]
 [7 8]] [0 1 1 0]
[[3 4]
 [3 4]] [0 1]
---------------------------------
输出索引下标:
[0 1 2 3] [4 5]
输出分割后结果:
[[1 2]
 [3 4]
 [1 2]
 [3 4]] [0 0 1 1]
[[5 6]
 [7 8]] [1 0]
---------------------------------

二、Parallel,delayed用法

from math import sqrt 
[sqrt(i ** 2) for i in range(10)]

from joblib import Parallel, delayed 
Parallel(n_jobs=2)(delayed(sqrt)(i ** 2) for i in range(10))

 


posted @ 2020-12-01 11:03  光彩照人  阅读(115)  评论(0)    收藏  举报