首页IT科技onehot编码作用(one-hot编码)

onehot编码作用(one-hot编码)

时间2025-06-20 10:21:52分类IT科技浏览7512
导读:one-hot编码...

one-hot编码

什么是one-hot编码

one-hot编码              ,又称独热编码              、一位有效编码              。其方法是使用N位状态寄存器来对N个状态进行编码                       ,每个状态都有它独立的寄存器位      ,并且在任意时候          ,其中只有一位有效                       。举个例子                        ,假设我们有四个样本(行)          ,每个样本有三个特征(列)      ,如下图:

我们拿feature2来说明:这里feature2有4种取值(状态)                       ,我们就用4个状态位来表示这个特征              ,one-hot编码就是保证每个样本中的单个特征只有1位处于状态1,其他的都是0      。

对于2种状态                       、3种状态      、甚至更多状态都可以这样表示   ,所以我们可以得到这些样本特征的新表示                      ,入下图:

one-ho编码将每个状态位都看成一个特征          。对于前两个样本我们可以得到它的特征向量分别为

Sample_1--->[0,1,1,0,0,0,1,0,0] Sample_2--->[1,0,0,1,0,0,0,1,0]

one-hot在提取文本特征上的应用

one hot在特征提取上属于词袋模型(bag of words)                        。关于如何使用one-hot抽取文本特征向量我们通过以下例子来说明          。假设我们的语料库中有三段话:

我爱中国

爸爸妈妈爱我

爸爸妈妈爱中国

我们首先对预料库分词                  ,并获取其中所有的词,然后对每个此进行编号:

1我;2爱;3爸爸;4妈妈;5中国

然后使用one hot对每段话提取特征向量:

因此我们得到了最终的特征向量为

我爱中国->(1,1,0,0,1)

爸爸妈妈爱我->(1,1,1,1,0)

爸爸妈妈爱中国->(0,1,1,1,1)

优缺点分析

优点:

一是解决了分类器不好处理离散数据的问题 二是在一定程度上也起到了扩充特征的作用(上面样本特征数从3扩展到了9)

缺点:

它是一个词袋模型                  ,不考虑词与词之间的顺序 它假设词与词相互独立(在大多数情况下                      ,词与词是相互影响的) 它得到的特征是离散稀疏的; 手动实现one-hot编码

import numpy as np samples = [他 毕业 于 哈佛大学, 他 就职 于 工科院计算机研究所] # 分完词之后一般要将词典索引做好   ,一般叫token_index token_index = {} for sample in samples: for word in sample.split(): if word not in token_index: token_index[word] = len(token_index)+1 print(len(token_index)) print(token_index) # 构造one—hot编码 results = np.zeros(shape=(len(samples), len(token_index)+1, max(token_index.values())+1)) for i, sample in enumerate(samples): # 索引 for j, word in list(enumerate(sample.split())): # 对list组进行链接 index = token_index.get(word) # 索引和word对应 print(i, j, index, word) results[i, j, index] = 1 print(results) # 改进的算法 results2 = np.zeros(shape=(len(samples),max(token_index.values())+1) ) for i, sample in enumerate(samples): for _, word in list(enumerate(sample.split())): index = token_index.get(word) results2[i, index] = 1 print(results2)

运行结果

Keras中one-hot编码的实现

Keras分词器Tokenizer的办法介绍

Tokenizer是一个用于向量化文本              ,或将文本转换为序列(即单词在字典中的下标形成的列表                       ,从1算起)的类      。Tokenizer实际上只是生成了一个字典      ,并且统计了词频等信息          ,并没有把文本转成须要的向量示意                       。 from keras.preprocessing.text import Tokenizer引入模块 tokenizer = Tokenizer()

生成词典tokenizer.fit_on_texts()

string = [他 毕业 于 哈佛大学, 他 就职 于 工科院计算机研究所] # 构建单词索引 tokenizer = Tokenizer() tokenizer.fit_on_texts(samples) print(tokenizer.word_index)

将句子序列转换成token矩阵tokenizer.texts_to_matrix()

tokenizer.texts_to_matrix(samples) #如果string中的word出现在了字典中,那么在矩阵中出现的位置处标1 tokenizer.texts_to_matrix(string,mode=count) #如果string中的word出现在了字典中,那么在矩阵中出现的位置处标记这个word出现的次数

句子转换成单词索引序列tokenizer.texts_to_sequences

sequences = tokenizer.texts_to_sequences(samples) print(sequences)

分词器被训练的文档(文本或者序列)数量tok.document_count

依照数量由大到小Order排列的token及其数量tok.word_counts

完整代码:

from keras.preprocessing.text import Tokenizer samples = [他 毕业 于 哈佛大学, 他 就职 于 工科院计算机研究所] # 构建单词索引 tokenizer = Tokenizer() tokenizer.fit_on_texts(samples) word_index = tokenizer.word_index print(word_index) print(len(word_index)) sequences = tokenizer.texts_to_sequences(samples) print(sequences) one_hot_results = tokenizer.texts_to_matrix(samples) print(one_hot_results)

运行结果

创心域SEO版权声明:以上内容作者已申请原创保护,未经允许不得转载,侵权必究!授权事宜、对本内容有异议或投诉,敬请联系网站管理员,我们将尽快回复您,谢谢合作!

展开全文READ MORE
javaweb入门书籍推荐(JavaWeb简易复习手册)