Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add binary based jaccard similarity #36

Merged
merged 3 commits into from
Sep 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions lib/scholar/metrics/similarity.ex
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,64 @@ defmodule Scholar.Metrics.Similarity do
intersection_size / union_size
end

@doc ~S"""
Calculates Jaccard similarity based on binary attributes.

$$
J = \frac{M_{11}}{M_{01} + M_{10} + M_{11}}
$$

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

## 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])
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}
"""
defn binary_jaccard(x, y) do
# We're requiring the same shape because usual use cases will have the same shape.
# The last axis could in theory be different on both sides.
assert_same_shape!(x, y)

m11 =
x
|> Nx.logical_and(y)
|> Nx.sum()

m10 =
x
|> Nx.greater(y)
|> Nx.sum()

m01 =
x
|> Nx.less(y)
|> Nx.sum()

m11 / (m11 + m10 + m01)
end

defnp unique_size(%Nx.Tensor{shape: shape} = tensor) do
case shape do
{} ->
Expand Down
62 changes: 62 additions & 0 deletions test/scholar/metrics/similarity_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,66 @@ defmodule Scholar.Metrics.SimilarityTest do
end
end
end

describe "binary_jaccard/2" do
test "returns similarity according to sklearn jaccard_score function" do
x = Nx.tensor([1, 0, 0, 1, 1, 1])
y = Nx.tensor([0, 0, 1, 1, 1, 0])

assert Similarity.binary_jaccard(x, y) == Nx.tensor(0.4000000059604645)
end

test "returns 100% of similarity" do
x = Nx.tensor([1, 0, 1])
y = Nx.tensor([1, 0, 1])

assert Similarity.binary_jaccard(x, y) == Nx.tensor(1.0)
end

test "returns 0% of similarity" do
x = Nx.tensor([1, 1, 1])
y = Nx.tensor([0, 0, 0])

assert Similarity.binary_jaccard(x, y) == Nx.tensor(0.0)
end

test "returns 20% of similarity" do
x = Nx.tensor([1, 0, 1, 0, 1])
y = Nx.tensor([0, 1, 1, 1, 0])

assert Similarity.binary_jaccard(x, y) == Nx.tensor(0.20000000298023224)
end

test "returns similarity when tensors have a single element" do
x = Nx.tensor([1])
y = Nx.tensor([1])

assert Similarity.binary_jaccard(x, y) == Nx.tensor(1.0)
end

test "returns similarity when tensors have scalars" do
x = Nx.tensor(1)
y = Nx.tensor(0)

assert Similarity.binary_jaccard(x, y) == Nx.tensor(0.0)
end

test "returns similarity when tensor has multiple dimensions" do
x = Nx.tensor([[0, 1, 1], [1, 1, 1]])
y = Nx.tensor([[1, 1, 1], [1, 1, 1]])

assert Similarity.binary_jaccard(x, y) == Nx.tensor(0.8333333134651184)
end

test "raises exception when tensors have different shapes" do
x = Nx.tensor([1, 1, 0])
y = Nx.tensor([1, 0])

assert_raise ArgumentError,
"expected tensor to have shape {3}, got tensor with shape {2}",
fn ->
Similarity.binary_jaccard(x, y)
end
end
end
end