Skip to content

mljs/distance

Folders and files

NameName
Last commit message
Last commit date

Latest commit

8f909ba · Oct 21, 2024
Oct 17, 2024
Jan 6, 2023
Oct 17, 2024
Jan 6, 2023
Jun 29, 2019
Dec 23, 2022
Jun 1, 2023
Oct 24, 2014
Oct 21, 2024
Jan 6, 2023
Oct 17, 2024
Oct 17, 2024
Jan 6, 2023
Jan 6, 2023
Oct 17, 2024

Repository files navigation

ml-distance

Zakodium logo

Maintained by Zakodium

NPM version Node.js CI Test coverage npm download

Distance functions to compare vectors.

Installation

$ npm i ml-distance

Methods

Distances

  • euclidean(p, q)

Returns the euclidean distance between vectors p and q

d ( p , q ) = i = 1 n ( p i q i ) 2

  • manhattan(p, q)

Returns the city block distance between vectors p and q

d ( p , q ) = i = 1 n | p i q i |

  • minkowski(p, q, d)

Returns the Minkowski distance between vectors p and q for order d

  • chebyshev(p, q)

Returns the Chebyshev distance between vectors p and q

d ( p , q ) = max i ( | p i q i | )

  • sorensen(p, q)

Returns the Sørensen distance between vectors p and q

d ( p , q ) = i = 1 n | p i q i | i = 1 n p i + q i

  • gower(p, q)

Returns the Gower distance between vectors p and q

d ( p , q ) = i = 1 n | p i q i | n

  • soergel(p, q)

Returns the Soergel distance between vectors p and q

d ( p , q ) = i = 1 n | p i q i | m a x ( p i , q i )

  • kulczynski(p, q)

Returns the Kulczynski distance between vectors p and q

d ( p , q ) = i = 1 n | p i q i | m i n ( p i , q i )

  • canberra(p, q)

Returns the Canberra distance between vectors p and q

d ( p , q ) = i = 1 n | p i q i | p i + q i

  • lorentzian(p, q)

Returns the Lorentzian distance between vectors p and q

d ( p , q ) = i = 1 n ln ( | p i q i | + 1 )

  • intersection(p, q)

Returns the Intersection distance between vectors p and q

d ( p , q ) = 1 i = 1 n m i n ( p i , q i )

  • waveHedges(p, q)

Returns the Wave Hedges distance between vectors p and q

d ( p , q ) = i = 1 n ( 1 m i n ( p i , q i ) m a x ( p i , q i ) )

  • czekanowski(p, q)

Returns the Czekanowski distance between vectors p and q

d ( p , q ) = 1 2 i = 1 n m i n ( p i , q i ) i = 1 n p i + q i

  • motyka(p, q)

Returns the Motyka distance between vectors p and q

d ( p , q ) = 1 i = 1 n m i n ( p i , q i ) i = 1 n p i + q i

Note: distance between 2 identical vectors is 0.5 !

  • ruzicka(p, q)

Returns the Ruzicka similarity between vectors p and q

d ( p , q ) = i = 1 n m a x ( p i , q i ) i = 1 n m i n ( p i , q i )

  • tanimoto(p, q, [bitVector])

Returns the Tanimoto distance between vectors p and q, and accepts the bitVector use, see the test case for an example

  • innerProduct(p, q)

Returns the Inner Product similarity between vectors p and q

s ( p , q ) = i = 1 n p i q i

  • harmonicMean(p, q)

Returns the Harmonic mean similarity between vectors p and q

d ( p , q ) = 2 i = 1 n p i q i p i + q i

  • cosine(p, q)

Returns the Cosine similarity between vectors p and q

d ( p , q ) = i = 1 n p i q i i = 1 n p i 2 i = 1 n q i 2

  • kumarHassebrook(p, q)

Returns the Kumar-Hassebrook similarity between vectors p and q

d ( p , q ) = i = 1 n p i q i i = 1 n p i 2 + i = 1 n q i 2 i = 1 n p i q i

  • jaccard(p, q)

Returns the Jaccard distance between vectors p and q

d ( p , q ) = 1 i = 1 n p i q i i = 1 n p i 2 + i = 1 n q i 2 i = 1 n p i q i

  • dice(p,q)

Returns the Dice distance between vectors p and q

d ( p , q ) = 1 i = 1 n ( p i q i ) 2 i = 1 n p i 2 + i = 1 n q i 2

  • fidelity(p, q)

Returns the Fidelity similarity between vectors p and q

d ( p , q ) = i = 1 n p i q i

  • bhattacharyya(p, q)

Returns the Bhattacharyya distance between vectors p and q

