• 机器学习sklearn(十八): 特征工程(九)特征编码(三)类别特征编码(一)标签编码 LabelEncoder


    LabelEncoder 是一个可以用来将标签规范化的工具类,它可以将标签的编码值范围限定在[0,n_classes-1]. 这在编写高效的Cython程序时是非常有用的. LabelEncoder 可以如下使用:

    >>> from sklearn import preprocessing
    >>> le = preprocessing.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 = 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']

    class sklearn.preprocessing.LabelEncoder

    Encode target labels with value between 0 and n_classes-1.

    This transformer should be used to encode target values, i.e. y, and not the input X.

    Read more in the User Guide.

    New in version 0.12.

    Attributes
    classes_ndarray of shape (n_classes,)

    Holds the label for each class.

    Examples

    LabelEncoder can be used to normalize labels.

    >>> from sklearn import preprocessing
    >>> le = preprocessing.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])

    It can also be used to transform non-numerical labels (as long as they are hashable and comparable) to numerical labels.

    >>> 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']
  • 相关阅读:
    CSS盒子模式(DIV布局快速入门)
    CSS中的滑动门技术
    由浅入深漫谈margin属性
    zz Apache 虚拟主机 VirtualHost 配置
    动态生成编译运行java类
    ubuntu 手动设置DNS服务器,重启后不能上网
    ubuntu下部署发布环境
    zz [Java]读取文件方法大全
    Ubuntu apache2 主机配置文件
    JAVA的CALLBACK
  • 原文地址:https://www.cnblogs.com/qiu-hua/p/14904568.html
Copyright © 2020-2023  润新知