Exemplo n.º 1
0
        public int CompareTo(object other)
        {
            Frequent_Itemset otherTemperature = other as Frequent_Itemset;

            if (this.Support == otherTemperature.Support)
            {
                return(0);
            }
            if (this.Support < otherTemperature.Support)
            {
                return(1);
            }
            return(-1);
        }
Exemplo n.º 2
0
        /// <summary>
        /// 生成关联规则
        /// </summary>
        public void genAssoc_rules()
        {
            AssociationRule obj_tempAR;
            float           total_confidence = 0;
            ArrayList       temp_AR          = new ArrayList();

            for (int count_ar = 0; count_ar < freq_itemsets.Count; count_ar++)
            {
                Frequent_Itemset obj = (Frequent_Itemset)freq_itemsets[count_ar];

                int first_id  = obj._itemid_1;                                                              // 第一个项目id
                int second_id = obj._itemid_2;                                                              // 第二个项目id

                float confidence_1 = (float)((float)obj.support_count / (float)getSupport_count(first_id)); // 置信度1
                total_confidence += confidence_1;
                obj_tempAR        = new AssociationRule(first_id, second_id, obj.Support, confidence_1);
                temp_AR.Add(obj_tempAR);

                float confidence_2 = (float)((float)obj.support_count / (float)getSupport_count(second_id));  // 置信度2
                total_confidence += confidence_2;
                obj_tempAR        = new AssociationRule(second_id, first_id, obj.Support, confidence_2);
                temp_AR.Add(obj_tempAR);
            }
            // 置信度阈值,取平均值
            float min_confidence = (float)total_confidence / (float)temp_AR.Count;

            AssociationRule.min_confidence = min_confidence;

            // 生成关联规则
            foreach (AssociationRule obj in temp_AR)
            {
                if (obj.confidence > min_confidence)
                {
                    association_Rules.Add(obj);
                }
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// 生成频繁-2项集
        /// </summary>
        public void genFreq_two_itemsets()
        {
            int       count = 0;
            ArrayList temp_freq_itemsets = new ArrayList();
            ArrayList count_ArrayList    = new ArrayList();

            Frequent_Itemset obj_Frequent_Itemset;

            Frequent_Itemset[] obj_freq_itemsets;

            int   count_Support = 0;
            float total_Support = 0;

            int[] freq_one_items_id = new int[freq_one_items.Count];

            for (int n = 0; n < freq_one_items.Count; n++)
            {
                Freq_Item obj = (Freq_Item)freq_one_items[n];
                freq_one_items_id[n] = obj.itemid;
            }

            for (int n = 0; n < freq_one_items_id.Length - 1; n++)
            {
                int itemid_1 = freq_one_items_id[n];
                for (int m = n + 1; m < freq_one_items_id.Length; m++)
                {
                    int itemid_2 = freq_one_items_id[m];
                    // 扫描数据集,得到共同用户数目
                    for (int userid = 1; userid < sourceUsers.Length; userid++)
                    {
                        // 如果该用户喜欢这两个项目
                        if ((discretize[userid][itemid_1] == true) && (discretize[userid][itemid_2] == true))
                        {
                            count_Support++;
                        }
                    }

                    // 计算这两个项目的支持度
                    float support = (float)((float)count_Support / (float)sourceUsers.Length);

                    obj_Frequent_Itemset = new Frequent_Itemset(itemid_1, itemid_2, count_Support, support);
                    temp_freq_itemsets.Add(obj_Frequent_Itemset);
                    total_Support += support;

                    count_Support = 0;
                }
            }
            obj_freq_itemsets = new Frequent_Itemset[temp_freq_itemsets.Count];
            int _count_zero = 0;

            foreach (Frequent_Itemset item in temp_freq_itemsets)
            {
                if (item.Support == 0)
                {
                    _count_zero++;
                }

                obj_freq_itemsets[count++] = item;
            }

            Array.Sort(obj_freq_itemsets);

            // 计算所有不为零频繁2项集的支持度平均值 设为支持度阈值
            float Min_Support = total_Support / (float)(obj_freq_itemsets.Length - _count_zero);

            Frequent_Itemset.min_support = Min_Support;

            foreach (Frequent_Itemset obj_freq_itemset in obj_freq_itemsets)
            {
                if (obj_freq_itemset.Support > Frequent_Itemset.min_support)   // 支持度大于支持度阈值
                {
                    freq_itemsets.Add(obj_freq_itemset);
                }
            }
            // 最小支持度计数
            Frequent_Itemset last_two = (Frequent_Itemset)freq_itemsets[freq_itemsets.Count - 1];

            Frequent_Itemset.min_support_count = last_two.support_count;
        }
        /// <summary>
        /// 生成频繁-2项集
        /// </summary>
        public void genFreq_two_itemsets()
        {
            int count = 0;
            ArrayList temp_freq_itemsets = new ArrayList();
            ArrayList count_ArrayList = new ArrayList();

            Frequent_Itemset obj_Frequent_Itemset;
            Frequent_Itemset[] obj_freq_itemsets;

            int count_Support = 0;
            float total_Support = 0;

            int[] freq_one_items_id = new int[freq_one_items.Count];

            for (int n = 0; n < freq_one_items.Count; n++)
            {
                Freq_Item obj = (Freq_Item)freq_one_items[n];
                freq_one_items_id[n] = obj.itemid;
            }

            for (int n = 0; n < freq_one_items_id.Length - 1; n++)
            {
                int itemid_1 = freq_one_items_id[n];
                for (int m = n + 1; m < freq_one_items_id.Length; m++)
                {
                    int itemid_2 = freq_one_items_id[m];
                    // 扫描数据集,得到共同用户数目
                    for (int userid = 1; userid < sourceUsers.Length; userid++)
                    {
                        // 如果该用户喜欢这两个项目
                        if ((discretize[userid][itemid_1] == true) && (discretize[userid][itemid_2] == true))
                            count_Support++;
                    }

                    // 计算这两个项目的支持度
                    float support = (float)((float)count_Support / (float)sourceUsers.Length);

                    obj_Frequent_Itemset = new Frequent_Itemset(itemid_1, itemid_2, count_Support, support);
                    temp_freq_itemsets.Add(obj_Frequent_Itemset);
                    total_Support += support;

                    count_Support = 0;
                }
            }
            obj_freq_itemsets = new Frequent_Itemset[temp_freq_itemsets.Count];
            int _count_zero = 0;
            foreach (Frequent_Itemset item in temp_freq_itemsets)
            {
                if (item.Support == 0)
                {
                    _count_zero++;
                }

                obj_freq_itemsets[count++] = item;
            }

            Array.Sort(obj_freq_itemsets);

            // 计算所有不为零频繁2项集的支持度平均值 设为支持度阈值
            float Min_Support = total_Support / (float)(obj_freq_itemsets.Length - _count_zero);
            Frequent_Itemset.min_support = Min_Support;

            foreach (Frequent_Itemset obj_freq_itemset in obj_freq_itemsets)
            {
                if (obj_freq_itemset.Support > Frequent_Itemset.min_support)   // 支持度大于支持度阈值
                    freq_itemsets.Add(obj_freq_itemset);
            }
            // 最小支持度计数
            Frequent_Itemset last_two = (Frequent_Itemset)freq_itemsets[freq_itemsets.Count - 1];
            Frequent_Itemset.min_support_count = last_two.support_count;
        }