Reputation: 815
I perform a pairwise comparison on a number of objects and estimate the similarity
Item-1,Item-2,Similarity
11, 16, 0.9
11, 18, 0.5
11, 21, 0.9
12, 19, 0.8
12,22, 0.9
12, 24, 0.3
I wish to now create clusters based on a threshold, for example with a threshold of 0.8 would like to create lists as below:
{11,16, 21}
{12,19,22}
with threshold > 0.8
Is there a way to do this in LINQ? I found several ways of generating pairs from lists, however I need to do it in reverse.
Upvotes: 0
Views: 540
Reputation: 532435
Assuming you have a collection of such pairs with the properties, Item1
, Item2
, and Similarity
where the first two are integers and the second a double.
var threshold = 0.8f;
var sets = pairs.Where(p => p.Similarity > threshold)
.GroupBy(p => p.Item1, p => p.Item2)
.Select(g => new [] { g.Key }.Union(g));
Upvotes: 2
Reputation: 3752
You can get the requested groups using
items.Where(i => i.Similarity >= .8).GroupBy(i => i.Item1)
and then the list is group.Key
+ group.Select(i => i.Item2)
Upvotes: 1