Apriori 购物栏挖掘算法的C#实现。原创代码

c# 代码
c# 代码
  1. using System;   
  2. using System.Collections.Generic;   
  3. using System.Text;   
  4. using System.Collections;   
  5.   
  6.     
  7.     class Program   
  8.     {   
  9.         static void Main(string[] args)   
  10.         {   
  11.             string file = @"c:\test.csv";   
  12.             string sup = "2";   
  13.             if (args.Length > 0) {   
  14.                 file = args[0];   
  15.                    
  16.             }   
  17.             if (args.Length== 2)   
  18.             {   
  19.                 sup= args[1];   
  20.   
  21.             }   
  22.   
  23.   
  24.             double support = double.Parse(sup);   
  25.   
  26.             CSVReader cr = new CSVReader();   
  27.             ItemSet data = cr.Read(file);   
  28.   
  29.   
  30.                
  31.             Program p = new Program();   
  32.             ItemSet a= p.apriori( data, support);   
  33.             for (int i = 0; i < a.Count;i++ )   
  34.             {   
  35.                 ItemSet cur = (ItemSet)a[i];   
  36.                 for (int j = 0; j < cur.Count; j++) {   
  37.                     ItemSet now = (ItemSet)cur[j];   
  38.                     foreach (DataItem item in now)   
  39.                     {   
  40.   
  41.                         Console.Write("编号" + item.Id + ":" + item.ItemName+"  ");   
  42.   
  43.                            
  44.                     }   
  45.                     Console.WriteLine("  支持度:"+now.ICount);   
  46.                 }   
  47.   
  48.             }   
  49.               
  50.             Console.Read();   
  51.         }   
  52.   
  53.         private ItemSet FindOneColSet(ItemSet data, double support)   
  54.         {   
  55.             ItemSet cur=null;   
  56.             ItemSet result = new ItemSet();   
  57.   
  58.             ItemSet set=null;   
  59.             ItemSet newset=null;   
  60.             DataItem cd=null;   
  61.              DataItem td=null;   
  62.              bool flag = true;   
  63.   
  64.             for (int i = 0; i < data.Count; i++) {   
  65.                 cur = (ItemSet)data[i];   
  66.                 for (int j = 0; j < cur.Count; j++) {   
  67.                     cd = (DataItem)cur[j];   
  68.                        
  69.                     for (int n = 0; n < result.Count; n++) {   
  70.                         set = (ItemSet)result[n];   
  71.                         td= (DataItem)set[0];   
  72.                         if (cd.Id == td.Id)   
  73.                         {   
  74.                             set.ICount++;   
  75.                             flag = false;   
  76.                             break;   
  77.                                                                  
  78.                         }   
  79.                         flag=true;   
  80.                     }   
  81.                     if (flag) {   
  82.                         newset = new ItemSet();   
  83.                         newset.Add(cd);   
  84.                         result.Add(newset);   
  85.                         newset.ICount = 1;   
  86.                        
  87.                        
  88.                     }   
  89.   
  90.                       
  91.                        
  92.                 }   
  93.   
  94.   
  95.                    
  96.             }   
  97.             ItemSet finalResult = new ItemSet();   
  98.             for (int i = 0; i < result.Count; i++)   
  99.             {   
  100.                 ItemSet con = (ItemSet)result[i];   
  101.                 if (con.ICount >= support)   
  102.                 {   
  103.   
  104.                     finalResult.Add(con);   
  105.                 }   
  106.   
  107.   
  108.             }   
  109.             //finalResult.Sort();   
  110.             return finalResult;   
  111.      
  112.            
  113.         }   
  114.   
  115.   
  116.         private ItemSet apriori(  ItemSet data, double support)   
  117.         {   
  118.   
  119.             ItemSet result = new ItemSet();   
  120.             ItemSet li = new ItemSet();   
  121.             ItemSet conList = new ItemSet();   
  122.             ItemSet subConList = new ItemSet();   
  123.             ItemSet subDataList = new ItemSet();   
  124.             int k = 2;   
  125.             li.Add( new ItemSet());   
  126.             li.Add(this.FindOneColSet(data,support));   
  127.                
  128.             while (((ItemSet)li[k-1]).Count != 0)   
  129.             {   
  130.   
  131.                 conList = AprioriGenerate((ItemSet)li[k - 1],k-1, support);   
  132.                 for (int i = 0; i < data.Count; i++)   
  133.                 {   
  134.                     subDataList = SubSet((ItemSet)data[i], k);   
  135.                     for (int j = 0; j < subDataList.Count; j++)   
  136.                     {   
  137.                         for (int n = 0; n < conList.Count; n++)   
  138.                         {   
  139.                             ((ItemSet)subDataList[j]).Sort();   
  140.                             ((ItemSet)conList[n]).Sort();   
  141.                             if (((ItemSet)subDataList[j]).Equals(conList[n]))   
  142.                             {   
  143.                                 ((ItemSet)conList[n]).ICount++;   
  144.   
  145.                             }   
  146.                         }   
  147.                          
  148.                     }   
  149.   
  150.                 }   
  151.   
  152.                 li.Add(new ItemSet());   
  153.                 for (int i = 0; i < conList.Count; i++)   
  154.                 {   
  155.                     ItemSet con = (ItemSet)conList[i];   
  156.                     if (con.ICount >= support)   
  157.                     {   
  158.                             
  159.                         ((ItemSet)li[k]).Add(con);   
  160.                     }   
  161.   
  162.   
  163.                 }   
  164.                  
  165.                 k++;   
  166.             }   
  167.             for (int i = 0; i < li.Count; i++)   
  168.             {   
  169.                    
  170.                 result.Add(li[i]);   
  171.   
  172.                      
  173.   
  174.             }   
  175.             return result;   
  176.   
  177.   
  178.   
  179.         }   
  180.   
  181.         private ItemSet AprioriGenerate(ItemSet li,int k, double support)   
  182.         {   
  183.   
  184.             ItemSet curList = null;   
  185.             ItemSet durList = null;   
  186.             ItemSet candi = null;   
  187.             ItemSet result = new ItemSet();   
  188.             for (int i = 0; i < li.Count; i++)   
  189.             {   
  190.                 for (int j = 0; j < li.Count; j++)   
  191.                 {   
  192.                     bool flag = true ;   
  193.                     curList = (ItemSet)li[i];   
  194.                     durList = (ItemSet)li[j];   
  195.                     for (int n = 2; n < k; n++)   
  196.                     {   
  197.   
  198.                         if (((DataItem)curList[n - 2]).Id == ((DataItem)durList[n - 2]).Id)   
  199.                         {   
  200.   
  201.                             flag = true;   
  202.   
  203.                         }   
  204.                         else {   
  205.                             break;   
  206.                             flag = false;   
  207.                               
  208.                               
  209.                         }   
  210.                          
  211.   
  212.                     }   
  213.   
  214.                     if (flag && ((DataItem)curList[k - 1] ).Id< ((DataItem)durList[k - 1]).Id)   
  215.                     {   
  216.   
  217.                         flag = true;   
  218.                     }   
  219.                     else {   
  220.                         flag = false;   
  221.                     }   
  222.                     if (flag)   
  223.                     {   
  224.                         candi = new ItemSet();   
  225.                            
  226.   
  227.                         for(int m=0;m<k;m++){   
  228.                             candi.Add(durList[m]);   
  229.                            
  230.                         }   
  231.                         candi.Add(curList[k-1]);   
  232.   
  233.   
  234.   
  235.   
  236.   
  237.                         if (HasInFrequentSubset(candi, li,k))   
  238.                         {   
  239.                             candi.Clear();   
  240.   
  241.                         }   
  242.                         else  
  243.                         {   
  244.                             result.Add(candi);   
  245.                         }   
  246.                     }   
  247.   
  248.                 }   
  249.             }   
  250.             return result;   
  251.   
  252.         }   
  253.   
  254.   
  255.            
  256.         private bool HasInFrequentSubset(ItemSet candidate, ItemSet li,int k)   
  257.         {   
  258.             ItemSet subSet = SubSet(candidate,k);   
  259.             ItemSet curList = null;   
  260.             ItemSet liCurList = null;   
  261.            
  262.             for (int i = 0; i < subSet.Count; i++)   
  263.             {   
  264.                 curList = (ItemSet)subSet[i];   
  265.                 for (int j = 0; j < li.Count; j++)   
  266.                 {   
  267.   
  268.                     liCurList = (ItemSet)li[j];   
  269.                     if (liCurList.Equals(curList))   
  270.                     {   
  271.                        return false;   
  272.   
  273.                     }   
  274.                       
  275.                 }   
  276.             }   
  277.             return true;;   
  278.         }   
  279.         //划分子集   
  280.         private ItemSet SubSet(ItemSet set)   
  281.         {   
  282.             ItemSet subSet = new ItemSet();   
  283.   
  284.             ItemSet itemSet = new ItemSet();   
  285.             //移位求2n次访   
  286.             int num = 1 << set.Count;   
  287.   
  288.             int bit;   
  289.             int mask = 0; ;   
  290.             for (int i = 0; i < num; i++)   
  291.             {   
  292.                 itemSet = new ItemSet();   
  293.                 for (int j = 0; j < set.Count; j++)   
  294.                 {   
  295.                     //mask与i可以得出某位是否为零   
  296.                     mask = 1 << j;   
  297.                     bit = i & mask;   
  298.                     if (bit > 0)   
  299.                     {   
  300.   
  301.                         itemSet.Add(set[j]);   
  302.   
  303.                     }   
  304.                 }   
  305.                 if (itemSet.Count > 0)   
  306.                 {   
  307.                     subSet.Add(itemSet);   
  308.                 }   
  309.   
  310.   
  311.             }   
  312.   
  313.   
  314.   
  315.             return subSet;   
  316.         }   
  317.   
  318.   
  319.   
  320.         //划分子集   
  321.         private ItemSet SubSet(ItemSet setint t)   
  322.         {   
  323.             ItemSet subSet = new ItemSet();   
  324.   
  325.             ItemSet itemSet = new ItemSet();   
  326.             //移位求2n次访   
  327.             int num = 1 << set.Count;   
  328.   
  329.             int bit;   
  330.             int mask = 0; ;   
  331.             for (int i = 0; i < num; i++)   
  332.             {   
  333.                 itemSet = new ItemSet();   
  334.                 for (int j = 0; j < set.Count; j++)   
  335.                 {   
  336.                     //mask与i可以得出某位是否为零   
  337.                     mask = 1 << j;   
  338.                     bit = i & mask;   
  339.                     if (bit > 0)   
  340.                     {   
  341.   
  342.                         itemSet.Add(set[j]);   
  343.   
  344.                     }   
  345.                 }   
  346.                 if (itemSet.Count == t)   
  347.                 {   
  348.                     subSet.Add(itemSet);   
  349.                 }   
  350.   
  351.   
  352.             }   
  353.   
  354.   
  355.   
  356.             return subSet;   
  357.         }   
  358.     }   
  359.     
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值