用java实现a算法代码 java实现apriori算法

怎么用java实现apriori算法作者:何史提
链接:
来源:知乎
著作权归作者所有 。商业转载请联系作者获得授权用java实现a算法代码,非商业转载请注明出处 。
Apriori算法用java实现a算法代码的理念其实很简单,可是实现起上来却复杂无比 , 因为当中无可避免用Set和Hash Table等高阶用java实现a算法代码的数据结构 , 而且有很多loop用以读取数据 。
用java实现a算法代码我不建议用Java,应改用Python或Scala一类的语言 。如果用Python,代码大概50行左右,但可以想像用Java便看起来复杂得多 。看如下:
from operator import and_
from itertools import combinations
【用java实现a算法代码 java实现apriori算法】class AprioriAssociationRule:
def __init__(self, inputfile):
self.transactions = []
self.itemSet = set([])
inf = open(inputfile, 'rb')
for line in inf.readlines():
elements = set(filter(lambda entry: len(entry)0, line.strip().split(',')))
if len(elements)0:
self.transactions.append(elements)
for element in elements:
self.itemSet.add(element)
inf.close()
self.toRetItems = {}
self.associationRules = []
def getSupport(self, itemcomb):
if type(itemcomb) != frozenset:
itemcomb = frozenset([itemcomb])
within_transaction = lambda transaction: reduce(and_, [(item in transaction) for item in itemcomb])
count = len(filter(within_transaction, self.transactions))
return float(count)/float(len(self.transactions))
def runApriori(self, minSupport=0.15, minConfidence=0.6):
itemCombSupports = filter(lambda freqpair: freqpair[1]=minSupport,
map(lambda item: (frozenset([item]), self.getSupport(item)), self.itemSet))
currentLset = set(map(lambda freqpair: freqpair[0], itemCombSupports))
k = 2
while len(currentLset)0:
currentCset = set([i.union(j) for i in currentLset for j in currentLset if len(i.union(j))==k])
currentItemCombSupports = filter(lambda freqpair: freqpair[1]=minSupport,
map(lambda item: (item, self.getSupport(item)), currentCset))
currentLset = set(map(lambda freqpair: freqpair[0], currentItemCombSupports))
itemCombSupports.extend(currentItemCombSupports)
k += 1
for key, supportVal in itemCombSupports:
self.toRetItems[key] = supportVal
self.calculateAssociationRules(minConfidence=minConfidence)
def calculateAssociationRules(self, minConfidence=0.6):
for key in self.toRetItems:
subsets = [frozenset(item) for k in range(1, len(key)) for item in combinations(key, k)]
for subset in subsets:
confidence = self.toRetItems[key] / self.toRetItems[subset]
if confidenceminConfidence:
self.associationRules.append([subset, key-subset, confidence])
用JAVA写一个a,b,c,d,e排列组合算法,谢谢了public class Paixu {
public static void main(String[] args) {
char[] in = "abcde".toCharArray();
new Paixu().paixu(in, in.length, 0);
}
private void paixu(char[] array, int n, int k) {
if (n == k) {
char[] out = new char[n];
for (int i = 0; iarray.length; i++) {
out[i] = array[i];
}
System.out.println(new String(out));
} else {
for (int i = k; in; i++) {
swap(array, k, i);
paixu(array, n, k + 1);
swap(array, i, k);
}
}
}
private void swap(char[] a, int x, int y) {
char temp = a[x];
a[x] = a[y];
a[y] = temp;
}
}
A*算法java实现代码实现(Java)
1. 输入
(1) 代表地图二值二维数组(0表示可通路,1表示路障)
int[][] maps = {
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0 },
{ 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0 },

推荐阅读