使用 LabelEncoder 转换数据

Posted

技术标签:

【中文标题】使用 LabelEncoder 转换数据【英文标题】:convert data with LabelEncoder 【发布时间】:2021-07-21 09:36:06 【问题描述】:

我写了这个函数来用 LabelEncoder 转换分类特征

#convert columns to dummies with LabelEncoder
cols = ['ToolType', 'TestType', 'BatteryType']
#apply ene hot encoder
le = LabelEncoder()
for col in cols:
    data[col] = data[col].astype('|S') #convert object to str type before apply label encoder
    le.fit(ravel(data[col]))
    data[col] = le.transform(ravel(data[col]))

这些列中有空值,但是有这样的错误

TypeError: ufunc 'isnan' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''

有谁知道如何帮助我解决这个问题?谢谢

【问题讨论】:

【参考方案1】:

此行正在转换为编码器不支持的 numpy bytes_

data[col] = data[col].astype('|S')

如果要转成字符串,把'|S'改成str

data[col] = data[col].astype(str)

作为旁注,您可以使用apply()fit_transform 将循环缩减为一行:

df[cols] = df[cols].astype(str).apply(le.fit_transform)

【讨论】:

以上是关于使用 LabelEncoder 转换数据的主要内容,如果未能解决你的问题,请参考以下文章