简体   繁体   English

如何在 arrays 的列表(数组)中找到最频繁的(n 个元素)组合

[英]How to find the most Frequent (n-elements) Combinations in list(array) of arrays

I'm trying to find out a way to list the most frequent combinations in a list of arrays.我试图找到一种方法来列出 arrays 列表中最常见的组合。 It has to be at least two elements that appears more than once in one of the arrays.它必须至少有两个元素在 arrays 之一中出现不止一次。 Then showing up the most frequents by numeric order.然后按数字顺序显示最频繁的。 Suppose I have a list of arrays and I would like to know the most common combinations.假设我有一个 arrays 列表,我想知道最常见的组合。

List=:列表=:

Array (4) ["a", "b", "c", "d"]
Array (4) ["b", "c", "d"]
Array (4) ["c", "d"]
Array (4) ["a", "b"]
Array (4) ["a", "c", "d"]

Desired Result:期望的结果:

No of occur - Elements
4 - ["c","d"]
2 - ["b","c"]; ["a","b"]; ["a","c","d"]

How can I get most commons combinations of elements in all of those arrays?如何在所有这些 arrays 中获得最常见的元素组合?

In this case it is ["c","d"], because they occur 4 times.在这种情况下,它是 ["c","d"],因为它们出现了 4 次。

Most common combinations of two elements or more in an array.数组中两个或多个元素的最常见组合。

Thanks.谢谢。

If the number of unique elements you expect is small enough, you can use this approach:如果您期望的唯一元素数量足够少,您可以使用以下方法:

  • Determine all possible combinations using the unique elements in your data set.使用数据集中的独特元素确定所有可能的组合。 In this case abcd , abc , abd , bcd , ab , etc.在这种情况下abcdabcabdbcdab等。
  • For every combination, check how many of the input arrays contain all of the combination's characters对于每个组合,检查有多少输入 arrays 包含所有组合的字符

The number of possible combinations will rapidly increase as you have more unique elements in your data (eg all characters from a to z).随着数据中有更多独特元素(例如从 a 到 z 的所有字符),可能的组合数量将迅速增加。

If that's the case but the individual lists are still small, I'd suggest to not look at all unique combinations, but use the combinations that actually appear in the data.如果是这种情况,但单个列表仍然很小,我建议不要查看所有唯一组合,而是使用实际出现在数据中的组合。

 //// Utils // Get all unique sets of a size const allSets = (xs, size) => size === 1? xs: xs.flatMap( (x, i) => allSets(xs.slice(i + 1), size - 1).map(tail => [x, ...tail])) // Get a range of ints between two values const range = (from, to) => Array.from({ length: to - from + 1 }, (_, i) => from + i); // Get all unique values from an array const uniques = xs => Array.from(new Set(xs)); //// App const input = [ ["a", "b", "c", "d"], ["b", "c", "d"], ["c", "d"], ["a", "b"], ["a", "c", "d"]]; const uniqueChars = uniques(input.flat()); const combinations = range(2, 4).flatMap(size => allSets(uniqueChars, size)); // Create sets so we can use.has instead of.includes const inputSets = input.map(xs => new Set(xs)); // For all combinations, check how many sets include all of the characters const combinationScores = combinations.map(combo => [ // Check how many sets contain all the chars of this combo inputSets.filter(s => combo.every(x => s.has(x))).length, // Join the set for easy logging combo.join("") ]) // Sort by appearance count.sort( ([c1], [c2]) => c2 - c1); // Print to console console.log(combinationScores.join("\n"));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM