【问题标题】:KeyError: 0 when using preprocess_testKeyError: 0 使用 preprocess_test 时
【发布时间】:2020-11-18 09:35:22
【问题描述】:

preprocess_train 运行成功。

有没有人在使用 preprocess_test / ktrain (0.18.5)时遇到过类似的KeyError?

preprocessing test...
language: en
test sequence lengths:
    mean : 608
    95percentile : 2102
    99percentile : 4783
---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
 in 
----> 1 val = t.preprocess_test(X_test, y_test)

~/.local/lib/python3.6/site-packages/ktrain/text/preprocessor.py in preprocess_test(self, texts, y, verbose)
   1172         """
   1173         self.check_trained()
-> 1174         return self.preprocess_train(texts, y=y, mode='test', verbose=verbose)
   1175 
   1176 

~/.local/lib/python3.6/site-packages/ktrain/text/preprocessor.py in preprocess_train(self, texts, y, mode, verbose)
   1142           TransformerDataset if self.use_with_learner = True else tf.Dataset
   1143         """
-> 1144         tseq = super().preprocess_train(texts, y=y, mode=mode, verbose=verbose)
   1145         if self.use_with_learner: return tseq
   1146         tseq.batch_size = self.batch_size

~/.local/lib/python3.6/site-packages/ktrain/text/preprocessor.py in preprocess_train(self, texts, y, mode, verbose)
    910         elif y is None:
    911             y = np.array([1] * len(texts))
--> 912         y = self._transform_y(y, verbose=verbose)
    913 
    914         # convert examples

~/.local/lib/python3.6/site-packages/ktrain/text/preprocessor.py in _transform_y(self, y_data, verbose)
    505 
    506         # check for errors and warnings
--> 507         if not isinstance(y_data[0], str) and len(y_data.shape) ==1 and not self.get_classes():
    508             if verbose:
    509                 warnings.warn('Task is being treated as TEXT REGRESSION because ' +\

~/.local/lib/python3.6/site-packages/pandas/core/series.py in __getitem__(self, key)
    869         key = com.apply_if_callable(key, self)
    870         try:
--> 871             result = self.index.get_value(self, key)
    872 
    873             if not is_scalar(result):

~/.local/lib/python3.6/site-packages/pandas/core/indexes/base.py in get_value(self, series, key)
   4403         k = self._convert_scalar_indexer(k, kind="getitem")
   4404         try:
-> 4405             return self._engine.get_value(s, k, tz=getattr(series.dtype, "tz", None))
   4406         except KeyError as e1:
   4407             if len(self) > 0 and (self.holds_integer() or self.is_boolean()):

pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_value()

pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_value()

pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.Int64HashTable.get_item()

pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.Int64HashTable.get_item()

KeyError: 0

【问题讨论】:

    标签: python ktrain


    【解决方案1】:

    看起来您正在向preprocess_test 提供熊猫系列,而不是 NumPy 数组或 Python 列表。如果X_testy_test 是pandas 系列,请将X_test.valuesy_test.values 提供给preprocess_test

    【讨论】:

    • 谢谢@blustax 我已经将 X_test 转换为 List.. 将查看 y_test。
    • 谢谢@blustax。现在可以了。检查并将 y_test 更改为 y_test.values 很好奇为什么 preprocess_train 和 preprocess_test 的行为不同。 preprocess_train 将 y_train 作为一个系列,它没有抱怨
    猜你喜欢
    • 1970-01-01
    • 2022-07-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-16
    • 2018-05-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多