I have a question about One-Hot-Encoding, something confusing me.:\

I have this sample dataset. My dataset is categorical:

F1 F2 F3 F4 Target
1 Blue 3 Car Yes
4 Red 6 Ship No
3 Pink 3 Cow Yes
9 Black 6 Fish Yes


Should I use the One-Hot-Encoding for F2 and F4 too?

This is clear for me, that must be used one hot encoding for column Target. But what about the Features?

If the answer is yes why and if the answer is no why?

Thanks for your support.


1 Answer 1


Correct, all categorical features should be encoded into binary digits so ML algo have more predictive power as categorised features cannot have order or magnitude ( be careful also about multicollinearity if you use regression)

Some ML framework such as Catboost automatically encode features for you if you specify the feature index. I also like using python patsy to create input matrices

  • $\begingroup$ Thank u for your answer. The question is, this Encoding should be done before scaling and train_test_split? $\endgroup$
    – Jsmoka
    Commented Feb 7, 2021 at 11:57
  • $\begingroup$ As above, should only scale F1 and F3 - the one-hot encodings will alrady be either 0 or 1, so scaling doesn't make much sense. However, train_test_split in my mind should always be the first thing you do - if your preprocessing scales by the mean, if you don't split first, you are scaling training data by a mean "infected" with test data - which is a no-no. $\endgroup$
    – Ken Syme
    Commented Feb 8, 2021 at 12:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.