Problems with a binary one-hot (one-of-K) coding in python

后端 未结 2 731
我寻月下人不归
我寻月下人不归 2021-02-02 03:57

Binary one-hot (also known as one-of-K) coding lies in making one binary column for each distinct value for a categorical variable. For example, if one has a color column (categ

2条回答
  •  遥遥无期
    2021-02-02 04:03

    If your columns are in the same order, you can concatenate the dfs, use get_dummies, and then split them back again, e.g.,

    encoded = pd.get_dummies(pd.concat([train,test], axis=0))
    train_rows = train.shape[0]
    train_encoded = encoded.iloc[:train_rows, :]
    test_encoded = encoded.iloc[train_rows:, :] 
    

    If your columns are not in the same order, then you'll have challenges regardless of what method you try.

提交回复
热议问题