【问题标题】:How to get words's id in the vocabulary of bag-of-words given the word?如何在给定单词的词袋词汇表中获取单词的id?
【发布时间】:2016-10-07 02:43:54
【问题描述】:

我在一堆消息上利用了词袋模型,如下所示:

    bow_transformer = CountVectorizer(analyzer=split_into_lemmas).fit(messages['message'])
    B4 = bow_transformer.transform([msg4])
    print B4
    print bow_transformer.get_feature_names()[6736]
    print bow_transformer.get_feature_names()[8013]

(0, 1158) 1
(0, 1899) 1
(0, 2897) 1
(0, 2927) 1
(0, 第4021章)1
(0, 6736) 2
(0, 7111) 1
(0, 7698) 1
(0, 8013) 2

我需要的是给像 "say" 这样的词提取其 id "6736" (反之亦然 bow_transformer.get_feature_names()[6736] 正在做的事情)?!

【问题讨论】:

  • 你不能只使用transform方法吗?即bow_transformer.transform(['say'])
  • 它给了我类似的东西:(0, 6736) 1 但我只需要 6736

标签: python-2.7 machine-learning scikit-learn


【解决方案1】:

您应该使用 vocabulary_ 属性:

>>> bow_transformer.vocabulary_.get('say')
6736

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-08-02
    • 1970-01-01
    • 1970-01-01
    • 2020-04-18
    • 2018-01-07
    • 2018-12-12
    • 1970-01-01
    相关资源
    最近更新 更多