2017-05-31 6 views
1

私はSKLearnのTfidfVectorizerで分析したい25のリストの辞書として格納されている大きなコーパスを持っています。各リストには多くの文字列が含まれます。今、私は全体のコーパス全体の全体的な用語頻度(tf)と、25の文字列(idf)の各リスト内で最もユニークな用語の両方を気にしています。問題は、この種のオブジェクトをTfidfVectorizerに渡す方法が見つからないということです。 dictを渡すと、キーをベクトル化して値を渡すと、AttributeError: 'list' object has no attribute 'lower'が返されます(文字列が必要です)。リストの辞書にTfidfVectorizerを使用

ありがとうございます。

更新:今、地域のdictを使用し、私の前処理工程、を含む、IDペアbuckets

for area in buckets: 
    area_docs = [] 
    for value in buckets[area]: 
     if 'filename_%s.txt' % value in os.listdir(directory): 
      fin = open(directory+'/filename_%s.txt' % value, 'r').read() 
      area_docs.append(fin) 
      buckets[area] = area_docs 



corpus = buckets.values() 
vectorizer = TfidfVectorizer(min_df=1, stop_words='english') 
X = vectorizer.fit_transform(corpus) 
idf = vectorizer.idf_ 
d = dict(zip(vectorizer.get_feature_names(), idf)) 
sorted_d = sorted(d.items(), key=operator.itemgetter(1)) 
sorted_d[:50] 
+1

'' TfidfVectorizer'は、 "元文書のコレクションをTF-IDF機能のマトリックスに変換する"ために使用されます。それは一連の文書を必要とする。あなたの辞書は何らかの形で処理されているように見えるので、 'TfidfVectorizer'が何をするのかははっきりしません。 –

+0

ありがとう@ juanpa.arrivillaga。そのリストの項目を反映するように編集された複数の単語の文字列(私の実際のケースの文書〜2000単語)です。リストは基本的にサブコーパスです。実際には、特定のサブコーパス(リスト)内で最も特徴的な単語を知りたい。 – 6Bacon

答えて

1

TfidfVectorizerと呼ばれるには、各文字列が文書である場合には、文字列のリストを望んでいます。 area_docs変数は既に文字列のリストなので、buckets.values()を呼び出すと、TfidfVectorizerの次元数が多すぎる文字列のリストが表示されます。あなたはそのリストを平坦化する必要があります。以下のコードはPython3で、1行だけが変更され、別の行が追加されています:

for area in buckets: 
    area_docs = [] 
    for value in buckets[area]: 
     if 'filename_%s.txt' % value in os.listdir(directory): 
      fin = open(directory+'/filename_%s.txt' % value, 'r').read() 
      area_docs.append(fin) 
      buckets[area] = area_docs 

corpus = list(buckets.values()) # Get your list of lists of strings 
corpus = sum(corpus, []) # Good trick for flattening 2D lists to 1D 
vectorizer = TfidfVectorizer(min_df=1, stop_words='english') 
X = vectorizer.fit_transform(corpus) 
idf = vectorizer.idf_ 
d = dict(zip(vectorizer.get_feature_names(), idf)) 
sorted_d = sorted(d.items(), key=operator.itemgetter(1)) 
sorted_d[:50] 

これはすべきです!

関連する問題