使用xgboost,scikit-learn和pandas的“KeyError:0”

问题描述 投票:0回答:2

我创建了这个演示来演示从库内部抛出的错误。此代码将数据集拆分为train / eval / test,并使用train / eval进行超参数搜索,提前停止,同时保留测试集以供以后评估。我缩小了与GridSearchCV的交叉验证相关的错误,但我无法找出确切的根本原因并修复。

from sklearn import svm, datasets
from sklearn.model_selection import GridSearchCV
from sklearn.model_selection import train_test_split
import numpy as np
import pandas as pd
import xgboost as xgb

iris = datasets.load_iris()
df = pd.DataFrame(data=np.c_[iris['data'], iris['target']], columns=iris['feature_names'] + ['target'])
X, y = df[['sepal length (cm)', 'sepal width (cm)', 'petal length (cm)', 'petal width (cm)']], df['target']

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
X_train_split, X_eval_split, y_train_split, y_eval_split = train_test_split(X_train, y_train, test_size=0.25, random_state=42)

parameters = {
    'max_depth': (2, 3, 4),
}

fit_params = {
    'early_stopping_rounds': 2,
    'eval_set': (X_eval_split, y_eval_split),
}

model = xgb.XGBClassifier()
gs = GridSearchCV(model, parameters, cv=3)
gs.fit(X_train_split, y_train_split, **fit_params)

但是我收到了这个不起眼的消息:

Traceback (most recent call last):
  File "/Users/foo/bar/.env/lib/python3.6/site-packages/pandas/core/indexes/base.py", line 3078, in get_loc
    return self._engine.get_loc(key)
  File "pandas/_libs/index.pyx", line 140, in pandas._libs.index.IndexEngine.get_loc
  File "pandas/_libs/index.pyx", line 162, in pandas._libs.index.IndexEngine.get_loc
  File "pandas/_libs/hashtable_class_helper.pxi", line 1492, in pandas._libs.hashtable.PyObjectHashTable.get_item
  File "pandas/_libs/hashtable_class_helper.pxi", line 1500, in pandas._libs.hashtable.PyObjectHashTable.get_item
KeyError: 0

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "t.py", line 36, in <module>
    gs.fit(X_train_split, y_train_split, **fit_params)
  File "/Users/foo/bar/.env/lib/python3.6/site-packages/sklearn/model_selection/_search.py", line 640, in fit
    cv.split(X, y, groups)))
  File "/Users/foo/bar/.env/lib/python3.6/site-packages/sklearn/externals/joblib/parallel.py", line 779, in __call__
    while self.dispatch_one_batch(iterator):
  File "/Users/foo/bar/.env/lib/python3.6/site-packages/sklearn/externals/joblib/parallel.py", line 625, in dispatch_one_batch
    self._dispatch(tasks)
  File "/Users/foo/bar/.env/lib/python3.6/site-packages/sklearn/externals/joblib/parallel.py", line 588, in _dispatch
    job = self._backend.apply_async(batch, callback=cb)
  File "/Users/foo/bar/.env/lib/python3.6/site-packages/sklearn/externals/joblib/_parallel_backends.py", line 111, in apply_async
    result = ImmediateResult(func)
  File "/Users/foo/bar/.env/lib/python3.6/site-packages/sklearn/externals/joblib/_parallel_backends.py", line 332, in __init__
    self.results = batch()
  File "/Users/foo/bar/.env/lib/python3.6/site-packages/sklearn/externals/joblib/parallel.py", line 131, in __call__
    return [func(*args, **kwargs) for func, args, kwargs in self.items]
  File "/Users/foo/bar/.env/lib/python3.6/site-packages/sklearn/externals/joblib/parallel.py", line 131, in <listcomp>
    return [func(*args, **kwargs) for func, args, kwargs in self.items]
  File "/Users/foo/bar/.env/lib/python3.6/site-packages/sklearn/model_selection/_validation.py", line 458, in _fit_and_score
    estimator.fit(X_train, y_train, **fit_params)
  File "/Users/foo/bar/.env/lib/python3.6/site-packages/xgboost/sklearn.py", line 526, in fit
    for i in range(len(eval_set))
  File "/Users/foo/bar/.env/lib/python3.6/site-packages/xgboost/sklearn.py", line 526, in <genexpr>
    for i in range(len(eval_set))
  File "/Users/foo/bar/.env/lib/python3.6/site-packages/pandas/core/frame.py", line 2688, in __getitem__
    return self._getitem_column(key)
  File "/Users/foo/bar/.env/lib/python3.6/site-packages/pandas/core/frame.py", line 2695, in _getitem_column
    return self._get_item_cache(key)
  File "/Users/foo/bar/.env/lib/python3.6/site-packages/pandas/core/generic.py", line 2489, in _get_item_cache
    values = self._data.get(item)
  File "/Users/foo/bar/.env/lib/python3.6/site-packages/pandas/core/internals.py", line 4115, in get
    loc = self.items.get_loc(item)
  File "/Users/foo/bar/.env/lib/python3.6/site-packages/pandas/core/indexes/base.py", line 3080, in get_loc
    return self._engine.get_loc(self._maybe_cast_indexer(key))
  File "pandas/_libs/index.pyx", line 140, in pandas._libs.index.IndexEngine.get_loc
  File "pandas/_libs/index.pyx", line 162, in pandas._libs.index.IndexEngine.get_loc
  File "pandas/_libs/hashtable_class_helper.pxi", line 1492, in pandas._libs.hashtable.PyObjectHashTable.get_item
  File "pandas/_libs/hashtable_class_helper.pxi", line 1500, in pandas._libs.hashtable.PyObjectHashTable.get_item
KeyError: 0

有人可以帮我解释为什么我收到这个错误吗?

python pandas scikit-learn xgboost
2个回答
2
投票

the documentation说:

eval_set(list,optional) - 用作早期停止验证集的(X,y)元组对列表

eval_set应该是一个元组列表。但你有eval_set只是一个元组:

fit_params = {
    'early_stopping_rounds': 2,
    'eval_set': (X_eval_split, y_eval_split),
}

改为:

fit_params = {
    'early_stopping_rounds': 2,
    'eval_set': [(X_eval_split, y_eval_split)],
}

并且您的代码将运行。


0
投票

当我读完错误跟踪时,我发现fit方法存在一些问题。

KeyError:0表示解释器正在数据框中查找第0个索引位置的元素/项。我尝试运行你的X_train_split,y_train_split和X_eval_split。指数是不同的,可能会打破执行。

但是,如果我们不对数据集进行训练和评估,那么交叉验证的目的可能会被打败。

尝试在fit方法中重置事物的索引,包括评估(在参数中使用)。如果问题仍然存在,请阅读早期停止参数的概念,训练测试分割和Gridsearch cv = 3区域以检查是否存在任何不一致。

希望它能让你对错误有所了解。

© www.soinside.com 2019 - 2024. All rights reserved.