mirror of
https://github.com/sigmasternchen/gleam-community-maths
synced 2025-03-15 07:59:01 +00:00
Add overlap coefficient
This commit is contained in:
parent
d7b4841c42
commit
c825bb522f
2 changed files with 57 additions and 0 deletions
|
@ -49,6 +49,7 @@ import gleam/list
|
|||
import gleam/pair
|
||||
import gleam/set
|
||||
import gleam/float
|
||||
import gleam/int
|
||||
|
||||
/// <div style="text-align: right;">
|
||||
/// <a href="https://github.com/gleam-community/maths/issues">
|
||||
|
@ -676,3 +677,37 @@ pub fn tversky_index(
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <div style="text-align: right;">
|
||||
/// <a href="https://github.com/gleam-community/maths/issues">
|
||||
/// <small>Spot a typo? Open an issue!</small>
|
||||
/// </a>
|
||||
/// </div>
|
||||
///
|
||||
///
|
||||
/// <details>
|
||||
/// <summary>Example:</summary>
|
||||
///
|
||||
/// import gleeunit/should
|
||||
/// import gleam_community/maths/metrics
|
||||
///
|
||||
/// pub fn example () {
|
||||
/// }
|
||||
/// </details>
|
||||
///
|
||||
/// <div style="text-align: right;">
|
||||
/// <a href="#">
|
||||
/// <small>Back to top ↑</small>
|
||||
/// </a>
|
||||
/// </div>
|
||||
///
|
||||
pub fn overlap_coefficient(aset: set.Set(a), bset: set.Set(a)) -> Float {
|
||||
let intersection: Float =
|
||||
set.intersection(aset, bset)
|
||||
|> set.size()
|
||||
|> conversion.int_to_float()
|
||||
let minsize: Float =
|
||||
piecewise.minimum(set.size(aset), set.size(bset), int.compare)
|
||||
|> conversion.int_to_float()
|
||||
intersection /. minsize
|
||||
}
|
||||
|
|
|
@ -234,3 +234,25 @@ pub fn example_jaccard_index_test() {
|
|||
metrics.jaccard_index(set_e, set_f)
|
||||
|> should.equal(1.0 /. 7.0)
|
||||
}
|
||||
|
||||
pub fn example_overlap_coefficient_test() {
|
||||
metrics.overlap_coefficient(set.from_list([]), set.from_list([]))
|
||||
|> should.equal(0.0)
|
||||
|
||||
let set_a: set.Set(Int) = set.from_list([0, 1, 2, 5, 6, 8, 9])
|
||||
let set_b: set.Set(Int) = set.from_list([0, 2, 3, 4, 5, 7, 9])
|
||||
metrics.overlap_coefficient(set_a, set_b)
|
||||
|> should.equal(4.0 /. 7.0)
|
||||
|
||||
let set_c: set.Set(Int) = set.from_list([0, 1, 2, 3, 4, 5])
|
||||
let set_d: set.Set(Int) = set.from_list([6, 7, 8, 9, 10])
|
||||
metrics.overlap_coefficient(set_c, set_d)
|
||||
|> should.equal(0.0 /. 5.0)
|
||||
|
||||
let set_e: set.Set(String) =
|
||||
set.from_list(["cat", "dog", "hippo", "monkey", "rhino"])
|
||||
let set_f: set.Set(String) =
|
||||
set.from_list(["monkey", "rhino", "ostrich", "salmon"])
|
||||
metrics.overlap_coefficient(set_e, set_f)
|
||||
|> should.equal(2.0 /. 4.0)
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue