2017-01-11 16 views
0

sklearnのTfIdfVectorizerを使用しようとしています。私の入力はおそらくTfIdfVectorizerのニーズに合っていないため、私は問題を抱えています。私はロードしてリストに追加したJSONをたくさん持っていますが、私はこれをTfIdfVectorizerの使用のためのコーパスにしたいと考えています。TfidfVectorizer使用のためのデータ準備(scikitlearn)

コード:

import json 
import pandas 
from sklearn.feature_extraction.text import TfidfVectorizer 

train=pandas.read_csv("train.tsv", sep='\t') 
documents=[] 

for i,row in train.iterrows(): 
     data = json.loads(row['boilerplate'].lower()) 
     documents.append(data['body']) 
vectorizer=TfidfVectorizer(min_df=1) 
X = vectorizer.fit_transform(documents) 
idf = vectorizer.idf_ 
print dict(zip(vectorizer.get_feature_names(), idf)) 

私は、次のエラーを取得しています:

Traceback (most recent call last): 

    File "<ipython-input-56-94a6b95b0745>", line 1, in <module> 
    runfile('C:/Users/Guinea Pig/Downloads/try.py', wdir='C:/Users/Guinea Pig/Downloads') 

    File "D:\Anaconda\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 585, in runfile 
    execfile(filename, namespace) 

    File "C:/Users/Guinea Pig/Downloads/try.py", line 19, in <module> 
    X = vectorizer.fit_transform(documents) 

    File "D:\Anaconda\lib\site-packages\sklearn\feature_extraction\text.py", line 1219, in fit_transform 
    X = super(TfidfVectorizer, self).fit_transform(raw_documents) 

    File "D:\Anaconda\lib\site-packages\sklearn\feature_extraction\text.py", line 780, in fit_transform 
    vocabulary, X = self._count_vocab(raw_documents, self.fixed_vocabulary) 

    File "D:\Anaconda\lib\site-packages\sklearn\feature_extraction\text.py", line 715, in _count_vocab 
    for feature in analyze(doc): 

    File "D:\Anaconda\lib\site-packages\sklearn\feature_extraction\text.py", line 229, in <lambda> 
    tokenize(preprocess(self.decode(doc))), stop_words) 

    File "D:\Anaconda\lib\site-packages\sklearn\feature_extraction\text.py", line 195, in <lambda> 
    return lambda x: strip_accents(x.lower()) 

AttributeError: 'NoneType' object has no attribute 'lower' 

私は、文書の配列がUnicodeオブジェクトで構成されていることを取得し、ない文字列オブジェクトが、私がすることはできませんしていますこの問題を解決するようです。アリのアイデア?

答えて

0

は最終的に私が使用:

str_docs=[] 
for item in documents: 
    str_docs.append(documents[i].encode('utf-8')) 

を加えたよう

関連する問題