LabelEncoder和OneHotEncoder

# 将离散型的数据转换成0到n-1之间的数,这里的n是一个列表的不同取值的个数,可以认为是某个特征的所有不同取值的个数
def testLabelEncoder():
    from sklearn.preprocessing import LabelEncoder

    labelencoder = LabelEncoder()

    x = labelencoder.fit_transform([1, 1, 100, 67, 5])

    # [0 0 3 2 1]
    print(x)

# 对于离散的特征基本就是按照one-hot(独热)编码,该离散特征有多少取值,就用多少维来表示该特征
def testOneHotEncoder():
    from sklearn.preprocessing import OneHotEncoder

    oneHotEncoder = OneHotEncoder()

    x = oneHotEncoder.fit_transform([[2], [1], [3], [4]]).toarray()

    '''
    [[0. 1. 0. 0.]
    [1. 0. 0. 0.]
    [0. 0. 1. 0.]
    [0. 0. 0. 1.]]
    '''
    print(x)

def testOneHotEncoder2():
    from sklearn import preprocessing
    enc = preprocessing.OneHotEncoder()
    enc.fit([[0, 0, 3], [1, 1, 0], [0, 2, 1], [1, 0, 2]])    # fit来学习编码
    x = enc.transform([[0, 1, 3]]).toarray()    # 进行编码
    # [[1. 0. 0. 1. 0. 0. 0. 0. 1.]]
    print(x)

    '''
    数据矩阵是4*3,即4个数据,3个特征维度。

    0 0 3        观察左边的数据矩阵,第一列为第一个特征维度,有两种取值0\1. 所以对应编码方式为10 、01

    1 1 0        同理,第二列为第二个特征维度,有三种取值0\1\2,所以对应编码方式为100、010、001

    0 2 1        同理,第三列为第三个特征维度,有四中取值0\1\2\3,所以对应编码方式为1000、0100、0010、0001

    1 0 2

    再来看要进行编码的参数[0 , 1,  3], 0作为第一个特征编码为10,  1作为第二个特征编码为010, 3作为第三个特征编码为0001.  故此编码结果为 1 0 0 1 0 0 0 0 1
    '''

if __name__ == "__main__":
    testOneHotEncoder2()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值