【问题标题】:How do I add extra features to the array created by doc2Vec model.infer_vector?如何向 doc2Vec model.infer_vector 创建的数组添加额外功能?
【发布时间】:2021-10-28 09:03:21
【问题描述】:

我是 NLP 和 doc2Vec 的新手。

  1. 我使用 doc2vec 为每个文档生成一个数组。
  2. 我想将数组和额外特征(例如收入)用作另一个模型(如逻辑回归)的特征。如何组合 doc 数组和额外功能?
def get_vectors(model, tagged_docs):
    sents = tagged_docs.values
    targets, regressors = zip(*[(doc.tags[0], model.infer_vector(doc.words)) for doc in sents])
    return targets, regressors

model= Doc2Vec(dm=0, vector_size=300, negative=5, hs=0, min_count=2, sample = 0, workers=cores)
model.build_vocab(train_tagged.values)
model.train(train_tagged.values, total_examples=len(train_tagged.values), epochs=1)

y_train_doc , X_train_doc = get_vectors(model, train_tagged)

print(X_train_doc)
(array([ 0.168, -0.36 , -0.13], dtype=float32), 
 array([ 0.185,  0.17, 0.04], dtype=float32),....)

X_train_doc 是一个数组元组。那么对于每个数组,我是否将每个元素输入到 df 中的不同列中,如下所示?

doc | Income | doc_feature1 | doc_feature2|  doc_feature3 |
  1 | 10000  |  0.168       | -0.36       | -0.13         |
  2 |  500   |  0.185       | 0.17        |  0.04         |

【问题讨论】:

    标签: doc2vec


    【解决方案1】:

    这将取决于您使用的下游库/模型。一般来说,您想重新引入 Pandas Dataframes 的开销 - 下游模型更有可能使用原始 numpy 数组。

    如果使用scikit-learn 管道,FeatureUnion 类可能有用:

    https://scikit-learn.org/stable/modules/generated/sklearn.pipeline.FeatureUnion.html

    如果你有,说...

    • numpy 数组,包含 10000 行,每行 300 维,用于 10000 个元素的文档向量,然后
    • 10000 行,每行 5 个维度,每行用于 10000 个元素的其他特征,顺序相同

    ...然后您可能希望使用 numpy hstack 函数(多种选项之一)将它们水平连接成 10000 行,每行 305 维:

    https://numpy.org/doc/stable/reference/generated/numpy.hstack.html#numpy.hstack

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-12-28
      • 2012-01-19
      • 1970-01-01
      • 2018-01-22
      • 1970-01-01
      • 2017-03-10
      相关资源
      最近更新 更多