列未编码

问题描述

我有一列名为“教育”的书。我正在尝试使用Label Encoder对其进行编码,但弹出以下错误

[我无法发布图片,因此数据的链接附在下面] [1]

TypeError                                 Traceback (most recent call last)
/opt/conda/lib/python3.7/site-packages/sklearn/preprocessing/_label.py in _encode(values,uniques,encode,check_unkNown)
    112         try:
--> 113             res = _encode_python(values,encode)
    114         except TypeError:

/opt/conda/lib/python3.7/site-packages/sklearn/preprocessing/_label.py in _encode_python(values,encode)
     60     if uniques is None:
---> 61         uniques = sorted(set(values))
     62         uniques = np.array(uniques,dtype=values.dtype)

TypeError: '<' not supported between instances of 'str' and 'float'

During handling of the above exception,another exception occurred:

TypeError                                 Traceback (most recent call last)
<ipython-input-90-64573b8efccb> in <module>
----> 1 df_temp['education']=encoder1.fit_transform(df_temp['education'])

/opt/conda/lib/python3.7/site-packages/sklearn/preprocessing/_label.py in fit_transform(self,y)
    254         """
    255         y = column_or_1d(y,warn=True)
--> 256         self.classes_,y = _encode(y,encode=True)
    257         return y
    258 

/opt/conda/lib/python3.7/site-packages/sklearn/preprocessing/_label.py in _encode(values,check_unkNown)
    115             types = sorted(t.__qualname__
    116                            for t in set(type(v) for v in values))
--> 117             raise TypeError("Encoders require their input to be uniformly "
    118                             f"strings or numbers. Got {types}")
    119         return res

TypeError: Encoders require their input to be uniformly strings or numbers. Got ['float','str']  ```


  


  [1]: https://i.stack.imgur.com/5F2uC.png

解决方法

您可以尝试


df_temp['education']=encoder1.fit_transform(df_temp['education'].astype(str))