关联算法增量实现原理

 

 

public class Test {

    /**
     * 打印一个数组所有的非空子集
     */
    public List<String> printAllSubsets(Integer[] array) {
        if (null == array || 0 == array.length) {
            throw new IllegalArgumentException("数组不能为Null,至少有一个元素");
        }
        Arrays.sort(array);  //进行排序
        int len = array.length;
        List<String> stringList = new LinkedList<String>();
        int allMasks = 1 << len;
        // 遍历所有的二进制表示方式
        for (int i = 1; i < allMasks; i++) {
            //if (i == allMasks - 1) break;
            StringBuilder s = new StringBuilder();
            for (int j = 0; j < len; j++)
                if ((i & (1 << j)) > 0) {
                    s.append(array[j]+ "|");
                }
            stringList.add(s.toString());
        }
        return stringList;
    }

    public static void main(String[] args) {

        Test exam = new Test();
        //三个订单
        List<String> stringList = exam.printAllSubsets(new Integer[]{1,3,2,4});
        List<String> stringList2 = exam.printAllSubsets(new Integer[]{1,3,2});
        List<String> stringList3 = exam.printAllSubsets(new Integer[]{1,4,3});
        //候选项集,出现次数,每天增量更新没有加入支持度限制(实际中排列组合会很多数据量巨大,根据每天的订单增量计算)
        Map<String,Integer> map=new HashMap<String, Integer>();//模拟数据库


        for (String s : stringList) {
            System.out.println(s);
            if(!map.containsKey(s)) {
                map.put(s, 1);
            }else
            {
                map.put(s,map.get(s)+1);
            }
        }
        for (String s : stringList2) {
            System.out.println(s);
            if(!map.containsKey(s)) {
                map.put(s, 1);
            }else
            {
                map.put(s,map.get(s)+1);
            }
        }
        for (String s : stringList3) {
            System.out.println(s);
            if(!map.containsKey(s)) {
                map.put(s, 1);
            }else
            {
                map.put(s,map.get(s)+1);
            }
        }

        float support = map.get("1|2|");
        System.out.println("支持度"+support);

        float confidence = map.get("1|3|")*100/map.get("3|");
        System.out.println(confidence+"%的用户购买了3|还购买了1|");
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值