【发布时间】:2017-04-23 10:37:42
【问题描述】:
我有一个 pandas DataFrame,大致如下:
cli_id | X1 | X2 | X3 | ... | Xn | Y |
----------------------------------------
123 | 1 | A | XX | ... | 4 | 0.1 |
456 | 2 | B | XY | ... | 5 | 0.2 |
789 | 1 | B | XY | ... | 5 | 0.3 |
101 | 2 | A | XX | ... | 4 | 0.1 |
...
我有客户 ID、少数分类属性和 Y,它是事件的概率,其值从 0 到 1 乘以 0.1。
我需要在大小为 200 的 Y 的每一组(所以 10 倍)中抽取一个分层样本
我经常在拆分成训练/测试时使用它来获取分层样本:
def stratifiedSplit(X,y,size):
sss = StratifiedShuffleSplit(y, n_iter=1, test_size=size, random_state=0)
for train_index, test_index in sss:
X_train, X_test = X.iloc[train_index], X.iloc[test_index]
y_train, y_test = y.iloc[train_index], y.iloc[test_index]
return X_train, X_test, y_train, y_test
但我不知道在这种情况下如何修改它。
【问题讨论】: