怎么用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 },
推荐阅读
- pdf怎么扫描到cad,pdf扫描文档怎么转换成cad
- 微信首页怎么能看到视频号,怎么能看到自己微信是哪年的
- hbasejps不全,hbase为啥快
- 精灵养成类单机游戏,精灵养成类单机游戏推荐
- 动态显示日期代码java java中如何实现日期类
- 淘宝直播去邀什么,淘宝直播邀请好友怎么获得奖励
- thinkphp缓存更新,thinkphp323升级 5024
- python中文映射函数 python 映射函数
- erp系统采购管理感受,erp采购心得体会