如何将sklearn CountVectorizer与';单词';和';char';分析仪?-python

如何将sklearn CountVectorizer与';单词';和';char';分析仪?-python,python,machine-learning,scikit-learn,analyzer,text-analysis,Python,Machine Learning,Scikit Learn,Analyzer,Text Analysis,如何将sklearn CountVectorizer与“word”和“char”分析器一起使用? 我可以分别按word或char提取文本特征,但如何创建charword\u矢量器?有没有办法组合矢量器?或者使用多个分析仪 >>> from sklearn.feature_extraction.text import CountVectorizer >>> word_vectorizer = CountVectorizer(analyzer='word', n

如何将sklearn CountVectorizer与“word”和“char”分析器一起使用?

我可以分别按word或char提取文本特征,但如何创建
charword\u矢量器
?有没有办法组合矢量器?或者使用多个分析仪

>>> 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']
>>来自sklearn.feature\u extraction.text import countvectorier
>>>word\u矢量器=计数器矢量器(analyzer='word',ngram\u range=(1,2),min\u df=1)
>>>字符向量器=计数向量器(analyzer='char',ngram_range=(1,2),min_df=1)
>>>x=[“这是一家美食酒吧”,“你是一家美食酒吧的害群之马”]
>>>word_矢量器。拟合_变换(x)
>>>字符向量器.拟合变换(x)
>>>字符向量器。获取特征名称()
[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'c',u'e',u'ee',u'ep',u'f',u'h',u'he u'hi u'i',u's',u'k',u'l',u'la',u'o',u'o',u'o',u'r',u'r',u'r',u's',u's',u's',u's',u's',u't',u'
>>>word\u矢量器。获取\u功能\u名称()
[u'are',u'are foo',u'bar',u'bar black',u'black',u'black sheep',u'foo',u'is',u'is foo',u'sheep',u'this',u'this',u'you',u'you'you']

您可以将callable作为
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}

您可以将任意特征提取步骤与FeatureUnion估计器结合使用:


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

请原谅,什么是
r'\w{3,}
意思?它是否与
[a-zA-Z0-9.]
相同?@alvas:
\w
[a-zA-Z0-9.]
{3,}
表示“三个或更多”,因此它跳过短令牌。您可能想尝试使用不同的REs来捕获令牌;这是我的基准之一。