2014-12-15 8 views
-1

Canopy Express에서 GENSIM 토픽 모델링 예제를 실행하고 Sum() 행에서 다음 오류를 얻으려고합니다.Canopy Express의 GENSIM 오류

from gensim import corpora, models, similarities 
from itertools import chain 

""" DEMO """ 
documents = ["Human machine interface for lab abc computer applications", 
     "A survey of user opinion of computer system response time", 
     "The EPS user interface management system", 
     "System and human system engineering testing of EPS", 
     "Relation of user perceived response time to error measurement", 
     "The generation of random binary unordered trees", 
     "The intersection graph of paths in trees", 
     "Graph minors IV Widths of trees and well quasi ordering", 
     "Graph minors A survey"] 

# remove common words and tokenize 
stoplist = set('for a of the and to in'.split()) 
texts = [[word for word in document.lower().split() if word not in stoplist] 
    for document in documents] 

# remove words that appear only once 
all_tokens = sum(texts, []) 
tokens_once = set(word for word in set(all_tokens) if all_tokens.count(word) == 1) 
texts = [[word for word in text if word not in tokens_once] for text in texts] 

오류는 TypeError입니다. 정수가 필요합니다. 일반 파이썬에서는 괜찮아 보이지만 Canopy에는 문제가 있습니다. Canopy가 sum 문을 처리하는 방법 인 것 같습니다. 그러나 나는 그 문제를 해결하는 방법을 모르겠습니다. 어떤 아이디어라도 파이썬과 텍스트 분석으로 시작하고 있습니다.

+0

감사합니다. 이것은 내가 초보자이며 패키지를 배우려고 노력할 때 많은 도움이됩니다. 나는 합계 성명서를 가리키는 유사한 질문을 보았지만 그것을 해결하는 방법을 상세히 설명하지는 않았다. 귀하의 대답은 그것을 제공합니다. 다시 감사합니다. – user3890455

답변

0

캐노피 파이썬 그 자체 "일반 파이썬 2.7"입니다. 그러나 Canopy GUI의 Python 창은 IPython QTConsole로 기능성 계층을 추가합니다. 주로 더 나은 기능을 제공하지만 드문 경우는 더 좋지 않습니다. 기본적으로 Pylab 모드에서 시작되며 초보자에게 혼동을 줄 수 있습니다 (https://support.enthought.com/entries/25750190-Modules-are-already-available-in-Canopy-s-Python-PyLab-prompt-but-not-in-a-script 참조).

정확도에 대해 설명하지는 않지만 설명하는 증상에서 IPython 프롬프트에서 하나씩 명령을 실행하는 것처럼 들리 겠지만 복사 붙여 넣기 또는 텍스트 편집기에서 명령을 선택하고 "선택 항목 실행"을 수행하십시오. IPython 프롬프트에서 Pylab은 암시적인 from numpy import *을 수행하기 때문에 sum 함수는보고 한 오류 메시지를 설명하는 내장 파이썬 합계가 아닌 numpy의 합계를 참조합니다. (많은 점 만점)

세 가지 다른 솔루션 : 예상대로

1) 당신은 단순히 오히려 "실행 선택"또는 복사/붙여 넣기 명령보다 (스크립트를 실행하는 경우),이 행동해야한다. 이것은 가장 강력하고 유연한 솔루션입니다.

2) Canopy 환경 설정에서 Pylab 모드를 비활성화하십시오. 어느 방향 으로든 명령을 실행할 수 있습니다.

3) (훌륭한 해결책은 아니지만 유익한 방법입니다.) IPython 프롬프트에서 del sum을 수행하십시오. 이렇게하면 IPython 네임 스페이스에서 numpy sum이 삭제되고 원래 내장 된 sum이 찾아지고 코드가 어느쪽으로 든 실행될 수 있습니다.