# one hot encoding target variable in tree and non tree (knn) methods

I am learning about label encoders, one hot encoding etc applied to datasets for classification via KNN and XGBoost type trees. However, I am a bit confused as to whether the target variable should be one hot encoded or not.

In my case, I have a dataframe which has 500x10 size and the last column is the target (for prediction) country column while the rest of the 499 columns contain floating point values. So, an example row:

feature1 ..... fetaure499 target
1.1  ......... 40.1       Germany
6.1  ......... 265.7       USA
8.1  ......... 98,7      Singapore


In this case, what is the optimal way to encode the target for: [1] KNN [2] tree based methods.

My relatively naive understanding says that one hot encoding is useful for the feature space while for the target such as this, i.e list of countries labelencoding should be enough. Is this true for this case? Are there any situations I need to aware of when the target has to be label encoded?

Thank you.

• Does this answer your question? Encode multi-class response variable Apr 19 at 14:56
• @BenReiniger yea, it kinda does - so, the essense is that scikit-learn does not care about the target output i.e one can labelencode the target output and thats it. Is that correct? Does this apply to say xg-boost aswell?
– AJW
Apr 19 at 16:49
• I'm not sure offhand how xgboost handles its targets. For sklearn, I'd suggest not encoding at all: that way it still knows what the targets are, often exposed through the classes_ attribute. Apr 20 at 14:24