Java 迭代 apriori算法

 

 

http://download.csdn.net/detail/huangyongjie986123/3976324

 

http://blog.csdn.net/wangyunyun00/article/details/38273213

 

http://blog.sina.com.cn/s/blog_6efce07e0101309a.html

 

https://zhidao.baidu.com/question/40651338.html

 

http://cxl2012.iteye.com/blog/2059218

 

http://www.cnblogs.com/amboyna/archive/2007/09/25/904804.html

 

http://www.tceic.com/3j1695j3312h3169g551g448.html

 

http://www.cnblogs.com/selene/p/5844686.html

 

 

http://blog.csdn.net/u010498696/article/details/45641719  apriori算法代码Java

 

 http://blog.sina.com.cn/s/blog_e7fb9d080102v9o0.html  java 离散数学 http://blog.163.com/xiaohui_1123@126/blog/static/3980524020109784356915/求集合的子集算法 

 http://blog.csdn.net/foreverbu/article/details/37567111   http://www.cnblogs.com/lixusign/archive/2012/06/13/2548124.html决策树算法    http://www.xqbase.com/computer/search_iterative.htm   迭代算法 http://blog.csdn.net/lg1259156776/article/details/48340829

 

 

 

 

 

 

 

===============================================

 

package com.cxl.algorithm;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;

public class AprioriMyself
{

 private static final double MIN_SUPPROT = 0.2;// 最小支持度
 private static boolean endTag = false;// 循环状态
  static List<List<String>> record = new ArrayList<List<String>>();// 数据集

 public static void main(String args[])
 {

  try{
  String encoding = "GBK"; // 字符编码(可解决中文乱码问题 )
  File file = new File("simple.txt");
  if (file.isFile() && file.exists())
  {
   InputStreamReader read = new InputStreamReader(new FileInputStream(file), encoding);
  
   BufferedReader bufferedReader = new BufferedReader(read);
   
   String lineTXT = null;
   
   while ((lineTXT = bufferedReader.readLine()) != null)
   {
    // 读一行文件
    String[] lineString = lineTXT.split("      ");
    
    List<String> lineList = new ArrayList<String>();
    
    for (int i = 0; i < lineString.length; i++)
    {        
     lineList.add(lineString[i]);          
    }
    System.out.println("数组输出:"+lineTXT);
    
    System.out.println("list集合输出:");
     
  
     Iterator i = lineList.iterator();

     while( i.hasNext() )
         {
          String  me =   (String) i.next();
          System.out.print( me +"  " );       

         }
     System.out.println( "  " );
    
   }  
    
  }
 } catch (Exception e)
 {
  System.out.println("读取文件内容操作出错");
  e.printStackTrace();
 }
}

  /**
   * 读取txt数据
   *
   * @return
   */
  /*
  List<List<String>> record = new ArrayList<List<String>>();

  try
  {
   String encoding = "GBK"; // 字符编码(可解决中文乱码问题 )
   File file = new File("simple.txt");
   if (file.isFile() && file.exists())
   {
    InputStreamReader read = new InputStreamReader(new FileInputStream(file), encoding);
    BufferedReader bufferedReader = new BufferedReader(read);
    String lineTXT = null;
    while ((lineTXT = bufferedReader.readLine()) != null)
    {// 读一行文件
     String[] lineString = lineTXT.split("      ");
     List<String> lineList = new ArrayList<String>();
     for (int i = 0; i < lineString.length; i++)
     {// 处理矩阵中的T、F、YES、NO
      if (lineString[i].endsWith("T") || lineString[i].endsWith("YES"))      
       
       lineList.add(record.get(0).get(i));
      
      else if (lineString[i].endsWith("F") || lineString[i].endsWith("NO"))
       ;// F,NO记录不保存
      else
       lineList.add(lineString[i]);
     }

      Iterator i = lineList.iterator();

      while( i.hasNext() )
          {
           String  me =   (String) i.next();
           System.out.print( me +"  " );       

          }
      System.out.println( "  " );
     record.add(lineList);
    }
    read.close();
   } else
   {
    System.out.println("找不到指定的文件!");
   }
  } catch (Exception e)
  {
   System.out.println("读取文件内容操作出错");
   e.printStackTrace();
  }
         */
 
  

 

以上代码输出:

数组输出:TID      I1      I2      I3      I4      I5
list集合输出:
TID  I1  I2  I3  I4  I5   
数组输出:T100      T      T      F      F      T
list集合输出:
T100  T  T  F  F  T   
数组输出:T200      F      T      F      T      F
list集合输出:
T200  F  T  F  T  F   
数组输出:T300      F      T      T      F      F
list集合输出:
T300  F  T  T  F  F   
数组输出:T400      T      T      F      T      F
list集合输出:
T400  T  T  F  T  F   
数组输出:T500      T      F      T      F      F
list集合输出:
T500  T  F  T  F  F   
数组输出:T600      F      T      T      F      F
list集合输出:
T600  F  T  T  F  F   
数组输出:T700      T      F      T      F      F
list集合输出:
T700  T  F  T  F  F   
数组输出:T800      T      T      T      F      T
list集合输出:
T800  T  T  T  F  T   
数组输出:T900      T      T      T      F      F
list集合输出:
T900  T  T  T  F  F   

=================================

package com.cxl.algorithm;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;

public class AprioriMyself
{

