我正在寻找像Apriori这样的算法,但是需要订购。
我需要一个算法来找到最频繁的模式。 例如:ABCDE ABC ACBD DECF
最常见的模式:ABC
顺序非常重要。 ACB不应该被看作是因为ACB在我的例子中只有一次,但ABC是三次。
有没有算法,或者是我自己编写代码的最佳解决方案?
感谢帮助。
i'm searching for an algorithm like Apriori but with order.
I need an algorithm to find most frequent pattern. Example: A B C D E A B C A C B D D E C F
the most frequent pattern: A B C
The order is very important. A C B should not be viewed because A C B is only one times in my example, but A B C three times.
Is there an algorithm or is the best solution to code it by my own?
Thanks for help.
最满意答案
您可以扫描序列并将其存储在map
String sequence = "ABCDEABCACBDDECF"; char[] chars = sequence.toCharArray(); int i = 0; Map<String,Integer> map = new HashMap<String,Integer>(); while(i < chars.length - 2){ String pattern = "" + chars[i] + chars[i+1] + chars[i+2]; System.out.println(pattern); Integer population = map.get(pattern); if(population == null){ population = 1; }else{ population++; } map.put(pattern, population); i++; } System.out.println(map);输出是
{ACB=1, DEA=1, BCA=1, ABC=2, BCD=1, BDD=1, DEC=1, CAC=1, CBD=1, DDE=1, CDE=1, EAB=1, ECF=1}您可以轻松地按值排序地图: http : //www.programcreek.com/2013/03/java-sort-map-by-value/或者只是扫描地图寻找最高人口
顺便说一句:模式ABC人口其实是2而不是3
You can scan the sequence and store populations in a map
String sequence = "ABCDEABCACBDDECF"; char[] chars = sequence.toCharArray(); int i = 0; Map<String,Integer> map = new HashMap<String,Integer>(); while(i < chars.length - 2){ String pattern = "" + chars[i] + chars[i+1] + chars[i+2]; System.out.println(pattern); Integer population = map.get(pattern); if(population == null){ population = 1; }else{ population++; } map.put(pattern, population); i++; } System.out.println(map);The output is
{ACB=1, DEA=1, BCA=1, ABC=2, BCD=1, BDD=1, DEC=1, CAC=1, CBD=1, DDE=1, CDE=1, EAB=1, ECF=1}You can easily sort the map by values : http://www.programcreek.com/2013/03/java-sort-map-by-value/ or just scan the map looking for the highest population
By the way: population of pattern ABC actually is 2 not 3
订单最频繁的模式(Most frequent pattern with an order)我正在寻找像Apriori这样的算法,但是需要订购。
我需要一个算法来找到最频繁的模式。 例如:ABCDE ABC ACBD DECF
最常见的模式:ABC
顺序非常重要。 ACB不应该被看作是因为ACB在我的例子中只有一次,但ABC是三次。
有没有算法,或者是我自己编写代码的最佳解决方案?
感谢帮助。
i'm searching for an algorithm like Apriori but with order.
I need an algorithm to find most frequent pattern. Example: A B C D E A B C A C B D D E C F
the most frequent pattern: A B C
The order is very important. A C B should not be viewed because A C B is only one times in my example, but A B C three times.
Is there an algorithm or is the best solution to code it by my own?
Thanks for help.
最满意答案
您可以扫描序列并将其存储在map
String sequence = "ABCDEABCACBDDECF"; char[] chars = sequence.toCharArray(); int i = 0; Map<String,Integer> map = new HashMap<String,Integer>(); while(i < chars.length - 2){ String pattern = "" + chars[i] + chars[i+1] + chars[i+2]; System.out.println(pattern); Integer population = map.get(pattern); if(population == null){ population = 1; }else{ population++; } map.put(pattern, population); i++; } System.out.println(map);输出是
{ACB=1, DEA=1, BCA=1, ABC=2, BCD=1, BDD=1, DEC=1, CAC=1, CBD=1, DDE=1, CDE=1, EAB=1, ECF=1}您可以轻松地按值排序地图: http : //www.programcreek.com/2013/03/java-sort-map-by-value/或者只是扫描地图寻找最高人口
顺便说一句:模式ABC人口其实是2而不是3
You can scan the sequence and store populations in a map
String sequence = "ABCDEABCACBDDECF"; char[] chars = sequence.toCharArray(); int i = 0; Map<String,Integer> map = new HashMap<String,Integer>(); while(i < chars.length - 2){ String pattern = "" + chars[i] + chars[i+1] + chars[i+2]; System.out.println(pattern); Integer population = map.get(pattern); if(population == null){ population = 1; }else{ population++; } map.put(pattern, population); i++; } System.out.println(map);The output is
{ACB=1, DEA=1, BCA=1, ABC=2, BCD=1, BDD=1, DEC=1, CAC=1, CBD=1, DDE=1, CDE=1, EAB=1, ECF=1}You can easily sort the map by values : http://www.programcreek.com/2013/03/java-sort-map-by-value/ or just scan the map looking for the highest population
By the way: population of pattern ABC actually is 2 not 3
发布评论