【问题标题】:Extracting one-hot vector from text从文本中提取 one-hot 向量
【发布时间】:2016-04-21 08:39:49
【问题描述】:

pandasnumpy 中,我可以执行以下操作来获得one-hot 向量:

>>> import numpy as np
>>> import pandas as pd
>>> x = [0,2,1,4,3]
>>> pd.get_dummies(x).values
array([[ 1.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  1.,  0.,  0.],
       [ 0.,  1.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  1.],
       [ 0.,  0.,  0.,  1.,  0.]])

>>> np.eye(len(set(x)))[x]
array([[ 1.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  1.,  0.,  0.],
       [ 0.,  1.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  1.],
       [ 0.,  0.,  0.,  1.,  0.]])

从文字,gensim,我可以做到:

>>> from gensim.corpora import Dictionary
>>> sent1 = 'this is a foo bar sentence .'.split()
>>> sent2 = 'this is another foo bar sentence .'.split()
>>> texts = [sent1, sent2]
>>> vocab = Dictionary(texts)
>>> [[vocab.token2id[word] for word in sent] for sent in texts]
[[3, 4, 0, 6, 1, 2, 5], [3, 4, 7, 6, 1, 2, 5]]

然后我必须执行相同的 pd.get_dummiesnp.eyes 来获取 one-hot 向量,但我收到一个错误,即我的 one-hot 向量中缺少一个维度我有 8 个唯一词,但一个-hot向量长度只有7:

>>> [pd.get_dummies(sent).values for sent in texts_idx]
[array([[ 0.,  0.,  0.,  1.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  1.,  0.,  0.],
       [ 1.,  0.,  0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.,  0.,  1.],
       [ 0.,  1.,  0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  1.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.,  1.,  0.]]), array([[ 0.,  0.,  1.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  1.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.,  0.,  1.],
       [ 0.,  0.,  0.,  0.,  0.,  1.,  0.],
       [ 1.,  0.,  0.,  0.,  0.,  0.,  0.],
       [ 0.,  1.,  0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  1.,  0.,  0.]])]

它似乎是在遍历每个句子时单独做一个热向量,而不是使用全局词汇表。

使用np.eye,我确实得到了正确的向量:

>>> [np.eye(len(vocab))[sent] for sent in texts_idx]
[array([[ 0.,  0.,  0.,  1.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  1.,  0.,  0.,  0.],
       [ 1.,  0.,  0.,  0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.,  0.,  1.,  0.],
       [ 0.,  1.,  0.,  0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  1.,  0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.,  1.,  0.,  0.]]), array([[ 0.,  0.,  0.,  1.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  1.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  1.],
       [ 0.,  0.,  0.,  0.,  0.,  0.,  1.,  0.],
       [ 0.,  1.,  0.,  0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  1.,  0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.,  1.,  0.,  0.]])]

另外,目前,我必须做几件事,从使用 gensim.corpora.Dictionary 到将单词转换为它们的 id,然后获取 one-hot 向量。

还有其他方法可以从文本中实现相同的 one-hot 矢量吗?

【问题讨论】:

    标签: python numpy pandas vector nlp


    【解决方案1】:

    有多种包可以在一个函数中完成所有步骤,例如http://scikit-learn.org/stable/modules/generated/sklearn.preprocessing.OneHotEncoder.html

    或者,如果您已经有每个句子的词汇和文本索引,您可以通过预分配和使用智能索引来创建一次性编码。下面的 text_idx 是一个整数列表,而 vocab 是一个将整数索引与单词相关联的列表。

    import numpy as np
    vocab_size = len(vocab)
    text_length = len(text_idx)
    one_hot = np.zeros(([vocab_size, text_length])
    one_hot[text_idx, np.arange(text_length)] = 1
    

    【讨论】:

    • 您能详细说明一下吗?这里的 d 是什么?
    • 对不起,回头看我认为有一个错字。和 d 和 sent 是一回事,而 text_idx 是与 one_hot 编码对应的整数列表。我已经更新了我的答案。
    【解决方案2】:

    要创建 one_hot_vector,您需要从文本中创建独特的词汇

    vocab=set(vocab)
    label_encoder = LabelEncoder()
    integer_encoded = label_encoder.fit_transform(vocab)
    one_hot_encoder = OneHotEncoder(sparse=False)
    doc = "dog"
    index=vocab.index(doc)
    integer_encoded = integer_encoded.reshape(len(integer_encoded), 1)
    one_hot_encoder=one_hot_encoder.fit_transform(integer_encoded)[index]
    

    【讨论】:

      【解决方案3】:

      第 7 个值是句子中的“.”(点),用“”(空格)分隔,split() 将其视为一个单词

      【讨论】:

        猜你喜欢
        • 2019-08-28
        • 2018-04-15
        • 2017-09-19
        • 1970-01-01
        • 2017-06-07
        • 2020-10-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多