main content

term frequency–inverse document frequency (tf-凯发k8网页登录

term frequency–inverse document frequency (tf-idf) matrix

description

example

m = tfidf(bag) returns a term frequency-inverse document frequency (tf-idf) matrix based on the bag-of-words or bag-of-n-grams model bag.

example

m = tfidf(bag,documents) returns a tf-idf matrix for the documents in documents by using the inverse document frequency (idf) factor computed from bag.

example

m = tfidf(___,name,value) specifies additional options using one or more name-value pair arguments.

examples

create a term frequency–inverse document frequency (tf-idf) matrix from a bag-of-words model.

load the example data. the file sonnetspreprocessed.txt contains preprocessed versions of shakespeare's sonnets. the file contains one sonnet per line, with words separated by a space. extract the text from sonnetspreprocessed.txt, split the text into documents at newline characters, and then tokenize the documents.

filename = "sonnetspreprocessed.txt";
str = extractfiletext(filename);
textdata = split(str,newline);
documents = tokenizeddocument(textdata);

create a bag-of-words model using bagofwords.

bag = bagofwords(documents)
bag = 
  bagofwords with properties:
          counts: [154x3092 double]
      vocabulary: ["fairest"    "creatures"    "desire"    "increase"    "thereby"    "beautys"    "rose"    "might"    "never"    "die"    "riper"    "time"    "decease"    "tender"    "heir"    "bear"    "memory"    "thou"    "contracted"    ...    ]
        numwords: 3092
    numdocuments: 154

create a tf-idf matrix. view the first 10 rows and columns.

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

create a term frequency-inverse document frequency (tf-idf) matrix from a bag-of-words model and an array of new documents.

load the example data. the file sonnetspreprocessed.txt contains preprocessed versions of shakespeare's sonnets. the file contains one sonnet per line, with words separated by a space. extract the text from sonnetspreprocessed.txt, split the text into documents at newline characters, and then tokenize the documents.

filename = "sonnetspreprocessed.txt";
str = extractfiletext(filename);
textdata = split(str,newline);
documents = tokenizeddocument(textdata);

create a bag-of-words model from the documents.

bag = bagofwords(documents) 
bag = 
  bagofwords with properties:
          counts: [154x3092 double]
      vocabulary: ["fairest"    "creatures"    "desire"    "increase"    "thereby"    "beautys"    "rose"    "might"    "never"    "die"    "riper"    "time"    "decease"    "tender"    "heir"    "bear"    "memory"    "thou"    "contracted"    ...    ]
        numwords: 3092
    numdocuments: 154

create a tf-idf matrix for an array of new documents using the inverse document frequency (idf) factor computed from bag.

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 = 
   (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

load the example data. the file sonnetspreprocessed.txt contains preprocessed versions of shakespeare's sonnets. the file contains one sonnet per line, with words separated by a space. extract the text from sonnetspreprocessed.txt, split the text into documents at newline characters, and then tokenize the documents.

filename = "sonnetspreprocessed.txt";
str = extractfiletext(filename);
textdata = split(str,newline);
documents = tokenizeddocument(textdata);

create a bag-of-words model using bagofwords.

bag = bagofwords(documents)
bag = 
  bagofwords with properties:
          counts: [154x3092 double]
      vocabulary: ["fairest"    "creatures"    "desire"    "increase"    "thereby"    "beautys"    "rose"    "might"    "never"    "die"    "riper"    "time"    "decease"    "tender"    "heir"    "bear"    "memory"    "thou"    "contracted"    ...    ]
        numwords: 3092
    numdocuments: 154

create a tf-idf matrix. view the first 10 rows and columns.

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

you can change the contributions made by the tf and idf factors to the tf-idf matrix by specifying the tf and idf weight formulas.

to ignore how many times a word appears in a document, use the binary option of 'tfweight'. create a tf-idf matrix and set 'tfweight' to 'binary'. view the first 10 rows and columns.

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

input arguments

input bag-of-words or bag-of-n-grams model, specified as a object or a object.

input documents, specified as a tokenizeddocument array, a string array of words, or a cell array of character vectors. if documents is not a tokenizeddocument array, then it must be a row vector representing a single document, where each element is a word. to specify multiple documents, use a tokenizeddocument array.

name-value arguments

specify optional pairs of arguments as name1=value1,...,namen=valuen, where name is the argument name and value is the corresponding value. name-value arguments must appear after other arguments, but the order of the pairs does not matter.

before r2021a, use commas to separate each name and value, and enclose name in quotes.

example: 'normalized',true specifies to normalize the frequency counts.

method to set term frequency (tf) factor, specified as the comma-separated pair consisting of 'tfweight' and one of the following:

  • 'raw' – set the tf factor to the unchanged term counts.

  • 'binary' – set the tf factor to the matrix of ones and zeros where the ones indicate whether a term is in a document.

  • 'log' – set the tf factor to 1 log(bag.counts).

example: 'tfweight','binary'

data types: char

method to compute inverse document frequency factor, specified as the comma-separated pair consisting of 'idfweight' and one of the following:

  • 'textrank' – use textrank idf weighting . for each term, set the idf factor to

    • log((n-nt 0.5)/(nt 0.5)) if the term occurs in more than half of the documents, where n is the number of documents in the input data and nt is the number of documents in the input data containing each term.

    • idfcorrection*avgidf if the term occurs in half of the documents or f, where avgidf is the average idf of all tokens.

  • 'classic-bm25' – for each term, set the idf factor to log((n-nt 0.5)/(nt 0.5)).

  • 'normal' – for each term, set the idf factor to log(n/nt).

  • 'unary' – for each term, set the idf factor to 1.

  • 'smooth' – for each term, set the idf factor to log(1 n/nt).

  • 'max' – for each term, set the idf factor to log(1 max(nt)/nt).

  • 'probabilistic' – for each term, set the idf factor to log((n-nt)/nt).

where n is the number of documents in the input data and nt is the number of documents in the input data containing each term.

example: 'idfweight','smooth'

data types: char

inverse document frequency correction factor, specified as the comma-separated pair consisting of 'idfcorrection' and a nonnegative scalar.

this option only applies when is 'textrank'.

data types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64

option to normalize term counts, specified as the comma-separated pair consisting of 'normalized' and true or false. if true, then the function normalizes each vector of term counts in the euclidean norm.

example: 'normalized',true

data types: logical

orientation of output documents in the frequency count matrix, specified as the comma-separated pair consisting of 'documentsin' and one of the following:

  • 'rows' – return a matrix of frequency counts with rows corresponding to documents.

  • 'columns' – return a transposed matrix of frequency counts with columns corresponding to documents.

data types: char

indicator for forcing output to be returned as cell array, specified as the comma separated pair consisting of 'forcecelloutput' and true or false.

data types: logical

output arguments

output term frequency-inverse document frequency matrix, specified as a sparse matrix or a cell array of sparse matrices.

if bag is a non-scalar array or 'forcecelloutput' is true, then the function returns the outputs as a cell array of sparse matrices. each element in the cell array is the tf-idf matrix calculated from the corresponding element of bag.

references

[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).

version history

introduced in r2017b

网站地图