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)는 행렬 M1M2의 인코딩된 문서 사이의 유사도를 반환합니다. 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형 배열 또는 문자형 벡터로 구성된 셀형 배열로 지정됩니다. documentstokenizedDocument 배열이 아닌 경우 이는 단일 문서를 나타내고 각 요소가 단어인 행 벡터여야 합니다. 문서를 여러 개 지정하려면 tokenizedDocument 배열을 사용하십시오.

입력 bag-of-words 모델 또는 bag-of-n-grams 모델로, bagOfWords 객체 또는 bagOfNgrams 객체로 지정됩니다. bagbagOfNgrams 객체일 경우 이 함수는 각 n-gram을 단일 단어로 처리합니다.

쿼리 문서의 세트로, 다음 중 하나로 지정됩니다.

  • tokenizedDocument 배열

  • 단일 문서를 나타내는 1×N string형 배열로 지정하며, 여기서 각 요소가 단어임

  • 단일 문서를 나타내는 문자열 벡터로 구성된 1×N 셀형 배열, 여기서 각 요소가 단어임

이 함수는 단어 빈도 및 역 문서 빈도 통계를 계산하기 위해 bag-of-words 모델을 사용하여 queries를 인코딩합니다. 사용하는 모델은 해당 함수를 호출하는 데 사용되는 구문에 따라 달라집니다. 구문에서 documents 입력 인수를 지정하면 bagOfWords(documents)가 사용됩니다. 구문에서 bag을 지정하면 이 함수는 bag을 사용하여 queries를 인코딩하고 결과로 생성되는 TF-IDF 행렬을 사용합니다.

입력 데이터로, 행렬로 지정됩니다. 예를 들어 M은 단어 또는 n-gram 개수로 구성된 행렬이거나 TF-IDF 행렬일 수 있습니다.

데이터형: double

출력 인수

모두 축소

코사인 유사도 점수로, 희소 행렬로 반환됩니다.

  • 토큰화된 문서로 구성된 단일 배열이 지정된 경우, 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.NumDocuments×bag.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,:) 사이의 유사도를 나타냅니다.

  • 두 행렬이 지정된 경우, similaritiessize(M1,1)×size(M2,1) 행렬입니다. 여기서 similarities(i,j)M1(i,:)M2(j,:) 사이의 유사도를 나타냅니다.

버전 내역

R2020a에 개발됨