【问题标题】:How do I use sklearn CountVectorizer with both 'word' and 'char' analyzer? - python如何将 sklearn CountVectorizer 与“word”和“char”分析器一起使用? - Python
【发布时间】:2014-03-03 05:43:27
【问题描述】:

如何将 sklearn CountVectorizer 与 'word' 和 'char' 分析器一起使用? http://scikit-learn.org/stable/modules/generated/sklearn.feature_extraction.text.CountVectorizer.html

我可以通过 word 或 char 分别提取文本特征,但如何创建 charword_vectorizer?有没有办法组合矢量化器?还是使用多个分析仪?

>>> from sklearn.feature_extraction.text import CountVectorizer
>>> word_vectorizer = CountVectorizer(analyzer='word', ngram_range=(1, 2), min_df=1)
>>> char_vectorizer = CountVectorizer(analyzer='char', ngram_range=(1, 2), min_df=1)
>>> x = ['this is a foo bar', 'you are a foo bar black sheep']
>>> word_vectorizer.fit_transform(x)
<2x15 sparse matrix of type '<type 'numpy.int64'>'
    with 18 stored elements in Compressed Sparse Column format>
>>> char_vectorizer.fit_transform(x)
<2x47 sparse matrix of type '<type 'numpy.int64'>'
    with 64 stored elements in Compressed Sparse Column format>
>>> char_vectorizer.get_feature_names()
[u' ', u' a', u' b', u' f', u' i', u' s', u'a', u'a ', u'ac', u'ar', u'b', u'ba', u'bl', u'c', u'ck', u'e', u'e ', u'ee', u'ep', u'f', u'fo', u'h', u'he', u'hi', u'i', u'is', u'k', u'k ', u'l', u'la', u'o', u'o ', u'oo', u'ou', u'p', u'r', u'r ', u're', u's', u's ', u'sh', u't', u'th', u'u', u'u ', u'y', u'yo']
>>> word_vectorizer.get_feature_names()
[u'are', u'are foo', u'bar', u'bar black', u'black', u'black sheep', u'foo', u'foo bar', u'is', u'is foo', u'sheep', u'this', u'this is', u'you', u'you are']

【问题讨论】:

    标签: python machine-learning scikit-learn analyzer text-analysis


    【解决方案1】:

    您可以将可调用对象作为analyzer 参数传递以完全控制标记化,例如

    >>> from pprint import pprint
    >>> import re
    >>> x = ['this is a foo bar', 'you are a foo bar black sheep']
    >>> def words_and_char_bigrams(text):
    ...     words = re.findall(r'\w{3,}', text)
    ...     for w in words:
    ...         yield w
    ...         for i in range(len(w) - 2):
    ...             yield w[i:i+2]
    ...             
    >>> v = CountVectorizer(analyzer=words_and_char_bigrams)
    >>> pprint(v.fit(x).vocabulary_)
    {'ac': 0,
     'ar': 1,
     'are': 2,
     'ba': 3,
     'bar': 4,
     'bl': 5,
     'black': 6,
     'ee': 7,
     'fo': 8,
     'foo': 9,
     'he': 10,
     'hi': 11,
     'la': 12,
     'sh': 13,
     'sheep': 14,
     'th': 15,
     'this': 16,
     'yo': 17,
     'you': 18}
    

    【讨论】:

    • 请原谅这个菜鸟,r'\w{3,} 是什么意思?和[a-zA-Z0-9_]一样吗?
    • @alvas: \w[a-zA-Z0-9_]; {3,} 表示“三个或更多”,因此它会跳过短标记。您可能想尝试使用不同的 RE 来捕获令牌;这是我的基线之一。
    【解决方案2】:

    您可以将任意特征提取步骤与 FeatureUnion 估计器相结合:http://scikit-learn.org/dev/modules/pipeline.html#featureunion-combining-feature-extractors

    在这种情况下,这可能比 larsmans 解决方案效率低,但可能更易于使用。

    【讨论】:

      猜你喜欢
      • 2017-04-27
      • 2020-01-21
      • 2019-12-28
      • 2019-12-18
      • 2017-11-05
      • 2021-12-26
      • 2022-08-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多