2017-05-12 14 views
0

나는 CountVectorizer 가지고있는 N- 그램으로부터 당기고 문서가이 문자열이 오류가 발생두 단어가있을 때 CountVectorizer에서 bigram에 대해 "빈 단어 오류"가 표시되는 이유는 무엇입니까? 그 벡터 기 구현</p> <pre><code>word_vectorizer = CountVectorizer(stop_words=None, ngram_range=(2,2), analyzer='word') </code></pre> <p>:

Traceback (most recent call last): 

    File "<ipython-input-63-d261e44b8cce>", line 1, in <module> 
    runfile('C:/Users/taca/Documents/Work/Python/Text Analytics/owccomments.py', wdir='C:/Users/taca/Documents/Work/Python/Text Analytics') 

    File "C:\Users\taca\AppData\Local\Continuum\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 866, in runfile 
    execfile(filename, namespace) 

    File "C:\Users\taca\AppData\Local\Continuum\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 102, in execfile 
    exec(compile(f.read(), filename, 'exec'), namespace) 

    File "C:/Users/taca/Documents/Work/Python/Text Analytics/owccomments.py", line 38, in <module> 
    X = word_vectorizer.fit_transform(group['cleanComments']) 

    File "C:\Users\taca\AppData\Local\Continuum\Anaconda3\lib\site-packages\sklearn\feature_extraction\text.py", line 839, in fit_transform 
    self.fixed_vocabulary_) 

    File "C:\Users\taca\AppData\Local\Continuum\Anaconda3\lib\site-packages\sklearn\feature_extraction\text.py", line 781, in _count_vocab 
    raise ValueError("empty vocabulary; perhaps the documents only" 

ValueError: empty vocabulary; perhaps the documents only contain stop words 

입니다 :

X = word_vectorizer.fit_transform(group['cleanComments']) 

이 오류가 발생합니다 : "중복 q". 문서가 ''일 때마다 발생합니다.

왜 CountVectorizer가 q (또는 그와 관련하여 하나의 문자)를 유효한 단어로 선택하지 않는 이유는 무엇입니까? 이 오류가 CountVectorizer에 발생할 수있는 이유를 나열하는 포괄적 인 장소가 있습니까?

EDIT : 나는 오류 자체를 파고 들었고 어휘와 관련이있는 것 같습니다. 나는 표준 어휘가 단문을 단어로 받아들이지 않는다고 가정하고 있지만, 그 문제를 해결하는 방법을 모르겠습니다.

+0

'group [ 'cleanComments']'에 행의 예를 보여 주시겠습니까? 그것은 어떻게 생겼습니까? – titipata

답변

1

_count_vocab() 함수가 CountVectorizer 클래스의 메서드 인이 오류를 발생시킵니다. 이 클래스에는 단어로 간주되는 것을 정의하는 token_pattern이 있습니다. token_pattern 인수 노트에 대한 문서 : 우리가 __init__에 기본 인수에 명시 적으로 볼 수

The default regexp select tokens of 2 or more alphanumeric characters

그리고 :

token_pattern=r"(?u)\b\w\w+\b" 

을 사용하면 단일 문자 단어를 허용하려면, 단지 첫 \w를 제거 이 패턴을 명시하고 CountVectorizertoken_pattern으로 명시 적으로 설정하면

CountVectorizer(token_pattern=r"(?u)\b\w+\b", 
       stop_words=None, ngram_range=(2,2), analyzer='word') 
+0

완벽 해 - 고맙습니다. 저는 일단 Sklearn과 Python이 눈에 보이는 것보다 훨씬 더 많은 것을 발견했습니다. 일단 잡초에 빠지면 말이죠. –