Beispiel #1
0
 //check if vertical transaction is in list
 private bool is_new(Vertical_transaction new_transaction, List <Vertical_transaction> new_data)
 {
     foreach (Vertical_transaction v in new_data)
     {
         var diff = v.itemset.Except(new_transaction.itemset);
         if (diff.ToList().Count == 0)
         {
             return(false);
         }
     }
     return(true);
 }
Beispiel #2
0
        // generate candidate itemset
        private List <Vertical_transaction> generate_candidates(List <Vertical_transaction> vertical_data)
        {
            List <Vertical_transaction> new_data = new List <Vertical_transaction>();

            if (vertical_data.Count == 0)
            {
                return(new_data);
            }
            if (vertical_data[0].itemset.Count == 1)
            {
                foreach (Vertical_transaction t1 in vertical_data)
                {
                    foreach (Vertical_transaction t2 in vertical_data)
                    {
                        if (t1.itemset[0] != t2.itemset[0])
                        {
                            List <string> items_list = new List <string>();
                            items_list.Add(t1.itemset[0]);
                            items_list.Add(t2.itemset[0]);
                            //deal with duplicates
                            items_list.Sort();
                            List <int> tids    = new List <int>();
                            List <int> onetids = new List <int>();
                            tids    = t1.tids.Intersect(t2.tids).ToList <int>();
                            onetids = t1.onetids.Intersect(t2.onetids).ToList <int>();
                            double support    = get_support(tids);
                            double confidence = get_confidence(tids, onetids);

                            Vertical_transaction new_transaction = new Vertical_transaction(items_list, tids, onetids, support, confidence);
                            if (is_new(new_transaction, new_data) && support >= min_sup && confidence >= min_conf)
                            {
                                new_data.Add(new_transaction);
                                string log = "";
                                foreach (string itemset in new_transaction.itemset)
                                {
                                    log += itemset.ToString() + ", ";
                                }
                            }
                        }
                    }
                }
                return(new_data);
            }

            else
            {
                string log = "";
                foreach (Vertical_transaction v in vertical_data)
                {
                    foreach (string s in v.itemset)
                    {
                        log += s + ", ";
                    }
                    log += "\n";
                }

                int k_count = vertical_data[0].itemset.Count;
                foreach (Vertical_transaction t1 in vertical_data)
                {
                    t1.itemset.Sort();
                    foreach (Vertical_transaction t2 in vertical_data)
                    {
                        t2.itemset.Sort();
                        bool          merge   = true;
                        List <string> new_set = new List <string>();
                        for (int iterator = 0; iterator < k_count; iterator++)
                        {
                            if (t1.itemset[iterator].Equals(t2.itemset[iterator]) && iterator < k_count - 1)
                            {
                                new_set.Add(t1.itemset[iterator]);
                            }
                            else if (!t1.itemset[iterator].Equals(t2.itemset[iterator]) && iterator < k_count - 1)
                            {
                                merge = false;
                            }
                        }
                        if (t1.itemset[k_count - 1].Equals(t2.itemset[k_count - 1]))
                        {
                            merge = false;
                        }
                        if (merge == true)
                        {
                            new_set.Add(t1.itemset[k_count - 1]);
                            new_set.Add(t2.itemset[k_count - 1]);

                            List <int>           tids            = t1.tids.Intersect(t2.tids).ToList <int>();
                            List <int>           onetids         = t1.onetids.Intersect(t2.onetids).ToList <int>();
                            double               support         = get_support(tids);
                            double               confidence      = get_confidence(tids, onetids);
                            Vertical_transaction new_transaction = new Vertical_transaction(new_set, tids, onetids, support, confidence);
                            if (is_new(new_transaction, new_data) && support >= min_sup && confidence >= min_conf)
                            {
                                new_data.Add(new_transaction);
                            }
                        }
                    }
                }
                return(new_data);
            }
        }