【问题标题】:Value Error : One Hot Encoder值错误:一个热编码器
【发布时间】:2018-01-17 12:08:35
【问题描述】:

我已按如下方式对 info.venue 列进行了标签编码,但是当我尝试执行 One Hot Encoding 时,它会出错。作为 ValueError:预期的 2D 数组,得到 1D 数组。

df['info.venue']=labelencoder.fit_transform(df['info.venue'])
from sklearn.preprocessing import OneHotEncoder
onehotencoder=OneHotEncoder()
var1=onehotencoder.fit_transform(df['info.venue'])

我的专栏是这样的。

info.venue
Adelaide Oval
Brabourne Stadium
Kensington Oval, Bridgetown
Kingsmead
Melbourne Cricket Ground
Melbourne Cricket Ground
Melbourne Cricket Ground
Punjab Cricket Association IS Bindra Stadium, Mohali
R Premadasa Stadium
Saurashtra Cricket Association Stadium
Shere Bangla National Stadium
Stadium Australia
Sydney Cricket Ground

我想对这个体育场名称进行编码。 但出现值错误。

【问题讨论】:

    标签: python scikit-learn one-hot-encoding


    【解决方案1】:

    OneHotEncoder 需要 2D 数组,而您通过了 1D(系列 - labelencoder.fit_transform 的 resilt) - 这可以很容易地修复 - 使用 df[['info.venue']] 而不是 df['info.venue'] (注意方括号),方法如下:

    df['info.venue']=labelencoder.fit_transform(df['info.venue'])
    R = onehotencoder.fit_transform(df[['info.venue']])
    

    其中 R 是一个稀疏的二维矩阵:

    In [155]: R
    Out[155]:
    <13x11 sparse matrix of type '<class 'numpy.float64'>'
            with 13 stored elements in Compressed Sparse Row format>
    
    In [156]: R.A
    Out[156]:
    array([[ 1.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.],
           [ 0.,  1.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.],
           [ 0.,  0.,  1.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.],
           [ 0.,  0.,  0.,  1.,  0.,  0.,  0.,  0.,  0.,  0.,  0.],
           [ 0.,  0.,  0.,  0.,  1.,  0.,  0.,  0.,  0.,  0.,  0.],
           [ 0.,  0.,  0.,  0.,  1.,  0.,  0.,  0.,  0.,  0.,  0.],
           [ 0.,  0.,  0.,  0.,  1.,  0.,  0.,  0.,  0.,  0.,  0.],
           [ 0.,  0.,  0.,  0.,  0.,  1.,  0.,  0.,  0.,  0.,  0.],
           [ 0.,  0.,  0.,  0.,  0.,  0.,  1.,  0.,  0.,  0.,  0.],
           [ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  1.,  0.,  0.,  0.],
           [ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  1.,  0.,  0.],
           [ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  1.,  0.],
           [ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  1.]])
    

    您也可以使用LabelBinarizer 直接从字符串中获取 One Hot Encoded 值:

    来源 DF:

    In [121]: df
    Out[121]:
                                    info.venue
    0                            Adelaide Oval
    1                        Brabourne Stadium
    2              Kensington Oval, Bridgetown
    3                                Kingsmead
    4                 Melbourne Cricket Ground
    ..                                     ...
    8                      R Premadasa Stadium
    9   Saurashtra Cricket Association Stadium
    10           Shere Bangla National Stadium
    11                       Stadium Australia
    12                   Sydney Cricket Ground
    
    [13 rows x 1 columns]
    

    解决方案:

    In [122]: from sklearn.preprocessing import LabelBinarizer
    
    In [123]: lb = LabelBinarizer()
    
    In [124]: r = pd.SparseDataFrame(lb.fit_transform(df['info.venue']),
         ...:                        df.index,
         ...:                        lb.classes_,
         ...:                        default_fill_value=0)
         ...:
    
    In [125]: r
    Out[125]:
        Adelaide Oval  Brabourne Stadium  Kensington Oval, Bridgetown  Kingsmead  Melbourne Cricket Ground  \
    0               1                  0                            0          0                         0
    1               0                  1                            0          0                         0
    2               0                  0                            1          0                         0
    3               0                  0                            0          1                         0
    4               0                  0                            0          0                         1
    ..            ...                ...                          ...        ...                       ...
    8               0                  0                            0          0                         0
    9               0                  0                            0          0                         0
    10              0                  0                            0          0                         0
    11              0                  0                            0          0                         0
    12              0                  0                            0          0                         0
    
        Punjab Cricket Association IS Bindra Stadium, Mohali  R Premadasa Stadium  Saurashtra Cricket Association Stadium  \
    0                                                   0                       0                                       0
    1                                                   0                       0                                       0
    2                                                   0                       0                                       0
    3                                                   0                       0                                       0
    4                                                   0                       0                                       0
    ..                                                ...                     ...                                     ...
    8                                                   0                       1                                       0
    9                                                   0                       0                                       1
    10                                                  0                       0                                       0
    11                                                  0                       0                                       0
    12                                                  0                       0                                       0
    
        Shere Bangla National Stadium  Stadium Australia  Sydney Cricket Ground
    0                               0                  0                      0
    1                               0                  0                      0
    2                               0                  0                      0
    3                               0                  0                      0
    4                               0                  0                      0
    ..                            ...                ...                    ...
    8                               0                  0                      0
    9                               0                  0                      0
    10                              1                  0                      0
    11                              0                  1                      0
    12                              0                  0                      1
    
    [13 rows x 11 columns]
    

    【讨论】:

    • 他已经在使用 LabelEncoder 将字符串转换为数字。所以他只需要将dataframe返回的1D数据转换为2D即可。 :)
    • @VivekKumar,哦,我现在看到了(在正确格式化问题后)-谢谢! PS我已经相应地编辑了我的答案
    • " (0, 3) 1.0 (1, 4) 1.0 (2, 1) 1.0 (3, 2) 1.0 (4, 9) 1.0 (5, 4) 1.0 (6, 6) 1.0 (7, 7) 1.0 (8, 8) 1.0 (9, 0) 1.0 (10, 4) 1.0 (11, 10) 1.0 (12, 5) 1.0" 为什么我得到这样的输出??
    • 我把我的代码改成了这个.. 基本上在 '[' "onehotencoder=OneHotEncoder() df['info.venue']=onehotencoder.fit_transform(df[[ 'info.venue']])"
    • @MayurMahajan,您不能将二维矩阵(OneHotEncoding 的结果)分配给单个列
    猜你喜欢
    • 2020-02-02
    • 1970-01-01
    • 2021-09-11
    • 2020-11-21
    • 2020-01-30
    • 1970-01-01
    • 2013-07-06
    • 2023-03-18
    • 2018-10-31
    相关资源
    最近更新 更多