one-hot编码

one-hot编码

什么是one-hot编码

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

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

运行结果

one-hot编码

one-hot编码

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)

运行结果

one-hot编码

Original: https://blog.csdn.net/qq_44795788/article/details/126451564
Author: 躺平yyds
Title: one-hot编码

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/727857/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球