d ( p , q ) = ln ( i = 1 n p i q i )

  • hellinger(p, q)

Returns the Hellinger distance between vectors p and q

d ( p , q ) = 2 1 i = 1 n p i q i

  • matusita(p, q)

Returns the Matusita distance between vectors p and q

d ( p , q ) = 2 2 i = 1 n p i q i

  • squaredChord(p, q)

Returns the Squared-chord distance between vectors p and q

d ( p , q ) = i = 1 n ( p i q i ) 2

  • squaredEuclidean(p, q)

Returns the squared euclidean distance between vectors p and q

d ( p , q ) = i = 1 n ( p i q i ) 2

  • pearson(p, q)

Returns the Pearson distance between vectors p and q

d ( p , q ) = i = 1 n ( p i q i ) 2 q i

  • neyman(p, q)

Returns the Neyman distance between vectors p and q

d ( p , q ) = i = 1 n ( p i q i ) 2 p i

  • squared(p, q)

Returns the Squared distance between vectors p and q

d ( p , q ) = i = 1 n ( p i q i ) 2 p i + q i

  • probabilisticSymmetric(p, q)

Returns the Probabilistic Symmetric distance between vectors p and q

d ( p , q ) = 2 i = 1 n ( p i q i ) 2 p i + q i

  • divergence(p, q)

Returns the Divergence distance between vectors p and q

d ( p , q ) = 2 i = 1 n ( p i q i ) 2 ( p i + q i ) 2

  • clark(p, q)

Returns the Clark distance between vectors p and q

d ( p , q ) = i = 1 n ( | p i q i | ( p i + q i ) ) 2

  • additiveSymmetric(p, q)

Returns the Additive Symmetric distance between vectors p and q

d ( p , q ) = i = 1 n ( p i q i ) 2 ( p i + q i ) p i q i

  • kullbackLeibler(p, q)

Returns the Kullback-Leibler distance between vectors p and q

d ( p , q ) = i = 1 n p i ln p i q i

  • jeffreys(p, q)

Returns the Jeffreys distance between vectors p and q

d ( p , q ) = i = 1 n ( ( p i q i ) ln p i q i )

  • kdivergence(p, q)

Returns the K divergence distance between vectors p and q

d ( p , q ) = i = 1 n ( p i ln 2 p i p i + q i )

  • topsoe(p, q)

Returns the Topsøe distance between vectors p and q

d ( p , q ) = i = 1 n ( p i ln 2 p i p i + q i + q i ln 2 q i p i + q i )

  • jensenShannon(p, q)

Returns the Jensen-Shannon distance between vectors p and q

d ( p , q ) = 1 2 [ i = 1 n p i ln 2 p i p i + q i + i = 1 n q i ln 2 q i p i + q i ]

  • jensenDifference(p, q)

Returns the Jensen difference distance between vectors p and q

d ( p , q ) = i = 1 n [ p i ln p i + q i ln q i 2 ( p i + q i 2 ) ln ( p i + q i 2 ) ]

  • taneja(p, q)

Returns the Taneja distance between vectors p and q

d ( p , q ) = i = 1 n [ p i + q i 2 ln ( p i + q i 2 p i q i ) ]

  • kumarJohnson(p, q)

Returns the Kumar-Johnson distance between vectors p and q

d ( p , q ) = i = 1 n ( p i 2 q i 2 ) 2 2 ( p i q i ) 3 / 2

  • avg(p, q)

Returns the average of city block and Chebyshev distances between vectors p and q

d ( p , q ) = i = 1 n | p i q i | + max i ( | p i q i | ) 2

Similarities

  • intersection(p, q)

Returns the Intersection similarity between vectors p and q

  • czekanowski(p, q)

Returns the Czekanowski similarity between vectors p and q

  • motyka(p, q)

Returns the Motyka similarity between vectors p and q

  • kulczynski(p, q)

Returns the Kulczynski similarity between vectors p and q

  • squaredChord(p, q)

Returns the Squared-chord similarity between vectors p and q

  • jaccard(p, q)

Returns the Jaccard similarity between vectors p and q

  • dice(p, q)

Returns the Dice similarity between vectors p and q

  • tanimoto(p, q, [bitVector])

Returns the Tanimoto similarity between vectors p and q, and accepts the bitVector use, see the test case for an example

  • tree(a,b, from, to, [options])

Refer to ml-tree-similarity

Contributing

A new metric should normally be in its own file in the src/dist directory. There should be a corresponding test file in test/dist.
The metric should be then added in the exports of src/index.js with a relatively small but understandable name (use camelCase).
It should also be added to this README with either a link to the formula or an inline description.

Authors

License

MIT