2017-05-12 6 views
0

매우 큰 csv 파일을 분석 중이며 scikit을 사용하여 tf-idf 정보를 추출하려고합니다. 불행히도,이 typeError를 던지기 때문에 결코 데이터 처리가 끝나지 않습니다. 이 오류를 제거하기 위해 프로그래밍 방식으로 CSV 파일을 변경하는 방법이 있습니까? 여기 내 코드는 다음과 같습니다.python TfidfVectorizer는 typeError : csv 파일의 예상 문자열 또는 바이트와 같은 객체를 제공합니다.

df = pd.read_csv("C:/Users/aidan/Downloads/papers/papers.csv", sep = None) 
df = df[pd.notnull(df)] 

    n_features = 1000 
    n_topics = 8 
    n_top_words = 10 
tfidf_vectorizer = TfidfVectorizer(max_df=0.95, min_df=2,max_features=n_features,stop_words='english', lowercase = False) 

tfidf = tfidf_vectorizer.fit_transform(df['paper_text']) 

마지막 줄부터 오류가 발생합니다. 미리 감사드립니다.

Traceback (most recent call last): 
    File "C:\Users\aidan\NIPS Analysis 2.0.py", line 35, in <module> 
    tfidf = tfidf_vectorizer.fit_transform(df['paper_text']) 
    File "c:\python\python36\lib\site-packages\sklearn\feature_extraction\text.py", line 1352, in fit_transform 
    X = super(TfidfVectorizer, self).fit_transform(raw_documents) 
    File "c:\python\python36\lib\site-packages\sklearn\feature_extraction\text.py", line 839, in fit_transform 
    self.fixed_vocabulary_) 
    File "c:\python\python36\lib\site-packages\sklearn\feature_extraction\text.py", line 762, in _count_vocab 
    for feature in analyze(doc): 
    File "c:\python\python36\lib\site-packages\sklearn\feature_extraction\text.py", line 241, in <lambda> 
    tokenize(preprocess(self.decode(doc))), stop_words) 
    File "c:\python\python36\lib\site-packages\sklearn\feature_extraction\text.py", line 216, in <lambda> 
    return lambda doc: token_pattern.findall(doc) 
TypeError: expected string or bytes-like object 

답변

1

df.dtypes을 확인하셨습니까? 출력은 무엇입니까?

.read_csv() 호출의 인수로 dtype=str을 추가하려고 할 수 있습니다.

+0

결과에 dtype : object가 아래쪽에 표시됩니다. 그 위에는 "대상"이라고도하는 단어와 문자의 테이블이 있습니다. 좋아요. –

+0

예! 그것은 작동합니다! 고마워요! –

+0

다행, 다행입니다! – neox