I am working on a cab booking prediction problem where I need to use datetime aspects like hour,day ,week etc for prediction. As I need to do categorical encoding for the purpose. can anyone help me out on which one to use i.e label encoder or one hot encoder. I am not able to gather much information on the web regarding this. I tried doing label encoding but as onehotencoding is preferred for ordinal data like this. Any help would be appreciated.
One hot encoding removed the order information compared to the label encoder. Which one to use depends on the specific question. If you think the order of categories matters, use the label encoder. If not, use the one-hot encoder. For booking prediction, I will prefer one hot.
I am not sure what you are going to study but one-hot encoding is mostly preferred over labeling. Here is my reason: one-hot encoding eliminate the ordinality of the column data and it is helpful to your modeling. This is because your model is not as smart as us humans that they can recognize your input as orders or values on its own. In other words, if you use label encoding, chances are that your model misunderstands your input as the pure value, and the explainability of your model is backfired: in this dimension, the model considers the values which have a larger ordinals are weighted more.
But the shortage of one-hot encoding is obvious: it requires more RAMs than the original set, especially there are tons of unique values.
Here is the python snippet of one-hot encoding implementation:
from sklearn.preprocessing import OneHotEncoder X = [['Male', 1], ['Female', 3], ['Female', 2]] enc = OneHotEncoder(handle_unknown='ignore') enc.fit(X) # OneHotEncoder(handle_unknown='ignore') enc.categories_ # [array(['Female', 'Male'], dtype=object), array([1, 2, 3], dtype=object)] enc.transform([['Female', 1], ['Male', 4]]).toarray() # array([[1., 0., 1., 0., 0.], # [0., 1., 0., 0., 0.]]) enc.inverse_transform([[0, 1, 1, 0, 0], [0, 0, 0, 1, 0]]) # array([['Male', 1], # [None, 2]], dtype=object) enc.get_feature_names(['gender', 'group']) # array(['gender_Female', 'gender_Male', 'group_1', 'group_2', 'group_3'], # dtype=object)
I would say since you are predicting cab bookings, you should go for ordinal encoding. The reason is because ordinal encoding preserves the order of the feature and cab bookings also have peak hours/days when they are more likely to be booked and hence need a higher order/importance.
For example cabs are more likely to be booked during weekends (people are more likely to be going out) than on weekdays. Hence the model needs to learn this trait and give more importance to weekends.
One hot encoding would just eliminate this sense of order which would lead to the model not learning this trait.