TF-IDF レコードの用語頻度逆文書頻度を表します。これは、シリーズまたはコーパス内の単語がテキストにどの程度関連しているかを計算するものとして定義できます。意味は、テキスト内で単語が出現する回数に比例して増加しますが、コーパス (データセット) 内の単語の頻度によって補われます。
用語:
- 用語頻度: 文書 d では、頻度は特定の単語 t のインスタンスの数を表します。したがって、単語がテキスト内に出現すると、その単語の関連性が高まり、合理的であることがわかります。用語の順序は重要ではないため、ベクトルを使用して用語モデルのバッグ内のテキストを記述することができます。論文内の特定の用語ごとに、用語の頻度を示す値を持つエントリがあります。
文書内で出現する用語の重みは、単純に用語の頻度に比例します。
tf(t,d) = count of t in d / number of words in d>
- 文書頻度: これは、コーパス コレクション全体で、TF に非常によく似たテキストの意味をテストします。唯一の違いは、文書 d では、TF が用語 t の頻度カウンタであるのに対し、df は文書セット N 内の用語 t の出現数であることです。つまり、その単語が存在する論文の数は DF です。
df(t) = occurrence of t in documents>
- 逆文書頻度: 主に、単語の関連性をテストします。検索の主な目的は、需要を満たす適切なレコードを見つけることです。 tf ではすべての項が同等に重要であるとみなされるため、論文内の項の重みを測定するために項頻度を使用できるだけではありません。まず、その用語を含むドキュメントの数を数えることによって、その用語 t のドキュメント頻度を見つけます。
df(t) = N(t) where df(t) = Document frequency of a term t N(t) = Number of documents containing the term t>
用語の頻度は、単一の文書内のみでの用語の出現数です。文書の頻度は、その用語が出現する個別の文書の数ですが、コーパス全体に依存します。次に、逆紙の周波数の定義を見てみましょう。単語の IDF は、テキストの頻度で区切られたコーパス内の文書の数です。
idf(t) = N/ df(t) = N/N(t)>
より一般的な単語はそれほど重要ではないと考えられていますが、要素 (最も明確な整数) は厳しすぎるように思えます。次に、紙の逆周波数の対数 (底が 2) を計算します。したがって、項 t の if は次のようになります。
idf(t) = log(N/ df(t))>
- 計算: Tf-idf は、シリーズまたはコーパス内のテキストに対する用語の重要性を判断するための最良の指標の 1 つです。 tf-idf は、単語頻度 (tf) と文書頻度 (tf) の逆数 (idf) に基づいて文書内の各単語に重みを割り当てる重み付けシステムです。重みのスコアが高い単語ほど、より重要であるとみなされます。
通常、tf-idf 重みは 2 つの項で構成されます。
- 正規化された用語頻度 (tf) 逆ドキュメント頻度 (idf)
tf-idf(t, d) = tf(t, d) * idf(t)>
Python では、tf-idf 値は次を使用して計算できます。 TfidfVectorizer() のメソッド スクラーン モジュール。
構文:
sklearn.feature_extraction.text.TfidfVectorizer(入力)
パラメーター:
入力 : これは渡されたパラメータドキュメントを指します。ファイル名、ファイル、またはコンテンツ自体の場合があります。
属性:
語彙 _ : 用語の辞書をキーとして返し、値を次のように返します。 特徴インデックス。 idf_ : 返されるのは、 パラメータとして渡されるドキュメントの逆ドキュメント周波数ベクトル。
戻り値:
fit_transform(): tf-idf 値とともに項の配列を返します。 get_feature_names(): 機能名のリストを返します。
段階的なアプローチ:
- モジュールをインポートします。
Python3
# import required module> from> sklearn.feature_extraction.text>import> TfidfVectorizer> |
>
>
- 文書から文字列を収集し、文書からの文字列のコレクションを含むコーパスを作成します d0、d1、 そして d2 。
Python3
# assign documents> d0>=> 'Geeks for geeks'> d1>=> 'Geeks'> d2>=> 'r2j'> # merge documents into a single corpus> string>=> [d0, d1, d2]> |
>
>
- から tf-idf 値を取得します fit_transform() 方法。
Python3
# create object> tfidf>=> TfidfVectorizer()> # get tf-df values> result>=> tfidf.fit_transform(string)> |
>
ジャワの睡眠
>
- コーパス内に存在する単語の idf 値を表示します。
Python3
# get idf values> print>(>'
idf values:'>)> for> ele1, ele2>in> zip>(tfidf.get_feature_names(), tfidf.idf_):> >print>(ele1,>':'>, ele2)> |
>
>
出力:

