关于onehot的一些使用

下面用两种方法进行onehot处理,一种是keras的函数,一种是sklearn的函数,注意使用sklearn的函数时,如果处理对象是一维的list时需要先转换成array再用reshape(-1,1)处理,还有函数需要有参数sparse = False,函数是preprocessing.OneHotEncoder(sparse = False)。根据下面例子的结果体会一下onehot的作用,其实data1,data2,data3,data4,data5进行onehot都是一样的道理。

以下为测试程序,可以直接复制到编译器中运行:
from keras.utils import to_categorical
data = [1,3,2,0,3,2,2,1,0,1]
print("data:\n",data)
print("type(data):",type(data))
encoded = to_categorical(data)
print("type(encoded):",type(encoded))
print("encoded:\n",encoded)

from sklearn import preprocessing
import numpy as np
one_hot = preprocessing.OneHotEncoder(sparse = False)
data1 =np.array([1,3,2]).reshape(-1,1)
print("data1:\n",data1)
print("type(data1):",type(data1))
encode_data1 = one_hot.fit_transform(data1)
print("type(encode_data1):",type(encode_data1))
print("encode_data1:\n",encode_data1)
data2 =np.array([1,0,2,0]).reshape(-1,1)
print("data2:\n",data2)
print("type(data2):",type(data2))
encode_data2 = one_hot.fit_transform(data2)
print("type(encode_data2):",type(encode_data2))
print("encode_data2:\n",encode_data2)

data3 =([1,0,2,0],[1,5,6,7])
print("data3:\n",data3)
print("type(data3):",type(data3))
encode_data3 = one_hot.fit_transform(data3)
print("type(encode_data3):",type(encode_data3))
print("encode_data3:\n",encode_data3)
data4 =([1,0,2,0],[4,5,6,7])
print("data4:\n",data4)
print("type(data4):",type(data4))
encode_data4 = one_hot.fit_transform(data4)
print("type(encode_data4):",type(encode_data4))
print("encode_data4:\n",encode_data4)
data5=([1,3,2],[2,1,1],[4,2,2])
print("data5:\n",data5)
print("type(data5):",type(data5))
encode_data5 = one_hot.fit_transform(data5)
print("type(encode_data5):",type(encode_data5))
print("encode_data5:\n",encode_data5)

输出结果为:

data:
 [1, 3, 2, 0, 3, 2, 2, 1, 0, 1]
type(data): <class 'list'>
type(encoded): <class 'numpy.ndarray'>
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.]]
data1:
 [[1]
 [3]
 [2]]
type(data1): <class 'numpy.ndarray'>
type(encode_data1): <class 'numpy.ndarray'>
encode_data1:
 [[1. 0. 0.]
 [0. 0. 1.]
 [0. 1. 0.]]
data2:
 [[1]
 [0]
 [2]
 [0]]
type(data2): <class 'numpy.ndarray'>
type(encode_data2): <class 'numpy.ndarray'>
encode_data2:
 [[0. 1. 0.]
 [1. 0. 0.]
 [0. 0. 1.]
 [1. 0. 0.]]
data3:
 ([1, 0, 2, 0], [1, 5, 6, 7])
type(data3): <class 'tuple'>
type(encode_data3): <class 'numpy.ndarray'>
encode_data3:
 [[1. 1. 0. 1. 0. 1. 0.]
 [1. 0. 1. 0. 1. 0. 1.]]
data4:
 ([1, 0, 2, 0], [4, 5, 6, 7])
type(data4): <class 'tuple'>
type(encode_data4): <class 'numpy.ndarray'>
encode_data4:
 [[1. 0. 1. 0. 1. 0. 1. 0.]
 [0. 1. 0. 1. 0. 1. 0. 1.]]
data5:
 ([1, 3, 2], [2, 1, 1], [4, 2, 2])
type(data5): <class 'tuple'>
type(encode_data5): <class 'numpy.ndarray'>
encode_data5:
 [[1. 0. 0. 0. 0. 1. 0. 1.]
 [0. 1. 0. 1. 0. 0. 1. 0.]
 [0. 0. 1. 0. 1. 0. 0. 1.]]

参考博客:https://blog.csdn.net/fitzgerald0/article/details/79200902 感谢!

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值