该类使用 0 到 n_classs-1 之间的值对目标标签进行编码。该转换器应用于编码目标值y,而不是输入X
LabelEncoder 可以用来规范标签
>>> from sklearn.preprocessing import LabelEncoder
>>> le = LabelEncoder()
>>> le.fit([1,2,3,4,5,6,6,6,7])
LabelEncoder()
>>> le.classes_
array([1, 2, 3, 4, 5, 6, 7])
>>> le.transform([1,2,3,4,5,6,6,6,7])
array([0, 1, 2, 3, 4, 5, 5, 5, 6], dtype=int64)
它也可以用于将非数字标签(只要它们是可哈希的和可比较的)转换为数字标签。
>>> le = preprocessing.LabelEncoder() >>> le.fit(["paris", "paris", "tokyo", "amsterdam"]) LabelEncoder() >>> list(le.classes_) ['amsterdam', 'paris', 'tokyo'] >>> le.transform(["tokyo", "tokyo", "paris"]) array([2, 2, 1]...) >>> list(le.inverse_transform([2, 2, 1])) ['tokyo', 'tokyo', 'paris']
|
| 安装标签编码器。 |
| 安装标签编码器并返回编码的标签。 | |
|
| 获取此估计量的参数。 |
| 将标签转换回原始编码。 | |
|
| 设置此估算器的参数。 |
|
| 将标签转换为归一化的编码。 |
该博客介绍了如何使用scikit-learn库中的LabelEncoder对目标变量进行编码。LabelEncoder能够将离散的分类标签转化为数值型,适用于预处理分类数据。示例中展示了对数字标签和非数字标签的编码与反编码过程。

3381

被折叠的 条评论
为什么被折叠?



