TransWikia.com

String indices must be integers

Data Science Asked by Abel on March 12, 2021

enter image description here

I was trying to encode the string values of the feature ‘ProductCategory’ into integer values but I got this error. Kindly help.

And I would also like to ask if label-encoding this feature would not force my model to misrepresent the integer values as 0<1<2. Thanks.

One Answer

LabelEncoding, if not processed again before passing into the model, will impose an order on the categories (namely the 0<1<3) that you mentioned. Here, since it does not seem like you have a lot of categories, one-hot encoding is the easiest to implement and also more effective (the categorical values are nominal rather than ordinal here).

These two articles are a decent starting point for categorical encoding in Python: https://pbpython.com/categorical-encoding.html

https://medium.com/@contactsunny/label-encoder-vs-one-hot-encoder-in-machine-learning-3fc273365621

As for the TypeError, can you double check what variable type train_df is?

Answered by Alexander Wang on March 12, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP