python类别变量(class_label)转换为One_Hot的几种方式

    首先解析一下,one_hot (独热)编码,和dummy variable(哑变量)的区别:

    在用keras时候,有一个模块写好one_hot转换

from keras.utils import to_categorical
data = [1, 3, 2, 0, 3, 2, 2, 1, 0, 1]
encoded=to_categorical(data)
print(encoded)

则打印出来的结果为:

[[ 0.  1.  0.  0.]
 [ 0.  0.  0.  1.]
 [ 0.  0.  1.  0.]
 [ 1.  0.  0.  0.]
 [ 0.  0.  0.  1.]
 [ 0.  0.  1.  0.]
 [ 0.  0.  1.  0.]
 [ 0.  1.  0.  0.]
 [ 1.  0.  0.  0.]
 [ 0.  1.  0.  0.]]
def to_categorical(y, num_classes=None):
    y = np.array(y, dtype='int')
    input_shape = y.shape
    if input_shape and input_shape[-1] == 1 and len(input_shape) > 1:
        input_shape = tuple(input_shape[:-1])
    y = y.ravel()
    if not num_classes:
        num_classes = np.max(y) + 1
    n = y.shape[0]
    categorical = np.zeros((n, num_classes))
    categorical[np.arange(n), y] = 1
    output_shape = input_shape + (num_classes,)
    categorical = np.reshape(categorical, output_shape)
    return categorical

在sklearn中的one_hot编码

from sklearn import preprocessing
import numpy as np
label = preprocessing.LabelEncoder()
one_hot = preprocessing.OneHotEncoder(sparse = False)
cat_data =([1,3,2], 
           [2,1,1],
          [4,2,2])
print one_hot.fit_transform(cat_data)

打印的结果为

[[1. 0. 0. 0. 0. 1. 0. 1.]

 [0. 1. 0. 1. 0. 0. 1. 0.]
 [0. 0. 1. 0. 1. 0. 0. 1.]]

 

如果是二分类(二进制)则,还可以用以下方法,定义一个函数

 

def cat_to_num(data):
    categories = unique(data)
    features = []
    for cat in categories:
        binary = (data == cat)
        features.append(binary.astype("int"))
    return features
import numpy as np
cat_data =np.array(['male', 'female', 'male', 'male'])

cat_to_num(cat_data)

打印出

[array([0, 1, 0, 0]), array([1, 0, 1, 1])]

    上文定义的这个转换函数可以是1维的,且可以是字符串

 

    当然在sklearn中也有实现的方法,(二进制 / 二分类编码)

from sklearn.preprocessing import Binarizer

Binarizer如果直接调用的话必须是二维矩阵,数值类型,需要设置threshold阈值

import numpy as np
cat_data =np.array([[1,2],
                   [3,4]])
binarizer =Binarizer(threshold=2.1) 
binarizer.transform(cat_data)

 

下面是pandas的dummy variable

import pandas as pd

data_dummy=pd.get_dummies(data)

  • 10
    点赞
  • 55
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值