Main Content

このページの内容は最新ではありません。最新版の英語を参照するには、ここをクリックします。

cosineSimilarity

コサイン類似度を使用した文書の類似度

R2020a 以降

説明

similarities = cosineSimilarity(documents) は、単語カウントから派生させた tf-idf 行列を使用して、指定された文書のペア単位のコサイン類似度を返します。similarities(i,j) のスコアは、documents(i)documents(j) の間の類似度を表します。

similarities = cosineSimilarity(documents,queries) は、documents 内の単語カウントから派生させた tf-idf 行列を使用して、documentsqueries の間の類似度を返します。similarities(i,j) のスコアは、documents(i)queries(j) の間の類似度を表します。

similarities = cosineSimilarity(bag) は、bag 内の単語カウントから派生させた tf-idf 行列を使用して、指定された bag-of-words または bag-of-n-grams モデルによって符号化された文書のペア単位の類似度を返します。similarities(i,j) のスコアは、bag によって符号化された i 番目の文書と j 番目の文書の間の類似度を表します。

similarities = cosineSimilarity(bag,queries) は、bag 内の単語カウントから派生させた tf-idf 行列を使用して、bag-of-words または bag-of-n-grams モデル bag によって符号化された文書と queries の間の類似度を返します。similarities(i,j) のスコアは、bag によって符号化された i 番目の文書と queries(j) の間の類似度を表します。

similarities = cosineSimilarity(M) は、行列 M の行ベクトル内の符号化されたデータの類似度を返します。similarities(i,j) のスコアは、M(i,:)M(j,:) の間の類似度を表します。

similarities = cosineSimilarity(M1,M2) は、行列 M1 と行列 M2 内の符号化された文書間の類似度を返します。similarities(i,j) のスコアは、M1(i,:)M2(j,:) の間の類似度に対応します。

すべて折りたたむ

トークン化された文書の配列を作成します。

textData = [
    "the quick brown fox jumped over the lazy dog"
    "the fast brown fox jumped over the lazy dog"
    "the lazy dog sat there and did nothing"
    "the other animals sat there watching"];
documents = tokenizedDocument(textData)
documents = 
  4x1 tokenizedDocument:

    9 tokens: the quick brown fox jumped over the lazy dog
    9 tokens: the fast brown fox jumped over the lazy dog
    8 tokens: the lazy dog sat there and did nothing
    6 tokens: the other animals sat there watching

関数 cosineSimilarity を使用して、それらの間の類似度を計算します。出力はスパース行列です。

similarities = cosineSimilarity(documents);

文書間の類似度をヒート マップで可視化します。

figure
heatmap(similarities);
xlabel("Document")
ylabel("Document")
title("Cosine Similarities")

Figure contains an object of type heatmap. The chart of type heatmap has title Cosine Similarities.

1 に近いスコアは、類似度が高いことを示します。0 に近いスコアは、類似度が低いことを示します。

入力文書の配列を作成します。

str = [
    "the quick brown fox jumped over the lazy dog"
    "the fast fox jumped over the lazy dog"
    "the dog sat there and did nothing"
    "the other animals sat there watching"];
documents = tokenizedDocument(str)
documents = 
  4x1 tokenizedDocument:

    9 tokens: the quick brown fox jumped over the lazy dog
    8 tokens: the fast fox jumped over the lazy dog
    7 tokens: the dog sat there and did nothing
    6 tokens: the other animals sat there watching

クエリ文書の配列を作成します。

str = [
    "a brown fox leaped over the lazy dog"
    "another fox leaped over the dog"];
queries = tokenizedDocument(str)
queries = 
  2x1 tokenizedDocument:

    8 tokens: a brown fox leaped over the lazy dog
    6 tokens: another fox leaped over the dog

関数 cosineSimilarity を使用して、入力文書とクエリ文書の間の類似度を計算します。出力はスパース行列です。

similarities = cosineSimilarity(documents,queries);

文書の類似度をヒート マップで可視化します。

figure
heatmap(similarities);
xlabel("Query Document")
ylabel("Input Document")
title("Cosine Similarities")

Figure contains an object of type heatmap. The chart of type heatmap has title Cosine Similarities.

1 に近いスコアは、類似度が高いことを示します。0 に近いスコアは、類似度が低いことを示します。

sonnets.csv のテキスト データから bag-of-words モデルを作成します。

filename = "sonnets.csv";
tbl = readtable(filename,'TextType','string');
textData = tbl.Sonnet;
documents = tokenizedDocument(textData);
bag = bagOfWords(documents)
bag = 
  bagOfWords with properties:

          Counts: [154x3527 double]
      Vocabulary: ["From"    "fairest"    "creatures"    "we"    "desire"    "increase"    ","    "That"    "thereby"    "beauty's"    "rose"    "might"    "never"    "die"    "But"    "as"    "the"    "riper"    "should"    ...    ] (1x3527 string)
        NumWords: 3527
    NumDocuments: 154

関数 cosineSimilarity を使用して、ソネット間の類似度を計算します。出力はスパース行列です。

similarities = cosineSimilarity(bag);