 private static final double MIN_SUPPROT = 0.2;// 最小支持度
 private static boolean endTag = false;// 循环状态
  static List<List<String>> record = new ArrayList<List<String>>();// 数据集

 public static void main(String args[])
 {

  try{
  String encoding = "GBK"; // 字符编码(可解决中文乱码问题 )
  File file = new File("simple.txt");
  if (file.isFile() && file.exists())
  {
   InputStreamReader read = new InputStreamReader(new FileInputStream(file), encoding);
  
   BufferedReader bufferedReader = new BufferedReader(read);
   
   String lineTXT = null;
   
   
   
   while ((lineTXT = bufferedReader.readLine()) != null)
   {
    System.out.println("数组输出:"+lineTXT);
    // 读一行文件
    String[] lineString = lineTXT.split("      ");
            
    List<String> lineList = new ArrayList<String>();
        
    for (int i = 0; i < lineString.length; i++)
    {  
     if(lineString[i].equals("T") )
     {
      lineList.add(record.get(0).get(i));  
     }
     else if(lineString[i].equals("F"))
     {
      ;
     }
     else  lineList.add(lineString[i]);          
    }
    
    record.add(lineList);
    
    
    System.out.println("list集合输出:");
     
  
     Iterator i = lineList.iterator();

     while( i.hasNext() )
         {
          String  me =   (String) i.next();
          System.out.print( me +"  " );       

         }
     System.out.println( "  " );
    
   }  
    
  }
 } catch (Exception e)
 {
  System.out.println("读取文件内容操作出错");
  e.printStackTrace();
 }
}

 

代码输出:

数组输出:TID      I1      I2      I3      I4      I5
list集合输出:
TID  I1  I2  I3  I4  I5   
数组输出:T100      T      T      F      F      T
list集合输出:
T100  I1  I2  I5   
数组输出:T200      F      T      F      T      F
list集合输出:
T200  I2  I4   
数组输出:T300      F      T      T      F      F
list集合输出:
T300  I2  I3   
数组输出:T400      T      T      F      T      F
list集合输出:
T400  I1  I2  I4   
数组输出:T500      T      F      T      F      F
list集合输出:
T500  I1  I3   
数组输出:T600      F      T      T      F      F
list集合输出:
T600  I2  I3   
数组输出:T700      T      F      T      F      F
list集合输出:
T700  I1  I3   
数组输出:T800      T      T      T      F      T
list集合输出:
T800  I1  I2  I3  I5   
数组输出:T900      T      T      T      F      F
list集合输出:
T900  I1  I2  I3   

 

===================================

 

 

package com.cxl.algorithm;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;

public class AprioriMyself
{

 private static final double MIN_SUPPROT = 0.2;// 最小支持度
 private static boolean endTag = false;// 循环状态
 static List<List<String>> record = new ArrayList<List<String>>();// 数据集

