({{, (RefLink[CosineDistance, paclet:ref/CosineDistance])[u,v]
gives the angular cosine distance between vectors u and v.}})

OpenersNotesSection(Spacer1)Details

(RefLink[CosineDistance, paclet:ref/CosineDistance])[u,v] is equivalent to 1-u.v/((RefLink[Norm, paclet:ref/Norm])[u](RefLink[Norm, paclet:ref/Norm])[v]). »

OpenersPrimaryExamplesSection(Spacer1)Examples  (8)

OpenersExampleSection0(Spacer1)Basic Examples  (2)

Cosine distance between two vectors:

CosineDistance[{a,b,c},{x,y,z}]

Cosine distance between numeric vectors:

CosineDistance[{1,2,3},{3,5,7}]

OpenersExampleSection1(Spacer1)Scope  (2)

Compute distance between any vectors of equal length:

CosineDistance[RandomReal[5,100],RandomReal[5,100]]

Compute distance between vectors of any precision:

CosineDistance[N[{1,5,2,3,10},50],N[{4,15,20,5,5},50]]

OpenersExampleSection3(Spacer1)Applications  (1)

Cluster data using cosine distance:

FindClusters[{{2,3},{5,10},{4,5},{2,2}},DistanceFunction->CosineDistance]

OpenersExampleSection4(Spacer1)Properties & Relations  (3)

Cosine distance includes a dot product scaled by norms:

CosineDistance[{a,b,c},{x,y,z}]

1-{a,b,c}.{x,y,z}/(Norm[{a,b,c}]Norm[{x,y,z}])

Cosine distance includes a dot product scaled by Euclidean distances from the origin:

u={a,b,c};
v={x,y,z};

scale=(EuclideanDistance[u,{0,0,0}]EuclideanDistance[v,{0,0,0}])

CosineDistance[u,v]==1-u.v/scale

(RefLink[CosineDistance, paclet:ref/CosineDistance]) of vectors shifted by their means is equivalent to (RefLink[CorrelationDistance, paclet:ref/CorrelationDistance]):

u={a,b,c};
v={x,y,z};

CosineDistance[u-Mean[u],v-Mean[v]]==CorrelationDistance[u,v]

({{DocumentationFooterIconsRelatedFunction.png, ({{See Also}, {(RefLink[CorrelationDistance, paclet:ref/CorrelationDistance])}})}})

({{DocumentationFooterIconsRelatedTutorial.png, ({{Tutorials}, {▪(RefLinkPlain[Partitioning Data into Clusters, paclet:tutorial/PartitioningDataIntoClusters])}})}})

({{DocumentationFooterIconsRelatedGuide.png, ({{Related Guides}, {▪(RefLinkPlain[Distance and Similarity Measures, paclet:guide/DistanceAndSimilarityMeasures])}})}})