【问题标题】:Why I am getting the error for GroupShuffleSplit (train test split)为什么我收到 GroupShuffleSplit 错误(训练测试拆分)
【发布时间】:2021-09-20 02:16:36
【问题描述】:

我有 2 个数据集并应用了 5 个不同的机器学习模型。

数据集 1:

def dataset_1():
    ...
    ...
    bike_data_hours = bike_data_hours[:500]
    X = bike_data_hours.iloc[:, :-1].values
    y = bike_data_hours.iloc[:, -1].values
    X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)
    return X_train, X_test, y_train.reshape(-1, 1), y_test.reshape(-1, 1)

形状是(400, 14) (100, 14) (400, 1) (100, 1)dtypes: object (int64, float64)。

数据集 2:

def dataset_2():
    ...
    ...
    final_movie_df = final_movie_df[:500]
    X = final_movie_df.iloc[:, :-1]
    y = final_movie_df.iloc[:, -1]
    gs = GroupShuffleSplit(n_splits=2, test_size=0.2)
    train_ix, test_ix = next(gs.split(X, y, groups=X.UserID))
    X_train = X.iloc[train_ix]
    y_train = y.iloc[train_ix]
    X_test = X.iloc[test_ix]
    y_test = y.iloc[test_ix]
    return X_train.shape, X_test.shape, y_train.values.reshape(-1,1).shape, y_test.values.reshape(-1,1).shape

形状是(400, 25) (100, 25) (400, 1) (100, 1)dtypes: object (int64, float64)。

我正在使用不同的模型。代码是

    X_train, X_test, y_train, y_test = dataset
    fold_residuals, fold_dfs = [], []
    kf = KFold(n_splits=k, shuffle=True)
    for train_index, _ in kf.split(X_train):
        if reg_name == "RF" or reg_name == "SVR":
            preds = regressor.fit(X_train[train_index], y_train[train_index].ravel()).predict(X_test)
        elif reg_name == "Knn-5":
            preds = regressor.fit(X_train[train_index], np.ravel(y_train[train_index], order="C")).predict(X_test)
        else:
            preds = regressor.fit(X_train[train_index], y_train[train_index]).predict(X_test)

但我遇到了一个常见错误,例如 thisthisthis。我已经浏览了所有这些帖子,但对错误一无所知。我使用了ilocvalues 作为访问链接的解决方案。

preds = regressor.fit(X_train[train_index], y_train[train_index]).predict(X_test)
  File "/home/fgd/.local/lib/python3.8/site-packages/pandas/core/frame.py", line 3030, in __getitem__
    indexer = self.loc._get_listlike_indexer(key, axis=1, raise_missing=True)[1]
  File "/home/fgd/.local/lib/python3.8/site-packages/pandas/core/indexing.py", line 1266, in _get_listlike_indexer
    self._validate_read_indexer(keyarr, indexer, axis, raise_missing=raise_missing)
  File "/home/fgd/.local/lib/python3.8/site-packages/pandas/core/indexing.py", line 1308, in _validate_read_indexer
    raise KeyError(f"None of [{key}] are in the [{axis_name}]")
KeyError: "None of [Int64Index([  0,   1,   3,   4,   5,   6,   7,   9,  10,  11,\n            ...\n            387, 388, 389, 390, 391, 392, 393, 395, 397, 399],\n           dtype='int64', length=320)] are in the [columns]"

在这里,如果我使用train_test_split 而不是GroupShuffleSplit,那么代码就可以工作了。但是,我想基于UserID 使用GroupShuffleSplit,这样同一用户就不会同时进行训练和测试。您能告诉我在使用GroupShuffleSplit 的同时如何解决这个问题吗?

你能告诉我为什么我收到dataset_2 的错误,而dataset_1 工作正常(并且shapedtypes)对于两个数据集都是相同的。

【问题讨论】:

    标签: python python-3.x machine-learning scikit-learn train-test-split


    【解决方案1】:

    您的 dataset_2 必须使用 values。做改变

        X_train = X.iloc[train_ix].values
        y_train = y.iloc[train_ix].values
        X_test = X.iloc[test_ix].values
        y_test = y.iloc[test_ix].values
        return X_train.shape, X_test.shape, y_train.reshape(-1,1).shape, y_test.reshape(-1,1).shape
    

    希望现在可以工作

    【讨论】:

      猜你喜欢
      • 2021-09-10
      • 2020-09-09
      • 2021-06-28
      • 2018-12-21
      • 1970-01-01
      • 1970-01-01
      • 2017-08-30
      • 2021-03-02
      • 1970-01-01
      相关资源
      最近更新 更多