 public static void main(String args[])
 {

  try
  {
   String encoding = "GBK"; // 字符编码(可解决中文乱码问题 )
   File file = new File("simple.txt");
   if (file.isFile() && file.exists())
   {
    InputStreamReader read = new InputStreamReader(new FileInputStream(file), encoding);

    BufferedReader bufferedReader = new BufferedReader(read);

    String lineTXT = null;

    while ((lineTXT = bufferedReader.readLine()) != null)
    {
     System.out.println("数组输出:" + lineTXT);
     // 读一行文件
     String[] lineString = lineTXT.split("      ");

     List<String> lineList = new ArrayList<String>();

     for (int i = 0; i < lineString.length; i++)
     {
      if (lineString[i].equals("T"))
      {
       lineList.add(record.get(0).get(i));
      } else if (lineString[i].equals("F"))
      {
       ;
      } else
       lineList.add(lineString[i]);
     }

     record.add(lineList);

     System.out.println("list集合输出:");

     Iterator i = lineList.iterator();

     while (i.hasNext())
     {
      String me = (String) i.next();
      System.out.print(me + "  ");

     }
     System.out.println("  ");

    }

   }
  } catch (Exception e)
  {
   System.out.println("读取文件内容操作出错");
   e.printStackTrace();
  }

  List<List<String>> record2 = new ArrayList<List<String>>();// 数据集

  record2 = record;

  for (int w = 1; w < record2.size(); w++)
  {

   int count = 0;
   for (int i = 1; i < record.size(); i++)
   {

    boolean notHaveThisList = false;

    for (int k = 1; k < record2.get(w).size(); k++)
    {// 判断record.get(i)是否包含list
     boolean thisRecordHave = false;
     for (int j = 1; j < record.get(i).size(); j++)
     {
      if (record2.get(w).get(k).equals(record.get(i).get(j))) // list。get(k)在record。get(i)中能找到
       thisRecordHave = true;
     }
     if (!thisRecordHave)
     {
      // 只要有一个list元素找不到,则退出其余元素比较,进行下一个record。get(i)比较
      notHaveThisList = true;
      break;
     }
    }

    if (notHaveThisList == false)
     count++;
    

   }
   
   System.out.println(count);  //输出每个集合在总集合中的包含个数,这本书重要。
  }

 }

}

 

 

输出结果:

数组输出:TID      I1      I2      I3      I4      I5
list集合输出:
TID  I1  I2  I3  I4  I5   
数组输出:T100      T      T      F      F      T
list集合输出:
T100  I1  I2  I5   
数组输出:T200      F      T      F      T      F
list集合输出:
T200  I2  I4   
数组输出:T300      F      T      T      F      F
list集合输出:
T300  I2  I3   
数组输出:T400      T      T      F      T      F
list集合输出:
T400  I1  I2  I4   
数组输出:T500      T      F      T      F      F
list集合输出:
T500  I1  I3   
数组输出:T600      F      T      T      F      F
list集合输出:
T600  I2  I3   
数组输出:T700      T      F      T      F      F
list集合输出:
T700  I1  I3   
数组输出:T800      T      T      T      F      T
list集合输出:
T800  I1  I2  I3  I5   
数组输出:T900      T      T      T      F      F
list集合输出:
T900  I1  I2  I3   
2
2
4
1
4
4
4
1
2

 

 ============================================

  public static void main(String[] args) {
       
       
        byte tmp=1<<4;
        System.out.println(tmp );
       
        for( byte b1 = 0; b1<16 ;b1++)
        {
       for(byte i=0; i<8; i++) {   //System.out.print(number &(tmp>>>=1));
       
       System.out.print( (b1 & (tmp>>>=1) ) > 0 ? 1 : 0 );
      }
        System.out.println("下一个元素是:" );
        tmp=1<<4;
        }
      

       }

 

 

输出结果:16
00000000下一个元素是:
00010000下一个元素是:
00100000下一个元素是:
00110000下一个元素是:
01000000下一个元素是:
01010000下一个元素是:
01100000下一个元素是:
01110000下一个元素是:
10000000下一个元素是:
10010000下一个元素是:
10100000下一个元素是:
10110000下一个元素是:
11000000下一个元素是:
11010000下一个元素是:
11100000下一个元素是:
11110000下一个元素是:

 

=============================

 

 

 

 

package com.cxl.algorithm;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Scanner;

public class AprioriMyself

       public static void main(String[] args) {
       
        List<String> record = new ArrayList<String>();
       
      String []a = {"A","B","C","D"};
      String b="" ;
        byte tmp=1<<4;
        System.out.println(tmp );
               
        for( byte b1 = 0; b1<16 ;b1++)
        {
       for(byte i=0; i<8; i++) {  
        
        if(i<4)
        {          
         int ii =(( b1 & (tmp>>>=1) ) > 0 ? 1 : 0 );         
         if(ii!=1)
          b=b+a[i];
         if(i==3) record.add(b);
        }               
      }
       
       b="";
       record.add("**********"); //前后每个子集元素 的分割符号      
       tmp=1<<4;
        }
      
        for(int i =0  ; i<record.size(); i++)
        {        
         System.out.println( record.get(i));                        
        }           
       }
}

 输出:

16
ABCD
**********
ABC
**********
ABD
**********
AB
**********
ACD
**********
AC
**********
AD
**********
A
**********
BCD
**********
BC
**********
BD
**********
B
**********
CD
**********
C
**********
D
**********

**********

