質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
Jupyter

Jupyter (旧IPython notebook)は、Notebook形式でドキュメント作成し、プログラムの記述・実行、その実行結果を記録するツールです。メモの作成や保存、共有、確認などもブラウザ上で行うことができます。

Python 3.x

Python 3はPythonプログラミング言語の最新バージョンであり、2008年12月3日にリリースされました。

Q&A

解決済

1回答

7710閲覧

TfidfVectorizerの結果をTF-IDF値でソートして単語と対応させて表示させたい

wandmaker

総合スコア8

Jupyter

Jupyter (旧IPython notebook)は、Notebook形式でドキュメント作成し、プログラムの記述・実行、その実行結果を記録するツールです。メモの作成や保存、共有、確認などもブラウザ上で行うことができます。

Python 3.x

Python 3はPythonプログラミング言語の最新バージョンであり、2008年12月3日にリリースされました。

0グッド

0クリップ

投稿2018/07/08 05:40

ある1つの文章をjanomeで分かち書きし、
sklearnのTfidVectorizerにかけて、
TF-IDFを得ています。

ベクトルとして結果が得られていることは下記ページからも分かるのですが、
http://ailaby.com/tfidf/
TF-IDFでソートし、対応する希少性の高い単語を表示させたいです。

初心者のため見当違いなことを言っているかもしれません。
その際はご指摘いただけると幸いです。

vectorizer = TfidfVectorizer(use_idf=True, norm=None,
token_pattern=u'(?u)\b\w+\b')

vectorizer.vocabulary_.items
のitemsや
tfidf = vectorizer.fit_transform(wakatilist)
pd.DataFrame(tfidf.toarray()).index
のindexを使って組んでいけばいいような気がするのですが...。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

python

1tfidf = vectorizer.fit_transform(wakatilist).toarray() 2feature_names = np.array(vectorizer.get_feature_names()) 3index = tfidf.argsort(axis=1)[:,::-1] 4n = 10 # いくつほしいか 5feature_words = [feature_names[doc[:n]] for doc in index]

各文書ごとに特徴語を取り出したければ、こんな感じでいけると思います。

追記

せっかくなので簡単なサンプルを。

python

1import numpy as np 2from sklearn.feature_extraction.text import TfidfVectorizer 3from sklearn.datasets import fetch_20newsgroups 4 5news20 = fetch_20newsgroups() 6vectorizer = TfidfVectorizer(min_df=0.03) 7 8tfidf = vectorizer.fit_transform(news20.data[:1000]).toarray() 9feature_names = np.array(vectorizer.get_feature_names()) 10index = tfidf.argsort(axis=1)[:,::-1] 11feature_words = [feature_names[doc] for doc in index] 12 13n = 5 # top何単語取るか 14m = 10 # 何記事サンプルとして抽出するか 15targets = np.array(news20.target_names)[news20.target[:m]] 16 17for fwords, target in zip(feature_words, targets): 18 print(target) 19 print(fwords[:n]) 20 21""" => 22rec.autos 23['car' 'was' 'this' 'the' 'where'] 24comp.sys.mac.hardware 25['washington' 'add' 'guy' 'speed' 'call'] 26comp.sys.mac.hardware 27['the' 'display' 'anybody' 'heard' 'disk'] 28comp.graphics 29['division' 'chip' 'systems' 'computer' 'four'] 30sci.space 31['error' 'known' 'tom' 'memory' 'the'] 32talk.politics.guns 33['of' 'the' 'com' 'to' 'says'] 34sci.med 35['thanks' 'couldn' 'instead' 'file' 'everyone'] 36comp.sys.ibm.pc.hardware 37['chip' 'is' 'fast' 'ibm' 'bit'] 38comp.os.ms-windows.misc 39['win' 'help' 'please' 'appreciated' 'figure'] 40comp.sys.mac.hardware 41['the' 'file' 'lost' 've' 'it'] 42"""

それっぽく動いているようです。

投稿2018/07/08 06:03

編集2018/07/08 11:26
hayataka2049

総合スコア30933

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問