Home > Net >  Encoding method for a categorical variable
Encoding method for a categorical variable

Time:06-06

Suppose we have a categorical variable

Age['0-17','18-25','35-40','55 ']

What should we prefer; OneHotEncoding, LabelEncoding or Mapping (like assigning data values such as '0-17':1, '18-25':2) and Why?

CodePudding user response:

You can solve this problem with pure python like below:

age = ['0-17','18-25','35-40','40-55', '55-70', '70-85', '85 ']
rng = range(len(age))
# If you want label start from '1' 
# rng = range(1,len(age) 1)
res = dict(zip(age, rng))
print(res)

Output:

{'0-17': 0, '18-25': 1, '35-40': 2, '40-55': 3, '55-70': 4, '70-85': 5, '85 ': 6}
  • Related