 我花了一天的时间研究 —— 求 一个4元素集合的所有子集 并输出 。

哈哈哈,原理搞通了,

如果你看到这篇文章了,  你可以继续改进代码, 求 任意集合 的所有子集  。

 

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是用 Java 实现 Apriori 算法的示例代码: ```java import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; public class Apriori { public static void main(String[] args) { // 构造事务数据库 List<List<String>> transactions = new ArrayList<>(); transactions.add(new ArrayList<String>() {{ add("A"); add("B"); add("C"); }}); transactions.add(new ArrayList<String>() {{ add("A"); add("C"); add("D"); }}); transactions.add(new ArrayList<String>() {{ add("B"); add("C"); add("E"); }}); transactions.add(new ArrayList<String>() {{ add("A"); add("C"); add("D"); }}); transactions.add(new ArrayList<String>() {{ add("B"); add("E"); }}); // 设置支持度阈值和置信度阈值 double minSupport = 0.4; double minConfidence = 0.6; // 调用 Apriori 算法进行频繁项集和关联规则挖掘 Map<List<String>, Double> frequentItemsets = apriori(transactions, minSupport); List<AssociationRule<String>> associationRules = generateAssociationRules(frequentItemsets, minConfidence); // 输出结果 System.out.println("频繁项集:"); for (Map.Entry<List<String>, Double> entry : frequentItemsets.entrySet()) { System.out.println(entry.getKey() + " : " + entry.getValue()); } System.out.println("关联规则:"); for (AssociationRule<String> rule : associationRules) { System.out.println(rule); } } /** * Apriori 算法 * * @param transactions 事务数据库 * @param minSupport 支持度阈值 * @return 频繁项集及其支持度 */ public static Map<List<String>, Double> apriori(List<List<String>> transactions, double minSupport) { // 计算候选频繁项集的支持度 Map<List<String>, Double> candidateItemsets = new HashMap<>(); for (List<String> transaction : transactions) { for (String item : transaction) { List<String> itemset = new ArrayList<String>() {{ add(item); }}; if (candidateItemsets.containsKey(itemset)) { candidateItemsets.put(itemset, candidateItemsets.get(itemset) + 1); } else { candidateItemsets.put(itemset, 1.0); } } } // 迭代计算频繁项集 Map<List<String>, Double> frequentItemsets = new HashMap<>(); while (!candidateItemsets.isEmpty()) { // 过滤掉支持度小于阈值的候选频繁项集 candidateItemsets.entrySet().removeIf(entry -> entry.getValue() / transactions.size() < minSupport); // 将剩余的候选频繁项集加入频繁项集列表 frequentItemsets.putAll(candidateItemsets); // 生成下一层的候选频繁项集 candidateItemsets = generateCandidateItemsets(candidateItemsets.keySet()); for (List<String> transaction : transactions) { for (List<String> candidateItemset : candidateItemsets.keySet()) { if (transaction.containsAll(candidateItemset)) { candidateItemsets.put(candidateItemset, candidateItemsets.get(candidateItemset) + 1); } } } } return frequentItemsets; } /** * 生成下一层的候选频繁项集 * * @param itemsets 上一层的频繁项集 * @return 下一层的候选频繁项集 */ private static Map<List<String>, Double> generateCandidateItemsets(Set<List<String>> itemsets) { Map<List<String>, Double> candidateItemsets = new HashMap<>(); for (List<String> itemset1 : itemsets) { for (List<String> itemset2 : itemsets) { if (itemset1.equals(itemset2)) { continue; } List<String> newItemset = new ArrayList<>(itemset1); newItemset.addAll(itemset2); Collections.sort(newItemset); if (!candidateItemsets.containsKey(newItemset)) { candidateItemsets.put(newItemset, 0.0); } } } return candidateItemsets; } /** * 生成关联规则 * * @param frequentItemsets 频繁项集及其支持度 * @param minConfidence 置信度阈值 * @return 关联规则 */ public static List<AssociationRule<String>> generateAssociationRules(Map<List<String>, Double> frequentItemsets, double minConfidence) { List<AssociationRule<String>> associationRules = new ArrayList<>(); for (Map.Entry<List<String>, Double> entry : frequentItemsets.entrySet()) { List<String> itemset = entry.getKey(); double support = entry.getValue() / transactions.size(); if (itemset.size() > 1) { for (int i = 0; i < itemset.size(); i++) { List<String> antecedent = new ArrayList<>(itemset.subList(0, i)); List<String> consequent = new ArrayList<>(itemset.subList(i, itemset.size())); double confidence = support / frequentItemsets.get(antecedent); if (confidence >= minConfidence) { associationRules.add(new AssociationRule<>(antecedent, consequent, support, confidence)); } } } } return associationRules; } /** * 关联规则 * * @param <T> 项集元素类型 */ public static class AssociationRule<T> { private List<T> antecedent; private List<T> consequent; private double support; private double confidence; public AssociationRule(List<T> antecedent, List<T> consequent, double support, double confidence) { this.antecedent = antecedent; this.consequent = consequent; this.support = support; this.confidence = confidence; } public List<T> getAntecedent() { return antecedent; } public List<T> getConsequent() { return consequent; } public double getSupport() { return support; } public double getConfidence() { return confidence; } @Override public String toString() { return antecedent + " => " + consequent + " : Support = " + support + ", Confidence = " + confidence; } } } ``` 这个示例代码中包含 Apriori 算法和关联规则生成的实现,你可以根据自己的需求进行修改和扩展。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值