在scikit-learn中分层训练/测试分裂

pir*_*pir 64 python scikit-learn

我需要将我的数据分成训练集(75%)和测试集(25%).我目前使用以下代码执行此操作:

X, Xt, userInfo, userInfo_train = sklearn.cross_validation.train_test_split(X, userInfo)   
Run Code Online (Sandbox Code Playgroud)

但是,我想对训练数据集进行分层.我怎么做?我一直在研究这种StratifiedKFold方法,但是不允许我指定75%/ 25%的分割,只对训练数据集进行分层.

And*_*ler 113

[更新0.17]

查看以下文档sklearn.model_selection.train_test_split:

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)
Run Code Online (Sandbox Code Playgroud)

[/更新0.17]

有一个拉要求在这里.但train, test = next(iter(StratifiedKFold(...))) 如果你愿意,你可以简单地使用火车和测试指数.

  • @Jordan没有在scikit-learn中实现.我不知道标准方式.我们可以使用百分位数. (2认同)

tan*_*ngy 24

TL; DR:使用StratifiedShuffleSplittest_size=0.25

Scikit-learn为分层分裂提供了两个模块:

  1. StratifiedKFold:此模块可用作直接k折叠交叉验证运算符:因为它将设置n_folds训练/测试集,使得类在两者中均衡.

下面是一些代码(直接来自上面的文档)

>>> skf = cross_validation.StratifiedKFold(y, n_folds=2) #2-fold cross validation
>>> len(skf)
2
>>> 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]
...    #fit and predict with X_train/test. Use accuracy metrics to check validation performance
Run Code Online (Sandbox Code Playgroud)
  1. StratifiedShuffleSplit:该模块创建一个具有同等平衡(分层)类的训练/测试集.基本上这就是你想要的n_iter=1.您可以在此处提及与测试大小相同的测试大小train_test_split

码:

>>> sss = StratifiedShuffleSplit(y, n_iter=1, test_size=0.5, random_state=0)
>>> len(sss)
1
>>> for train_index, test_index in sss:
...    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]
>>> # fit and predict with your classifier using the above X/y train/test
Run Code Online (Sandbox Code Playgroud)

  • 请注意,从`0.18.x`开始,`n_iter`应该是`stratifiedShuffleSplit`的`n_splits` - 并且它的API有一个不同的API:http://scikit-learn.org/stable/modules/generated/sklearn .model_selection.StratifiedShuffleSplit.html (4认同)
  • 如果 `y` 是 Pandas 系列,使用 `y.iloc[train_index], y.iloc[test_index]` (3认同)
  • @tangy为什么这是一个for循环?是不是当调用`X_train,X_test = X [train_index],X [test_index]`这行时会覆盖`X_train`和`X_test`?那么为什么不只是一个`next(sss)`? (2认同)

Jor*_*dan 9

这是连续/回归数据的示例(直到GitHub上的此问题得到解决).

# Your bins need to be appropriate for your output values
# e.g. 0 to 50 with 25 bins
bins     = np.linspace(0, 50, 25)
y_binned = np.digitize(y_full, bins)
X_train, X_test, y_train, y_test = train_test_split(X, y, stratify=y_binned)
Run Code Online (Sandbox Code Playgroud)


Sha*_*ani 6

您可以train_test_split()使用Scikit学习中可用的方法简单地做到这一点:

from sklearn.model_selection import train_test_split 
train, test = train_test_split(X, test_size=0.25, stratify=X['YOUR_COLUMN_LABEL']) 
Run Code Online (Sandbox Code Playgroud)

我还准备了一个简短的GitHub Gist,展示了stratifyoption的工作原理:

https://gist.github.com/SHi-ON/63839f3a3647051a180cb03af0f7d0d9


Max*_*Max 5

除了@Andreas Mueller接受的答案外,只需将其添加为上述@tangy:

StratifiedShuffleSplit最类似于train_test_splitstratify = y),具有以下新增功能:

  1. 默认分层
  2. 通过指定n_splits,它反复拆分数据