模型评估:数据切分
Posted yongfuxue
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了模型评估:数据切分相关的知识,希望对你有一定的参考价值。
顺序切分
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%的类别数的所有数据集作为训练集,其余
#为验证集。
以上是关于模型评估:数据切分的主要内容,如果未能解决你的问题,请参考以下文章