【问题标题】:pandas/scikit learn combining columnspandas/scikit 学习组合列
【发布时间】:2020-09-02 06:15:04
【问题描述】:

我有一个包含以下列的 pandas 数据框:

id, cookie_id, file_loc

我正在使用 scikit-learn 进行分层拆分(按id 分层):

sss = StratifiedShuffleSplit(n_splits=1, test_size=0.2, random_state=0)
X=df['file_loc']
y=df['id']
for train_index, test_index in sss.split(X, y):
    # 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]

但是,我希望将X 视为[df['cookie_id']df['file_loc']],以便X_trainy_train 包含信息(cookie_idfile_loc),但出于某种原因我无法弄清楚如何做到这一点:(

任何指针都会很棒。

【问题讨论】:

  • sk-learn 在后台使用 numpy,而不是 pandas DataFrame。仔细看一些例子,它可能不会产生二维数组而是别的东西。

标签: python pandas scikit-learn


【解决方案1】:

我猜你可能正在寻找:

X=df[['cookie_id','file_loc']]

【讨论】:

  • 这不起作用。它抛出:KeyError: "None of [Int64Index([ 764, 999, 919, 424, 1637, 1438, 524, 317, 1295, 208,\n ...\n 951, 744, 1679, 774, 1627, 1508, 669, 367, 1475, 737],\n dtype='int64', length=1378)] are in the [columns]"
  • 您能否编辑您的问题并包含部分数据框?
猜你喜欢
  • 2015-09-23
  • 2020-03-24
  • 2018-10-21
  • 2016-12-20
  • 2015-12-22
  • 2016-07-16
  • 1970-01-01
  • 2015-04-10
  • 2018-05-28
相关资源
最近更新 更多