View source: R/matrixFunctions.R
cosineSimilarity | R Documentation |
Calculate the cosine similarity of two matrices or a matrix and a vector.
cosineSimilarity(x, y)
x |
A matrix or VectorSpaceModel object |
y |
A vector, matrix or VectorSpaceModel object. Vector inputs are coerced to single-row matrices; y must have the same number of dimensions as x. |
A matrix. Rows correspond to entries in x; columns to entries in y.
# Inspect the similarity of several academic disciplines by hand. subjects = demo_vectors[[c("history","literature","biology","math","stats"),average=FALSE]] similarities = cosineSimilarity(subjects,subjects) # Use 'closest_to' to build up a large list of similar words to a seed set. subjects = demo_vectors[[c("history","literature","biology","math","stats"),average=TRUE]] new_subject_list = closest_to(demo_vectors,subjects,20) new_subjects = demo_vectors[[new_subject_list$word,average=FALSE]] # Plot the cosineDistance of these as a dendrogram. plot(hclust(as.dist(cosineDist(new_subjects,new_subjects))))
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.