【问题标题】:how to make one hot encoding to column in data frame in python如何在python中对数据框中的列进行一种热编码
【发布时间】:2020-11-21 01:30:44
【问题描述】:

我的数据集包含教育级别的分类列 初始值为0,nan,高中,研究生,大学 我已经清理了数据并将其转换为以下值

0-> 其他 1-> 高中 2-> 研究生院 3-> 大学

在同一列中,现在我想将此列热编码为 4 列

我尝试使用 scikit learn 如下

onehot_encoder = OneHotEncoder()
onehot_encoded = onehot_encoder.fit_transform(df_csv['EDUCATION'])
print(onehot_encoded)

我收到了这个错误

ValueError: Expected 2D array, got 1D array instead:
array=[3 3 3 ... 3 1 3].
Reshape your data either using array.reshape(-1, 1) if your data has a single feature or array.reshape(1, -1) if it contains a single sample.

【问题讨论】:

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


    【解决方案1】:

    对于您的具体情况,如果您重塑底层数组(连同设置sparse=False),它将为您提供一次性编码数组:

    import pandas as pd
    from sklearn.preprocessing import OneHotEncoder
    
    df = pd.DataFrame({'EDUCATION':['high school','high school','high school',
                                    'university','university','university',
                                    'graduate school', 'graduate school','graduate school',
                                    'others','others','others']})
    
    onehot_encoder = OneHotEncoder(sparse=False)
    onehot_encoder.fit_transform(df['EDUCATION'].to_numpy().reshape(-1,1))
    
    >>>
    
    array([[0., 1., 0., 0.],
           [0., 1., 0., 0.],
           [0., 1., 0., 0.],
           [0., 0., 0., 1.],
           [0., 0., 0., 1.],
           [0., 0., 0., 1.],
           [1., 0., 0., 0.],
           [1., 0., 0., 0.],
           [1., 0., 0., 0.],
           [0., 0., 1., 0.],
           [0., 0., 1., 0.],
           [0., 0., 1., 0.]])
    

    我认为最直接的方法是使用pandas.get_dummies

    pd.get_dummies(df['EDUCATION'])
    

    【讨论】:

      【解决方案2】:

      您需要将sparse 设置为False

      from sklearn.preprocessing import OneHotEncoder
      
      onehot_encoder = OneHotEncoder(sparse=False)
      y_train = np.random.randint(0,4,100)[:,None]
      y_train = onehot_encoder.fit_transform(y_train)
      

      或者,你也可以这样做

      from sklearn.preprocessing import LabelEncoder
      from keras.utils import np_utils
      
      y_train = np.random.randint(0,4,100)
      encoder = LabelEncoder()
      encoder.fit(y_train)
      encoded_y = encoder.transform(y_train)
      y_train = np_utils.to_categorical(encoded_y)
      

      【讨论】:

        猜你喜欢
        • 2021-11-16
        • 2019-02-14
        • 2020-11-14
        • 2017-05-02
        • 2022-09-27
        • 2018-12-28
        • 2019-09-24
        • 1970-01-01
        • 2018-12-17
        相关资源
        最近更新 更多