Why is one-hot encoding commonly used to handle categorical variables in machine learning?
Think about how machine learning models interpret numbers and categories.
One-hot encoding creates separate binary features for each category, avoiding any false order or priority between categories.
What is the output of the following Python code using sklearn's LabelEncoder?
from sklearn.preprocessing import LabelEncoder le = LabelEncoder() categories = ['red', 'blue', 'green', 'blue', 'red'] encoded = le.fit_transform(categories) print(list(encoded))
LabelEncoder assigns integers in alphabetical order of categories.
LabelEncoder assigns 0 to 'blue', 1 to 'green', and 2 to 'red' because of alphabetical sorting.
You have a dataset with a categorical feature containing 10,000 unique categories. Which model is best suited to handle this feature without extensive preprocessing?
Consider how models handle many categories and the risk of too many features.
Gradient Boosting with target encoding reduces dimensionality and captures category information effectively for high-cardinality features.
Which encoding method is generally preferred for categorical variables when using tree-based models like Random Forest or XGBoost?
Tree models can split on integer values without assuming order.
Label encoding works well with tree models because they split on values and do not assume numeric order, avoiding high dimensionality from one-hot encoding.
You trained two models on the same dataset: Model A uses one-hot encoding for categorical variables, Model B uses target encoding. Both models are gradient boosting classifiers. Model A has 85% accuracy, Model B has 88% accuracy on the test set. What is the most likely explanation?
Think about how encoding affects feature representation and model learning.
Target encoding can provide more meaningful numeric values for categories, helping gradient boosting models learn better patterns and improve accuracy.
