我想对序数变量进行编码。例如,客户的满意度有4个级别:非常好、良好、中等和差。我尝试使用 scikit-learn
库中的 LabelEncoder
,但编码返回值 Very Good,这应该是最好的值,是 2 而不是 3。
我想知道是否可以在 LabelEncoder
方法中为每个级别设置具体值。
最佳答案
您可以使用OrdinalEncoder
并提供您自己的映射表。映射表的格式是一个列表列表,其中第 n 个列表保存输入数据第 n 列中的值。
from sklearn.preprocessing import OrdinalEncoder
import random
import pandas as pd
# the categorical values in the right order
satisfaction = ['Poor', 'Moderate', 'Good', 'Very Good']
# create the mapping list
mapping = [satisfaction]
# create some random data but reproducible data
random.seed(42)
X = pd.DataFrame({'satisfaction': [random.choice(satisfaction) for _ in range(25)]})
print(X)
0 Poor 1 Poor 2 Good 3 Moderate 4 Moderate 5 Moderate
[...]
# create the encoder
enc = OrdinalEncoder(categories=mapping)
# transform your data
print(enc.fit_transform(X))
[[0.] [0.] [2.] [1.] [1.] [1.] ... ]
关于python - 想了解 Scikit-Learn 中的编码算法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57025433/