LabelEncoder 标签编码器#
- class sklearn.preprocessing.LabelEncoder[source]#
- 将目标标签编码为0到n_classes-1之间的值。 - 此变换器应用于编码目标值,即 - y,而不是输入- X。- 更多信息请参见用户指南。 - 版本0.12中添加。 - 属性:
- classes_ndarray of shape (n_classes,)
- 保存每个类的标签。 
 
 - 另请参见 - OrdinalEncoder 序数编码器
- 使用序数编码方案对分类特征进行编码。 
- OneHotEncoder 独热编码器
- 将分类特征编码为独热数值数组。 
 - 示例 - LabelEncoder可用于规范化标签。- >>> from sklearn.preprocessing import LabelEncoder >>> le = LabelEncoder() >>> le.fit([1, 2, 2, 6]) LabelEncoder() >>> le.classes_ array([1, 2, 6]) >>> le.transform([1, 1, 2, 6]) array([0, 0, 1, 2]...) >>> le.inverse_transform([0, 0, 1, 2]) array([1, 1, 2, 6]) - 它还可以用于将非数值标签(只要它们是可哈希和可比较的)转换为数值标签。 - >>> le = LabelEncoder() >>> le.fit(["paris", "paris", "tokyo", "amsterdam"]) LabelEncoder() >>> list(le.classes_) [np.str_('amsterdam'), np.str_('paris'), np.str_('tokyo')] >>> le.transform(["tokyo", "tokyo", "paris"]) array([2, 2, 1]...) >>> list(le.inverse_transform([2, 2, 1])) [np.str_('tokyo'), np.str_('tokyo'), np.str_('paris')] - fit(y)[source]#
- 拟合标签编码器。 - 参数:
- yarray-like of shape (n_samples,)
- 目标值。 
 
- 返回值:
- selfreturns an instance of self.
- 已拟合的标签编码器。 
 
 
 - fit_transform(y)[source]#
- 拟合标签编码器并返回编码后的标签。 - 参数:
- yarray-like of shape (n_samples,)
- 目标值。 
 
- 返回值:
- yarray-like of shape (n_samples,)
- 编码后的标签。 
 
 
 - get_metadata_routing()[source]#
- 获取此对象的元数据路由。 - 请查看 用户指南,了解路由机制的工作原理。 - 返回值:
- routingMetadataRequest
- 一个 - MetadataRequest封装了路由信息。
 
 
 - get_params(deep=True)[source]#
- 获取此估计器的参数。 - 参数:
- deepbool, default=True
- 如果为 True,则将返回此估计器和包含的作为估计器的子对象的参数。 
 
- 返回值:
- paramsdict
- 参数名称与其值的映射。 
 
 
 - inverse_transform(y)[source]#
- 将标签转换回原始编码。 - 参数:
- yarray-like of shape (n_samples,)
- 目标值。 
 
- 返回值:
- yndarray of shape (n_samples,)
- 原始编码。 
 
 
 - set_output(*, transform=None)[source]#
- 设置输出容器。 - 参见 介绍 set_output API,了解如何使用此 API 的示例。 - 参数:
- transform{“default”, “pandas”, “polars”}, default=None
- 配置 - transform和- fit_transform的输出。- "default": 变换器的默认输出格式
- "pandas": DataFrame 输出
- "polars": Polars 输出
- None: 变换配置不变
 - Added in version 1.4: - "polars"选项已添加。
 
- 返回值:
- selfestimator instance
- 估计器实例。 
 
 
 
