我有一个包含不同权重的分类数据的数据集,例如,博士的权重高于硕士,而理学硕士的权重高于理学学士。
我知道要使用标签编码器,但我不希望 python 为这些变量任意分配代码。我想要更高的代码:Phd = 4、Msc = 3、Bsc = 2、O Levels = 1 和无教育 = 0。
我可以解决这个问题吗?有人可以帮忙吗?
最佳答案
LabelEncoder 将根据字母顺序对类别进行编码并存储在 classes_
属性中。默认情况下是这样的:
from sklearn.preprocessing import LabelEncoder
le = LabelEncoder()
le.fit(['Phd', 'Msc','Bsc', 'O Levels','No education'])
ll.classes_
# Output: array(['Bsc', 'Msc', 'No education', 'O Levels', 'Phd'], dtype='|S12')
有多少个类别?如果较少,您可以自己使用字典进行转换,类似于 this answer here :
my_dict = {'Phd':4, 'Msc':3 , 'Bsc':2, 'O Levels':1, 'No education':0}
y = ['No education', 'O Levels','Bsc', 'Msc','Phd']
np.vectorize(my_dict.get)(y)
# Output: array([0, 1, 2, 3, 4])
关于python - 回归中的序数分类数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49854070/