# When to do feature encoding with XGBoost, random forest

**URL:** <https://forum.knime.com/t/when-to-do-feature-encoding-with-xgboost-random-forest/85598>\
**Category:** KNIME Analytics Platform\
**Tags:** big-data\
**Created:** [January 8, 2025, 11:41am UTC](https://forum.knime.com/t/when-to-do-feature-encoding-with-xgboost-random-forest/85598 "2025-01-08T11:41:40Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![pippo](https://forum-cdn.knime.com/letter_avatar_proxy/v4/letter/p/ebca7d/32.png) [@pippo](https://forum.knime.com/u/pippo)\
**Post date:** [January 8, 2025, 11:41am UTC](https://forum.knime.com/t/when-to-do-feature-encoding-with-xgboost-random-forest/85598/1 "2025-01-08T11:41:40Z")

</div>

Hi I would like to understand when it is necessary to do feature encoding. For example, I am using models like XGBoost and Random Forest. Do I need to encode categorical variables? For instance, if I have a variable like ‘education’ with levels: high-school, bachelor, master, phd, should I recode the feature as: high-school=1, bachelor=2, master=3, phd=4? Or is it better to use one-hot encoding, creating 4 new columns? What if the categorical variable doesn’t have a natural order, like ‘country of birth’? In this case, is it correct to only apply one-hot encoding?

---

<div class="post-metadata">

**Author:** ![mlauber71](https://forum-cdn.knime.com/user_avatar/forum.knime.com/mlauber71/32/73590_2.png) [@mlauber71](https://forum.knime.com/u/mlauber71)\
**Post date:** [January 8, 2025, 12:50pm UTC](https://forum.knime.com/t/when-to-do-feature-encoding-with-xgboost-random-forest/85598/2 "2025-01-08T12:50:40Z")

</div>

@pippo This will very much depend on your task. XGBoost and random Forest can handle categorical data themself. Although advanced data preparation can help to improve performance.

> [@Data Manipulation of categorical data in the background in ML process](https://forum.knime.com/t/data-manipulation-of-categorical-data-in-the-background-in-ml-process/71618/4):
>
> @moritz_skb every KNIME nodes has some sort of documentation like the [XGBoost Tree Ensemble Learner](https://hub.knime.com/knime/extensions/org.knime.features.xgboost/latest/org.knime.xgboost.base.nodes.learner.classification.XGBTreeClassificationLearnerNodeFactory2/): There you will find additional links and literature to explain what has been done. In this case to the official documentation and there you will find what the algorithm does: [https://xgboost.readthedocs.io/en/stable/tutorials/categorical.html](https://xgboost.readthedocs.io/en/stable/tutorials/categorical.html) The KNIME implementation might ‘compress’ such settings or provide some of them by default or with switches in the node. If you want to do the…

More on Data Preparation and Machine Learning you can find here:

> **[KNIME — Machine Learning and Artificial Intelligence— A Collection](https://medium.com/low-code-for-advanced-data-science/knime-machine-learning-and-artificial-intelligence-a-collection-12e0f7d83b50#5bf1)**
>
> TL;DR: Where to find resources and workflow examples for various machine-learning tasks you can do with KNIME (and some Python)

There are more advanced preparation techniques like [vtreat](https://medium.com/p/efcaf58fa783) and other Python packages. Some examples are here (I have some code still on my machine that might get published in the future):

> **[ashokharnal/Collection of Components and Workflows – feature-generation](https://hub.knime.com/ashokharnal/spaces/Collection%20of%20Components%20and%20Workflows/feature-generation~DkHY6_lFrwy1kOCs/)**
>
> This is a collection of Components and Workflows to assist me in my teaching at FORE School of Management, New Delhi – contains 13 items

---

<div class="post-metadata">

**Author:** ![system](https://forum-cdn.knime.com/uploads/default/original/1X/ab3ccf34482a0329361734a18199390177204f15.png) [@system](https://forum.knime.com/u/system)\
**Post date:** [April 8, 2025, 12:51pm UTC](https://forum.knime.com/t/when-to-do-feature-encoding-with-xgboost-random-forest/85598/3 "2025-04-08T12:51:01Z")

</div>

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.
