View Source Scholar.Metrics.Similarity (Scholar v0.3.0)

Similarity metrics between multi-dimensional tensors.

Summary

Functions

Calculates Jaccard similarity based on binary attributes. It assumes that inputs have the same shape.

Calculates Dice coefficient.

Calculates Dice coefficient based on binary attributes. It assumes that inputs have the same shape.

Calculates Jaccard similarity (also known as Jaccard similarity coefficient, or Jaccard index).

Functions

Link to this function

binary_jaccard(x, y, opts \\ [])

View Source

Calculates Jaccard similarity based on binary attributes. It assumes that inputs have the same shape.

$$ J(X, Y) = \frac{M\_{11}}{M\_{01} + M\_{10} + M\_{11}} $$

Where:

  • $M_{11}$ is the total number of attributes, for which both $X$ and $Y$ have 1.
  • $M_{10}$ is the total number of attributes, for which $X$ has 1 and $Y$ has 0.
  • $M_{01}$ is the total number of attributes, for which $X$ has 0 and $Y$ has 1.

Options

  • :axis - Axis over which the distance will be calculated. By default the distance is calculated between the whole tensors.

Examples

iex> x = Nx.tensor([1,0,0,1,1,1])
iex> y = Nx.tensor([0,0,1,1,1,0])
iex> Scholar.Metrics.Similarity.binary_jaccard(x, y)
#Nx.Tensor<
  f32
  0.4000000059604645
>

iex> x = Nx.tensor([[1,1,0,1], [1,1,0,1]])
iex> y = Nx.tensor([[1,1,0,1], [1,0,0,1]])
iex> Scholar.Metrics.Similarity.binary_jaccard(x, y)
#Nx.Tensor<
  f32
  0.8333333134651184
>

iex> x = Nx.tensor([[1,1,0,1], [1,1,0,1]])
iex> y = Nx.tensor([[1,1,0,1], [1,0,0,1]])
iex> Scholar.Metrics.Similarity.binary_jaccard(x, y, axis: 1)
#Nx.Tensor<
  f32[2]
  [1.0, 0.6666666865348816]
>

iex> x = Nx.tensor([1, 1])
iex> y = Nx.tensor([1, 0, 0])
iex> Scholar.Metrics.Similarity.binary_jaccard(x, y)
** (ArgumentError) expected tensor to have shape {2}, got tensor with shape {3}
Link to this function

dice_coefficient(x, y, opts \\ [])

View Source

Calculates Dice coefficient.

It is a statistic used to gauge the similarity of two samples. Mathematically, it is defined as:

$$ Dice(A, B) = \frac{2 \mid A \cap B \mid}{\mid A \mid + \mid B \mid} $$

where A and B are sets.

Options

  • :axis - Axis over which the distance will be calculated. By default the distance is calculated between the whole tensors.

Examples

iex> x = Nx.tensor([1.0, 5.0, 3.0, 6.7])
iex> y = Nx.tensor([5.0, 2.5, 3.1, 9.0])
iex> Scholar.Metrics.Similarity.dice_coefficient(x, y)
#Nx.Tensor<
  f32
  0.25
>

iex> x = Nx.tensor([1, 2, 3, 5, 7])
iex> y = Nx.tensor([1, 2, 4, 8, 9])
iex> Scholar.Metrics.Similarity.dice_coefficient(x, y)
#Nx.Tensor<
  f32
  0.4000000059604645
>

iex> x = Nx.iota({2,3})
iex> y = Nx.tensor([[0, 3, 4], [3, 4, 8]])
iex> Scholar.Metrics.Similarity.dice_coefficient(x, y, axis: 1)
#Nx.Tensor<
  f32[2]
  [0.3333333134651184, 0.6666666865348816]
>

iex> x = Nx.tensor([1, 2])
iex> y = Nx.tensor([1, 2, 3])
iex> Scholar.Metrics.Similarity.dice_coefficient(x, y)
#Nx.Tensor<
  f32
  0.800000011920929
>
Link to this function

dice_coefficient_binary(x, y, opts \\ [])

View Source

Calculates Dice coefficient based on binary attributes. It assumes that inputs have the same shape.

Options

  • :axis - Axis over which the distance will be calculated. By default the distance is calculated between the whole tensors.

Examples

iex> x = Nx.tensor([1,0,0,1,1,1])
iex> y = Nx.tensor([0,0,1,1,1,0])
iex> Scholar.Metrics.Similarity.dice_coefficient_binary(x, y)
#Nx.Tensor<
  f32
  0.5714285969734192
>

iex> x = Nx.tensor([[1,1,0,1], [1,1,0,1]])
iex> y = Nx.tensor([[1,1,0,1], [1,0,0,1]])
iex> Scholar.Metrics.Similarity.dice_coefficient_binary(x, y)
#Nx.Tensor<
  f32
  0.9090909361839294
>

iex> x = Nx.tensor([[1,1,0,1], [1,1,0,1]])
iex> y = Nx.tensor([[1,1,0,1], [1,0,0,1]])
iex> Scholar.Metrics.Similarity.dice_coefficient_binary(x, y, axis: 1)
#Nx.Tensor<
  f32[2]
  [1.0, 0.800000011920929]
>

iex> x = Nx.tensor([1, 1])
iex> y = Nx.tensor([1, 0, 0])
iex> Scholar.Metrics.Similarity.dice_coefficient_binary(x, y)
** (ArgumentError) expected tensor to have shape {2}, got tensor with shape {3}
Link to this function

jaccard(x, y, opts \\ [])

View Source

Calculates Jaccard similarity (also known as Jaccard similarity coefficient, or Jaccard index).

Jaccard similarity is a statistic used to measure similarities between two sets. Mathematically, the calculation of Jaccard similarity is the ratio of set intersection over set union.

$$ J(A, B) = \frac{\mid A \cap B \mid}{\mid A \cup B \mid} $$

where A and B are sets.

Options

  • :axis - Axis over which the distance will be calculated. By default the distance is calculated between the whole tensors.

Examples

iex> x = Nx.tensor([1.0, 5.0, 3.0, 6.7])
iex> y = Nx.tensor([5.0, 2.5, 3.1, 9.0])
iex> Scholar.Metrics.Similarity.jaccard(x, y)
#Nx.Tensor<
  f32
  0.1428571492433548
>

iex> x = Nx.tensor([1, 2, 3, 5, 7])
iex> y = Nx.tensor([1, 2, 4, 8, 9])
iex> Scholar.Metrics.Similarity.jaccard(x, y)
#Nx.Tensor<
  f32
  0.25
>

iex> x = Nx.tensor([[0, 1, 2], [3, 4, 5]])
iex> y = Nx.tensor([[0, 3, 4], [3, 4, 8]])
iex> Scholar.Metrics.Similarity.jaccard(x, y, axis: 1)
#Nx.Tensor<
  f32[2]
  [0.20000000298023224, 0.5]
>

iex> x = Nx.tensor([1, 2])
iex> y = Nx.tensor([1, 2, 3])
iex> Scholar.Metrics.Similarity.jaccard(x, y)
#Nx.Tensor<
  f32
  0.6666666865348816
>