- インデックス付けとともに tf-idf 値を表示します。
Python3
# get indexing> print>(>'
Word indexes:'>)> print>(tfidf.vocabulary_)> # display tf-idf values> print>(>'
tf-idf value:'>)> print>(result)> # in matrix form> print>(>'
tf-idf values in matrix form:'>)> print>(result.toarray())> |
>
>
出力:
文字列形式のJava

の 結果 変数は、一意の単語と tf-if 値で構成されます。以下の画像を使用して詳しく説明します。

上の画像から、以下の表を生成できます。
| 書類 | 言葉 | 文書索引 | 単語索引 | tf-idf 値 |
|---|---|---|---|---|
| d0 | のために | 0 | 0 | 0.549 |
| d0 | オタク | 0 | 1 | 0.8355 |
| d1 | オタク | 1 | 1 | 1,000 |
| d2 | r2j | 2 | 2 | 1,000 |
以下は、コーパスから単語の tf-idf 値を計算する方法を示すいくつかの例です。
例 1: 上記のアプローチに基づいた完全なプログラムを以下に示します。
Python3
# import required module> from> sklearn.feature_extraction.text>import> TfidfVectorizer> # assign documents> d0>=> 'Geeks for geeks'> d1>=> 'Geeks'> d2>=> 'r2j'> # merge documents into a single corpus> string>=> [d0, d1, d2]> # create object> tfidf>=> TfidfVectorizer()> # get tf-df values> result>=> tfidf.fit_transform(string)> # get idf values> print>(>'
idf values:'>)> for> ele1, ele2>in> zip>(tfidf.get_feature_names(), tfidf.idf_):> >print>(ele1,>':'>, ele2)> # get indexing> print>(>'
Word indexes:'>)> print>(tfidf.vocabulary_)> # display tf-idf values> print>(>'
tf-idf value:'>)> print>(result)> # in matrix form> print>(>'
tf-idf values in matrix form:'>)> print>(result.toarray())> |
>
>
出力:

例 2: ここで、tf-idf 値は固有の値を持つコーパスから計算されます。
Python3
Javaのstr.substring
# import required module> from> sklearn.feature_extraction.text>import> TfidfVectorizer> # assign documents> d0>=> 'geek1'> d1>=> 'geek2'> d2>=> 'geek3'> d3>=> 'geek4'> # merge documents into a single corpus> string>=> [d0, d1, d2, d3]> # create object> tfidf>=> TfidfVectorizer()> # get tf-df values> result>=> tfidf.fit_transform(string)> # get indexing> print>(>'
Word indexes:'>)> print>(tfidf.vocabulary_)> # display tf-idf values> print>(>'
tf-idf values:'>)> print>(result)> |
>
>
出力:

例 3: このプログラムでは、類似した文書を含むコーパスから tf-idf 値が計算されます。
Python3
# import required module> from> sklearn.feature_extraction.text>import> TfidfVectorizer> # assign documents> d0>=> 'Geeks for geeks!'> d1>=> 'Geeks for geeks!'> # merge documents into a single corpus> string>=> [d0, d1]> # create object> tfidf>=> TfidfVectorizer()> # get tf-df values> result>=> tfidf.fit_transform(string)> # get indexing> print>(>'
Word indexes:'>)> print>(tfidf.vocabulary_)> # display tf-idf values> print>(>'
tf-idf values:'>)> print>(result)> |
>
>
出力:

例 4: 以下は、単一の単語の tf-idf 値を計算しようとするプログラムです。 オタク 複数の文書で複数回繰り返されます。
Python3
# import required module> from> sklearn.feature_extraction.text>import> TfidfVectorizer> # assign corpus> string>=> [>'Geeks geeks'>]>*>5> # create object> tfidf>=> TfidfVectorizer()> # get tf-df values> result>=> tfidf.fit_transform(string)> # get indexing> print>(>'
Word indexes:'>)> print>(tfidf.vocabulary_)> # display tf-idf values> print>(>'
tf-idf values:'>)> print>(result)> |
>
>
出力:
