pandas 如何将数据集拆分为训练集和验证集,保持类之间的比例?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29082001/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 23:03:32  来源:igfitidea点击:

how to split a dataset into training and validation set keeping ratio between classes?

pythonnumpypandasmachine-learningscikit-learn

提问by HymanNova

I have a multi class classification problem and my dataset is skewed, I have 100 instances of a particular class and say 10 of some different class, so I want to split my dataset keeping ratio between classes, if I have 100 instances of a particular class and I want 30% of records to go in the training set I want to have there 30 instances of my 100 record represented class and 3 instances of my 10 record represented class and so on.

我有一个多类分类问题,我的数据集有偏差,我有一个特定类的 100 个实例,并且说一些不同类的 10 个,所以如果我有 100 个特定类的实例,我想在类之间拆分我的数据集保持比率并且我希望 30% 的记录进入训练集中我希望有我的 100 个记录代表类的 30 个实例和我的 10 个记录代表类的 3 个实例等等。

采纳答案by EdChum

You can use sklearn's StratifiedKFold, from the online docs:

您可以使用 sklearn's StratifiedKFold,来自在线文档:

Stratified K-Folds cross validation iterator

Provides train/test indices to split data in train test sets.

This cross-validation object is a variation of KFold that returns stratified folds. The folds are made by preserving the percentage of samples for each class.

分层 K 折交叉验证迭代器

提供训练/测试索引以拆分训练测试集中的数据。

这个交叉验证对象是 KFold 的变体,它返回分层折叠。通过保留每个类别的样本百分比来进行折叠。

>>> from sklearn import cross_validation
>>> X = np.array([[1, 2], [3, 4], [1, 2], [3, 4]])
>>> y = np.array([0, 0, 1, 1])
>>> skf = cross_validation.StratifiedKFold(y, n_folds=2)
>>> len(skf)
2
>>> print(skf)  
sklearn.cross_validation.StratifiedKFold(labels=[0 0 1 1], n_folds=2,
                                         shuffle=False, random_state=None)
>>> for train_index, test_index in skf:
...    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]
TRAIN: [1 3] TEST: [0 2]
TRAIN: [0 2] TEST: [1 3]

This will preserve your class ratios so that the splits retain the class ratios, this will work fine with pandas dfs.

这将保留您的班级比率,以便拆分保留班级比率,这将适用于 Pandas dfs。

As suggested by @Ali_m you could use StratifiedShuffledSplitwhich accepts a split ratio param:

正如@Ali_m 所建议的,您可以使用StratifiedShuffledSplit它接受分流比参数:

sss = StratifiedShuffleSplit(y, 3, test_size=0.7, random_state=0)

sss = StratifiedShuffleSplit(y, 3, test_size=0.7, random_state=0)

would produce a 70% split.

将产生 70% 的分割。

回答by Th? Sinh

As simple as :

就像这样简单:

from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y,
                                                stratify=y, 
                                                test_size=0.25)