最初の 5 つの文書の類似度をヒート マップで可視化します。

figure
heatmap(similarities(1:5,1:5));
xlabel("Document")
ylabel("Document")
title("Cosine Similarities")

Figure contains an object of type heatmap. The chart of type heatmap has title Cosine Similarities.

1 に近いスコアは、類似度が高いことを示します。0 に近いスコアは、類似度が低いことを示します。

bag-of-words 入力の場合、関数 cosineSimilarity は、モデルから派生させた tf-idf 行列を使用してコサイン類似度を計算します。単語カウント ベクトルのコサイン類似度を直接計算するには、単語カウントを関数 cosineSimilarity に行列として入力します。

sonnets.csv のテキスト データから bag-of-words モデルを作成します。

filename = "sonnets.csv";
tbl = readtable(filename,'TextType','string');
textData = tbl.Sonnet;
documents = tokenizedDocument(textData);
bag = bagOfWords(documents)
bag = 
  bagOfWords with properties:

          Counts: [154x3527 double]
      Vocabulary: ["From"    "fairest"    "creatures"    "we"    "desire"    "increase"    ","    "That"    "thereby"    "beauty's"    "rose"    "might"    "never"    "die"    "But"    "as"    "the"    "riper"    "should"    ...    ] (1x3527 string)
        NumWords: 3527
    NumDocuments: 154

モデルから単語カウントの行列を取得します。

M = bag.Counts;

関数 cosineSimilarity を使用して、単語カウント行列の文書のコサイン類似度を計算します。出力はスパース行列です。

similarities = cosineSimilarity(M);

最初の 5 つの文書の類似度をヒート マップで可視化します。

figure
heatmap(similarities(1:5,1:5));
xlabel("Document")
ylabel("Document")
title("Cosine Similarities")

Figure contains an object of type heatmap. The chart of type heatmap has title Cosine Similarities.

1 に近いスコアは、類似度が高いことを示します。0 に近いスコアは、類似度が低いことを示します。

入力引数

すべて折りたたむ

入力文書。tokenizedDocument 配列、単語の string 配列、または文字ベクトルの cell 配列として指定します。documents は、tokenizedDocument 配列でない場合、各要素が単語である単一の文書を表す行ベクトルでなければなりません。複数の文書を指定するには、tokenizedDocument 配列を使用します。

入力の bag-of-words モデルまたは bag-of-n-grams モデル。bagOfWords オブジェクトまたは bagOfNgrams オブジェクトとして指定します。bagbagOfNgrams オブジェクトの場合、関数は各 n-gram を 1 つの単語として扱います。

クエリ文書のセット。次のいずれかとして指定します。

  • tokenizedDocument 配列

  • 各要素が単語である単一の文書を表す 1 行 N 列の string 配列

  • 各要素が単語である単一の文書を表す文字ベクトルから成る 1 行 N 列の cell 配列

単語頻度と逆文書頻度の統計を計算するために、関数は bag-of-words モデルを使用して queries を符号化します。使用されるモデルは、呼び出す構文によって異なります。構文で入力引数 documents が指定されている場合、bagOfWords(documents) が使用されます。構文で bag が指定されている場合、関数は bag を使用して queries を符号化し、結果の tf-idf 行列を使用します。

入力データ。行列として指定します。たとえば、M は、単語または n-gram のカウントの行列、または tf-idf 行列にすることができます。

データ型: double

出力引数

すべて折りたたむ

コサイン類似度スコア。次のスパース行列として返されます。

  • トークン化された文書の 1 つの配列が与えられた場合、similarities は N 行 N 列の対称行列になります。ここで、similarities(i,j)documents(i)documents(j) の間の類似度を表し、N は入力文書の数です。

  • トークン化された文書の配列とクエリ文書のセットが与えられた場合、similarities は N1 行 N2 列の行列になります。ここで、similarities(i,j)documents(i)j 番目のクエリ文書の間の類似度を表し、N1 と N2 はそれぞれ documentsqueries の文書数を表します。

  • 単一の bag-of-words モデルまたは bag-of-n-grams モデルが与えられた場合、similaritiesbag.NumDocumentsbag.NumDocuments 列の対称行列になります。ここで、similarities(i,j) は、bag によって符号化された i 番目の文書と j 番目の文書の間の類似度を表します。

  • bag-of-words モデルまたは bag-of-n-grams モデルとクエリ文書のセットが与えられた場合、similaritiesbag.NumDocuments 行 N2 列の行列になります。ここで、similarities(i,j) は、bag によって符号化された i 番目の文書と queriesj 番目の文書の間の類似度を表します。また、N2 は queries の文書の数に対応します。

  • 単一の行列が与えられた場合、similaritiessize(M,1)size(M,1) 列の対称行列になります。ここで、similarities(i,j)M(i,:)M(j,:) の間の類似度を表します。

  • 2 つの行列が与えられた場合、similaritiessize(M1,1)size(M2,1) 列の行列になります。ここで、similarities(i,j)M1(i,:)M2(j,:) の間の類似度を表します。

バージョン履歴

R2020a で導入