【问题标题】:How to split data based on a column value in sklearn如何根据sklearn中的列值拆分数据
【发布时间】:2020-07-21 17:47:16
【问题描述】:

我有一个包含以下列的数据文件

'客户', 'calibrat' - 校准样本 = 1;验证样本 = 0; '搅拌', '搅动', '收入', '牟',

数据文件包含大约 40000 行,其中 20000 的 calibrat 值为 1。我想将此数据拆分为

X1 = data.loc[:, data.columns != 'churn']
y1 = data.loc[:, data.columns == 'churn']
from imblearn.over_sampling import SMOTE
os = SMOTE(random_state=0)
X1_train, X1_test, y1_train, y1_test = train_test_split(X1, y1, test_size=0.3, random_state=0)

我想要的是在我的 X1_train 中应该包含校准数据,校准为 calibrat =1 并且在 X1_test 中应该包含所有用于验证的数据 calibrat = 0

【问题讨论】:

  • 你试过X1_train, X1_test, y1_train, y1_test = train_test_split(X1.loc[X1['calibrat']==1], y1.loc[X1['calibrat']!=1], test_size=0.3, random_state=0)吗?
  • 不,这不起作用

标签: python machine-learning logistic-regression train-test-split smote


【解决方案1】:

sklearn.model_selection 除了train_test_split 之外还有其他几个选项。其中之一旨在解决您的要求。在这种情况下,您可以使用GroupShuffleSplit,如文档中所述,它提供随机训练/测试索引以根据第三方提供的组拆分数据。这在您进行交叉验证时很有用,并且您想多次拆分验证训练,确保集合被 group 字段拆分。对于这些情况,您还可以使用 GroupKFold,这非常有用。

所以,调整你的例子,这就是你可以做的。

比如说你有:

from sklearn.model_selection import GroupShuffleSplit

cols = ['customer', 'calibrat', 'churn', 'churndep', 'revenue', 'mou',]
X = pd.DataFrame(np.random.rand(10, 6), columns=cols)
X['calibrat'] = np.random.choice([0,1], size=10)

print(X)

   customer  calibrat     churn  churndep   revenue       mou
0  0.523571         1  0.394896  0.933637  0.232630  0.103486
1  0.456720         1  0.850961  0.183556  0.885724  0.993898
2  0.411568         1  0.003360  0.774391  0.822560  0.840763
3  0.148390         0  0.115748  0.089891  0.842580  0.565432
4  0.505548         0  0.370198  0.566005  0.498009  0.601986
5  0.527433         0  0.550194  0.991227  0.516154  0.283175
6  0.983699         0  0.514049  0.958328  0.005034  0.050860
7  0.923172         0  0.531747  0.026763  0.450077  0.961465
8  0.344771         1  0.332537  0.046829  0.047598  0.324098
9  0.195655         0  0.903370  0.399686  0.170009  0.578925

y = X.pop('churn')

您现在可以实例化GroupShuffleSplit,并像使用train_test_split 一样执行操作,唯一的区别是指定group 列,该列将用于拆分Xy,因此组是根据组值拆分:

gs = GroupShuffleSplit(n_splits=2, train_size=.7, random_state=42)

如前所述,当您想分成多个组时,这会更方便,通常用于交叉验证。如问题中所述,这只是一个如何获得两个拆分的示例:

train_ix, test_ix = next(gs.split(X, y, groups=X.calibrat))

X_train = X.loc[train_ix]
y_train = y.loc[train_ix]

X_test = X.loc[test_ix]
y_test = y.loc[test_ix]

给予:

print(X_train)

   customer  calibrat  churndep   revenue       mou
3  0.148390         0  0.089891  0.842580  0.565432
4  0.505548         0  0.566005  0.498009  0.601986
5  0.527433         0  0.991227  0.516154  0.283175
6  0.983699         0  0.958328  0.005034  0.050860
7  0.923172         0  0.026763  0.450077  0.961465
9  0.195655         0  0.399686  0.170009  0.578925

print(X_test)

   customer  calibrat  churndep   revenue       mou
0  0.523571         1  0.933637  0.232630  0.103486
1  0.456720         1  0.183556  0.885724  0.993898
2  0.411568         1  0.774391  0.822560  0.840763
8  0.344771         1  0.046829  0.047598  0.324098

【讨论】:

  • 感谢您的回答。我不想将数据集拆分为多个集合。我有一个数据集,它是可变数据,并且想要校准数据为 1,校准数据为 0
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-30
  • 2017-07-27
  • 1970-01-01
  • 2014-09-02
  • 1970-01-01
相关资源
最近更新 更多