term frequency–inverse document frequency (tf-凯发k8网页登录
term frequency–inverse document frequency (tf-idf) matrix
description
examples
create tf-idf matrix
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 tf-idf matrix from new documents
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
specify tf weight formulas
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
bag
— input bag-of-words or bag-of-n-grams model
bagofwords
object | bagofngrams
object
input bag-of-words or bag-of-n-grams model, specified as a object or a object.
documents
— input documents
tokenizeddocument
array | string array of words | cell array of character vectors
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.
tfweight
— method to set term frequency factor
'raw'
(default) | 'binary'
| 'log'
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 to1 log(bag.counts)
.
example: 'tfweight','binary'
data types: char
idfweight
— method to compute inverse document frequency factor
'normal'
(default) | 'textrank'
| 'classic-bm25'
| 'unary'
| 'smooth'
| 'max'
| 'probabilistic'
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 tolog((n-nt 0.5)/(nt 0.5))
if the term occurs in more than half of the documents, wheren
is the number of documents in the input data andnt
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, whereavgidf
is the average idf of all tokens.
'classic-bm25'
– for each term, set the idf factor tolog((n-nt 0.5)/(nt 0.5))
.'normal'
– for each term, set the idf factor tolog(n/nt)
.'unary'
– for each term, set the idf factor to 1.'smooth'
– for each term, set the idf factor tolog(1 n/nt)
.'max'
– for each term, set the idf factor tolog(1 max(nt)/nt)
.'probabilistic'
– for each term, set the idf factor tolog((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
idfcorrection
— inverse document frequency correction factor
0.25 (default) | nonnegative scalar
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
normalized
— option to normalize term counts
false
(default) | true
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
documentsin
— orientation of output documents
'rows'
(default) | 'columns'
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
forcecelloutput
— indicator for forcing output to be returned as cell array
false
(default) | true
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
m
— output term frequency-inverse document frequency matrix
sparse matrix | cell array of sparse matrices
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
打开示例
您曾对此示例进行过修改。是否要打开带有您的编辑的示例?
matlab 命令
您点击的链接对应于以下 matlab 命令:
请在 matlab 命令行窗口中直接输入以执行命令。web 浏览器不支持 matlab 命令。
select a web site
choose a web site to get translated content where available and see local events and offers. based on your location, we recommend that you select: .
you can also select a web site from the following list:
how to get best site performance
select the china site (in chinese or english) for best site performance. other mathworks country sites are not optimized for visits from your location.
americas
- (español)
- (english)
- (english)
europe
- (english)
- (english)
- (deutsch)
- (español)
- (english)
- (français)
- (english)
- (italiano)
- (english)
- (english)
- (english)
- (deutsch)
- (english)
- (english)
- switzerland
- (english)
asia pacific
- (english)
- (english)
- (english)
- 中国
- (日本語)
- (한국어)