码迷,mamicode.com
首页 > 其他好文 > 详细

模型评估:数据切分

时间:2018-12-10 14:09:22      阅读:204      评论:0      收藏:0      [点我收藏+]

标签:return   val   切分   shu   its   test   core   names   rand   

 
顺序切分

from sklearn.model_selection import train_test_split

X_train, X_test, y_train, y_test = train_test_split(iris.data, iris.target, test_size=0.4, random_state=0)

ShuffleSplit

from sklearn.model_selection import ShuffleSplit

sample = pd.DataFrame()

rs = ShuffleSplit(n_splits=5, test_size=0.2, random_state=0)

for train_indices, test_indices in rs.split(sample):

       #共循环5次

       #每次循环拿到打乱顺序的训练集和验证集的索引值,训练集数量为80%,测试集数量为20%

 

这种数据切分方式经常与CV结合使用

from sklearn.model_selection import cross_val_score

from sklearn.model_selection import ShuffleSplit

 

cv = ShuffleSplit(n_splits=3, test_size=0.3, random_state=0)

cross_val_score(clf, iris.data, iris.target, cv=cv)

 

GroupShuffleSplit

from sklearn.model_selection import GroupShuffleSplit

sample = pd.DataFrame()

rs = GroupShuffleSplit(n_splits=4, test_size=0.25, random_state=0)

 

#获取组标记

_, driver_indices = np.unique(np.array(driver_ids), return_inverse=True)

driver_indices  = [0 0 1 1 1 2 2 2 2 3]

 

#这里是按照groups所给列的类别进行数据集的划分

for train_indices, test_indices in rs.split(x_train_names_all, y_train_labels_all, groups=driver_indices):

       #共循环4次

       #假设groups列中有4个类别,每次循环,从中选择75%的类别数,属于这75%的类别数的所有数据集作为训练集,其余

#为验证集。

模型评估:数据切分

标签:return   val   切分   shu   its   test   core   names   rand   

原文地址:https://www.cnblogs.com/yongfuxue/p/10095389.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!