【发布时间】:2015-11-19 21:59:36
【问题描述】:
我有一个 Pandas 数据框:comb
ENROLLED_Response 条目的数量非常少,因此仅对整个 DataFrame 进行随机抽样可能会丢失太多已注册的数据。
解决方案是从ENROLLED_Response == True 的所有条目中抽取 75% 的样本
然后对 ENROLLED_Response == False 的所有条目进行 70% 的抽样
所以我应该在 DataFrame 上以 is_train 和 true/false 列结束
所以我通常使用类似的东西:
from sklearn.cross_validation import cross_val_score
#split the dataset for train and test
comb['is_train'] = np.random.uniform(0, 1, len(comb)) <= .75
train, test = comb[comb['is_train']==True], comb[comb['is_train']==False]
这对大多数情况来说都很好,但由于注册人数很少,这种方法往往会遗漏太多“注册”,因为人数太少了。所以我需要的更像是:
comb['is_train'] = train_test_split(comb['ENROLLED_Response']==True, Train_size = 0.75)
comb['is_train']= train_test_split(comb['ENROLLED_Response']==False, Train_size = 0.75)
这当然行不通。概念是:第一次对已注册的样本进行抽样并将其中的 0.75 个随机标记为火车,然后在同一新列 (is_train) 中对未注册的样本(其他所有内容)进行抽样并将其中的 0.75 个标记为火车,这样它就可以很容易在 Scikit_learn 中使用,例如:
train, test = comb[comb['is_train']==True],comb[comb['is_train']==False]
由于 random 生成的 np 数组是相对于整个 DataFrame 的长度的(以及其他问题......),所以无法弄清楚如何做到这一点。
【问题讨论】:
标签: python pandas scikit-learn