gpt4 book ai didi

python - 值错误 : y contains previously unseen labels: 'A' using OrdinalEncoder

转载 作者:行者123 更新时间:2023-12-05 06:47:33 28 4
gpt4 key购买 nike

label_encode_dict['cat116']
# output
OrdinalEncoder(handle_unknown='use_encoded_value', unknown_value=-99)

label_encode_dict['cat116'].transform(np.array([xq['cat116']]).reshape(-1,1))

堆栈跟踪

---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
/usr/local/lib/python3.7/dist-packages/sklearn/utils/_encode.py in _encode(values, uniques, check_unknown)
177 try:
--> 178 return _map_to_integer(values, uniques)
179 except KeyError as e:

6 frames
/usr/local/lib/python3.7/dist-packages/sklearn/utils/_encode.py in _map_to_integer(values, uniques)
122 table = _nandict({val: i for i, val in enumerate(uniques)})
--> 123 return np.array([table[v] for v in values])
124

/usr/local/lib/python3.7/dist-packages/sklearn/utils/_encode.py in <listcomp>(.0)
122 table = _nandict({val: i for i, val in enumerate(uniques)})
--> 123 return np.array([table[v] for v in values])
124

/usr/local/lib/python3.7/dist-packages/sklearn/utils/_encode.py in __missing__(self, key)
116 return self.nan_value
--> 117 raise KeyError(key)
118

KeyError: 'A'

During handling of the above exception, another exception occurred:

ValueError Traceback (most recent call last)
<ipython-input-120-25237eb44032> in <module>()
----> 1 label_encode_dict['cat116'].transform(np.array([xq['cat116']]).reshape(-1,1))

/usr/local/lib/python3.7/dist-packages/sklearn/preprocessing/_encoders.py in transform(self, X)
785 Transformed input.
786 """
--> 787 X_int, X_mask = self._transform(X, handle_unknown=self.handle_unknown)
788 X_trans = X_int.astype(self.dtype, copy=False)
789

/usr/local/lib/python3.7/dist-packages/sklearn/preprocessing/_encoders.py in _transform(self, X, handle_unknown, force_all_finite)
152 # already called above.
153 X_int[:, i] = _encode(Xi, uniques=self.categories_[i],
--> 154 check_unknown=False)
155
156 return X_int, X_mask

/usr/local/lib/python3.7/dist-packages/sklearn/utils/_encode.py in _encode(values, uniques, check_unknown)
178 return _map_to_integer(values, uniques)
179 except KeyError as e:
--> 180 raise ValueError(f"y contains previously unseen labels: {str(e)}")
181 else:
182 if check_unknown:

ValueError: y contains previously unseen labels: 'A'

为什么这个看不见的标签错误是因为 handle_unknown 已经在 Ordinal Encoder 中指定了。

Sk 学习版本 = 0.24.1

**编辑:简短示例**

from sklearn.preprocessing import OrdinalEncoder
enc = OrdinalEncoder(handle_unknown = 'use_encoded_value', unknown_value = -9)
PP = [['AA','B']]
enc.fit(PP)

print(enc.categories_)

enc.transform(np.array(['A','B']).reshape(1,-1)) # gives the same ValueError: y contains previously unseen labels: 'A'

enc.transform(np.array(['AC','B']).reshape(1,-1)) # this works as expected

我现在认为它处理高于字母顺序的 unkown_values。在上面的例子中,'A' 小于 'AA'。结果出错了。有什么转机吗?

最佳答案

你不适合序号编码器,它不知道如何转换数据。

enc = OrdinalEncoder(handle_unknown='use_encoded_value', unknown_value=-99)
enc.fit(label_encode_dict['cat116'])
enc.transform(np.array([xq['cat116']])

关于python - 值错误 : y contains previously unseen labels: 'A' using OrdinalEncoder,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67063883/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com