獨熱編碼 將單詞映射到數字向量

# 代碼 容易理解

import numpy as np

sence = "An apple a day keeps doctor away said the doctor this is a girl I love you"

print(len(sence.split()))


class Dictionary(object):
    def __init__(self):
        self.word2idx = {}  # 存放單詞 對應的索引
        self.idx2word = []  # 放 唯一的 單詞
        self.length = -1

    def add_word(self, word):
        if word not in self.idx2word:
            self.idx2word.append(word)
            self.word2idx[word] = self.length + 1  # 索引 從0開始
            self.length += 1
        return self.word2idx[word]  # 返回單詞 對應索引

    def __len__(self):
        return len(self.idx2word)  # 返回單詞的總長度

    def onehot_encoded(self, word):
        vec = np.zeros(self.length + 1)  # 返回 唯一單詞總數 對應的 長度 0
        vec[self.word2idx[word]] = 1  # 單詞所在長度 爲1
        return vec


die = Dictionary()
for tok in sence.split():  # 添加完所有的詞, 之後 才能輸出所有的獨熱編碼
    die.add_word(tok)
    # die.onehot_encoded(tok)


print(die.idx2word)
print(die.word2idx)
print(die.onehot_encoded(die.idx2word[0]))
print(die.onehot_encoded(die.idx2word[2]))
print(die.onehot_encoded("you"))

# 輸出結果

17
['An', 'apple', 'a', 'day', 'keeps', 'doctor', 'away', 'said', 'the', 'this', 'is', 'girl', 'I', 'love', 'you']
{'An': 0, 'apple': 1, 'a': 2, 'day': 3, 'keeps': 4, 'doctor': 5, 'away': 6, 'said': 7, 'the': 8, 'this': 9, 'is': 10, 'girl': 11, 'I': 12, 'love': 13, 'you': 14}
[1. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 1. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 1.]

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章