tfidf
単語頻度-逆文書頻度 (tf-idf) 行列
説明
例
bag-of-words モデルから単語頻度-逆文書頻度 (tf-idf) 行列を作成します。
サンプル データを読み込みます。ファイル sonnetsPreprocessed.txt には、シェイクスピアのソネット集の前処理されたバージョンが格納されています。ファイルには、1 行に 1 つのソネットが含まれ、単語がスペースで区切られています。sonnetsPreprocessed.txt からテキストを抽出し、テキストを改行文字で文書に分割した後、文書をトークン化します。
filename = "sonnetsPreprocessed.txt";
str = extractFileText(filename);
textData = split(str,newline);
documents = tokenizedDocument(textData);bagOfWords を使用して bag-of-words モデルを作成します。
bag = bagOfWords(documents)
bag =
bagOfWords with properties:
NumWords: 3092
Counts: [154×3092 double]
Vocabulary: ["fairest" "creatures" "desire" "increase" "thereby" "beautys" "rose" "might" "never" "die" "riper" "time" "decease" "tender" "heir" "bear" "memory" "thou" … ] (1×3092 string)
NumDocuments: 154
tf-idf 行列を作成します。最初の 10 個の行と列を表示します。
M = tfidf(bag); full(M(1:10,1:10))
ans = 10×10
3.6507 4.3438 2.7344 3.6507 4.3438 2.2644 3.2452 3.8918 2.4720 2.5520
0 0 0 0 0 4.5287 0 0 0 0
0 0 0 0 0 0 0 0 0 2.5520
0 0 0 0 0 2.2644 0 0 0 0
0 0 0 0 0 2.2644 0 0 0 0
0 0 0 0 0 2.2644 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 2.2644 0 0 0 2.5520
0 0 2.7344 0 0 0 0 0 0 0
bag-of-words モデルと新規文書の配列から Term Frequency-Inverse Document Frequency (tf-idf) 行列を作成します。
サンプル データを読み込みます。ファイル sonnetsPreprocessed.txt には、シェイクスピアのソネット集の前処理されたバージョンが格納されています。ファイルには、1 行に 1 つのソネットが含まれ、単語がスペースで区切られています。sonnetsPreprocessed.txt からテキストを抽出し、テキストを改行文字で文書に分割した後、文書をトークン化します。
filename = "sonnetsPreprocessed.txt";
str = extractFileText(filename);
textData = split(str,newline);
documents = tokenizedDocument(textData);文書から bag-of-words モデルを作成します。
bag = bagOfWords(documents)
bag =
bagOfWords with properties:
NumWords: 3092
Counts: [154×3092 double]
Vocabulary: ["fairest" "creatures" "desire" "increase" "thereby" "beautys" "rose" "might" "never" "die" "riper" "time" "decease" "tender" "heir" "bear" "memory" "thou" … ] (1×3092 string)
NumDocuments: 154
bag から計算された逆文書頻度 (IDF) 係数を使用して、新しい文書の配列に対する tf-idf 行列を作成します。
newDocuments = tokenizedDocument([
"what's in a name? a rose by any other name would smell as sweet."
"if music be the food of love, play on."]);
M = tfidf(bag,newDocuments)M = 2×3092 sparse double matrix (8 nonzeros)
(1,7) 3.2452
(1,36) 1.2303
(2,197) 3.4275
(2,313) 3.6507
(2,387) 0.6061
(1,1205) 4.7958
(1,1835) 3.6507
(2,1917) 5.0370
サンプル データを読み込みます。ファイル sonnetsPreprocessed.txt には、シェイクスピアのソネット集の前処理されたバージョンが格納されています。ファイルには、1 行に 1 つのソネットが含まれ、単語がスペースで区切られています。sonnetsPreprocessed.txt からテキストを抽出し、テキストを改行文字で文書に分割した後、文書をトークン化します。
filename = "sonnetsPreprocessed.txt";
str = extractFileText(filename);
textData = split(str,newline);
documents = tokenizedDocument(textData);bagOfWords を使用して bag-of-words モデルを作成します。
bag = bagOfWords(documents)
bag =
bagOfWords with properties:
NumWords: 3092
Counts: [154×3092 double]
Vocabulary: ["fairest" "creatures" "desire" "increase" "thereby" "beautys" "rose" "might" "never" "die" "riper" "time" "decease" "tender" "heir" "bear" "memory" "thou" … ] (1×3092 string)
NumDocuments: 154
tf-idf 行列を作成します。最初の 10 個の行と列を表示します。
M = tfidf(bag); full(M(1:10,1:10))
ans = 10×10
3.6507 4.3438 2.7344 3.6507 4.3438 2.2644 3.2452 3.8918 2.4720 2.5520
0 0 0 0 0 4.5287 0 0 0 0
0 0 0 0 0 0 0 0 0 2.5520
0 0 0 0 0 2.2644 0 0 0 0
0 0 0 0 0 2.2644 0 0 0 0
0 0 0 0 0 2.2644 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 2.2644 0 0 0 2.5520
0 0 2.7344 0 0 0 0 0 0 0
TF と IDF の重み式を指定することにより、TF 係数と IDF 係数の tf-idf 行列に対する貢献度を変更できます。
単語が文書に出現する回数を無視するには、'TFWeight' のバイナリ オプションを使用します。tf-idf 行列を作成し、'TFWeight' を 'binary' に設定します。最初の 10 個の行と列を表示します。
M = tfidf(bag,'TFWeight','binary'); full(M(1:10,1:10))
ans = 10×10
3.6507 4.3438 2.7344 3.6507 4.3438 2.2644 3.2452 1.9459 2.4720 2.5520
0 0 0 0 0 2.2644 0 0 0 0
0 0 0 0 0 0 0 0 0 2.5520
0 0 0 0 0 2.2644 0 0 0 0
0 0 0 0 0 2.2644 0 0 0 0
0 0 0 0 0 2.2644 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 2.2644 0 0 0 2.5520
0 0 2.7344 0 0 0 0 0 0 0
入力引数
入力の bag-of-words モデルまたは bag-of-n-grams モデル。bagOfWords オブジェクトまたは bagOfNgrams オブジェクトとして指定します。
入力文書。tokenizedDocument 配列、単語の string 配列、または文字ベクトルの cell 配列として指定します。documents は、tokenizedDocument 配列でない場合、各要素が単語である単一の文書を表す行ベクトルでなければなりません。複数の文書を指定するには、tokenizedDocument 配列を使用します。
名前と値の引数
オプションの引数のペアを Name1=Value1,...,NameN=ValueN として指定します。ここで、Name は引数名で、Value は対応する値です。名前と値の引数は他の引数の後に指定しなければなりませんが、ペアの順序は重要ではありません。
R2021a より前では、コンマを使用して名前と値をそれぞれ区切り、Name を引用符で囲みます。
例: 'Normalized',true は、頻度カウントの正規化を指定します。
用語頻度 (TF) 係数の設定方法。'TFWeight' と次のいずれかで構成されるコンマ区切りのペアとして指定します。
'raw'– TF 係数を未変更用語カウントに設定します。'binary'– TF 係数を 1 と 0 の行列に設定します。ここで、1 は、用語が文書内にあるかどうかを示します。'log'– TF 係数を1 + log(bag.Counts)に設定します。
例: 'TFWeight','binary'
データ型: char
逆文書頻度係数の計算方法。'IDFWeight' と次のいずれかで構成されるコンマ区切りのペアとして指定します。
'textrank'– TextRank IDF 重み[1]を使用します。各用語について、IDF 係数を次のように設定します。半数を超える文書に用語が出現する場合、
log((N-NT+0.5)/(NT+0.5))。ここで、Nは入力データ内の文書の数、NTは入力データ内の各用語を含む文書の数です。用語が文書の半数または f 回出現する場合、
IDFCorrection*avgIDF。ここで、avgIDFはすべてのトークンの平均 IDF です。
'classic-bm25'– 各用語について、IDF 係数をlog((N-NT+0.5)/(NT+0.5))に設定します。'normal'– 各用語について、IDF 係数をlog(N/NT)に設定します。'unary'– 各用語について、IDF 係数を 1 に設定します。'smooth'– 各用語について、IDF 係数をlog(1+N/NT)に設定します。'max'– 各用語について、IDF 係数をlog(1+max(NT)/NT)に設定します。'probabilistic'– 各用語について、IDF 係数をlog((N-NT)/NT)に設定します。
ここで、N は入力データ内の文書の数、NT は入力データ内の各用語を含む文書の数です。
例: 'IDFWeight','smooth'
データ型: char
逆文書頻度補正係数。'IDFCorrection' と非負のスカラーで構成されるコンマ区切りのペアとして指定します。
このオプションは、'IDFWeight' が 'textrank' である場合にのみ適用されます。
データ型: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64
用語カウントを正規化するオプション。'Normalized' および true または false で構成されるコンマ区切りのペアとして指定します。true の場合、関数は用語カウントの各ベクトルをユークリッド ノルムで正規化します。
例: 'Normalized',true
データ型: logical
頻度カウント行列での出力文書の方向。'DocumentsIn' と次のいずれかで構成されるコンマ区切りのペアとして指定します。
'rows'– 文書に対応する行をもつ頻度カウントの行列を返します。'columns'– 文書に対応する列をもつ頻度カウントの転置行列を返します。
データ型: char
cell 配列として出力を返すように強制するインジケーター。'ForceCellOutput' および true または false で構成されるコンマ区切りのペアとして指定します。
データ型: logical
出力引数
参照
[1] Barrios, Federico, Federico López, Luis Argerich, and Rosa Wachenchauzer. "Variations of the Similarity Function of TextRank for Automated Summarization." arXiv preprint arXiv:1602.03606 (2016).
バージョン履歴
R2017b で導入
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の MathWorks のサイトは、お客様の地域からのアクセスが最適化されていません。
南北アメリカ
- América Latina (Español)
- Canada (English)
- United States (English)
ヨーロッパ
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)