Пример #1
0
        public ItemSet Read(string fileName)
        {

            ItemSet rowSet = new ItemSet();
            ItemSet colSet = null;
            string col = "";
            string[] head = null;
            if (File.Exists(fileName))
            {
                // Create a file to write to.

                StreamReader sr = new StreamReader(File.OpenRead(fileName), Encoding.Default, true);
                string row = "";
                int k = 0;
                while (!sr.EndOfStream)
                {
                    k++;
                    row = sr.ReadLine();

                    string[] cols = row.Split(",".ToCharArray());

                    if (k == 1)
                    {
                        head = cols;

                    }
                    else
                    {

                        colSet = new ItemSet();

                        for (int i = 1; i < cols.Length + 1; i++)
                        {
                            col = cols[i - 1];
                            if (col.Equals("1"))
                            {
                                colSet.Add(new DataItem(i, head[i - 1]));
                            }


                        }

                        rowSet.Add(colSet);
                    }


                }



                sr.Close();



            }
            return rowSet;
        }
Пример #2
0
        private static ItemSet FindOneColSet(ItemSet data, double support)
        {
            ItemSet cur = null;
            ItemSet result = new ItemSet();

            ItemSet set = null;
            ItemSet newset = null;
            DataItem cd = null;
            DataItem td = null;
            bool flag = true;

            for (int i = 0; i < data.Count; i++)
            {
                cur = (ItemSet)data[i];
                for (int j = 0; j < cur.Count; j++)
                {
                    cd = (DataItem)cur[j];

                    for (int n = 0; n < result.Count; n++)
                    {
                        set = (ItemSet)result[n];
                        td = (DataItem)set[0];
                        if (cd.Id == td.Id)
                        {
                            set.ICount++;
                            flag = false;
                            break;

                        }
                        flag = true;
                    }
                    if (flag)
                    {
                        newset = new ItemSet();
                        newset.Add(cd);
                        result.Add(newset);
                        newset.ICount = 1;
                    }
                }
            }
            ItemSet finalResult = new ItemSet();
            for (int i = 0; i < result.Count; i++)
            {
                ItemSet con = (ItemSet)result[i];
                if (con.ICount >= support)
                {
                    finalResult.Add(con);
                }
            }
            //finalResult.Sort();
            return finalResult;


        }
Пример #3
0
        public static ItemSet GenerateRules(ItemSet data, double support)
        {
            ItemSet result = new ItemSet();
            ItemSet li = new ItemSet();
            ItemSet conList = new ItemSet();
            ItemSet subConList = new ItemSet();
            ItemSet subDataList = new ItemSet();
            int k = 2;
            li.Add(new ItemSet());
            li.Add(FindOneColSet(data, support));

            while (((ItemSet)li[k - 1]).Count != 0)
            {

                conList = AprioriGenerate((ItemSet)li[k - 1], k - 1, support);
                for (int i = 0; i < data.Count; i++)
                {
                    subDataList = SubSet((ItemSet)data[i], k);
                    for (int j = 0; j < subDataList.Count; j++)
                    {
                        for (int n = 0; n < conList.Count; n++)
                        {
                            ((ItemSet)subDataList[j]).Sort();
                            ((ItemSet)conList[n]).Sort();
                            if (((ItemSet)subDataList[j]).Equals(conList[n]))
                            {
                                ((ItemSet)conList[n]).ICount++;

                            }
                        }

                    }

                }

                li.Add(new ItemSet());
                for (int i = 0; i < conList.Count; i++)
                {
                    ItemSet con = (ItemSet)conList[i];
                    if (con.ICount >= support)
                    {

                        ((ItemSet)li[k]).Add(con);
                    }
                }

                k++;
            }
            for (int i = 0; i < li.Count; i++)
            {
                result.Add(li[i]);
            }
            return result;
        }
Пример #4
0
        public override object Clone()
        {
            ArrayList al = (ArrayList)base.Clone();
            ItemSet set = new ItemSet();
            for (int i = 0; i < al.Count; i++)
            {

                set.Add(al[i]);
            }


            set.ICount = this.icount;
            return set;
        }
Пример #5
0
        //划分子集
        private static ItemSet SubSet(ItemSet set, int t)
        {
            ItemSet subSet = new ItemSet();

            ItemSet itemSet = new ItemSet();
            //移位求2n次访
            int num = 1 << set.Count;

            int bit;
            int mask = 0; ;
            for (int i = 0; i < num; i++)
            {
                itemSet = new ItemSet();
                for (int j = 0; j < set.Count; j++)
                {
                    //mask与i可以得出某位是否为零
                    mask = 1 << j;
                    bit = i & mask;
                    if (bit > 0)
                    {

                        itemSet.Add(set[j]);

                    }
                }
                if (itemSet.Count == t)
                {
                    subSet.Add(itemSet);
                }
            }
            return subSet;
        }
Пример #6
0
        private static bool HasInFrequentSubset(ItemSet candidate, ItemSet li, int k)
        {
            ItemSet subSet = SubSet(candidate, k);
            ItemSet curList = null;
            ItemSet liCurList = null;

            for (int i = 0; i < subSet.Count; i++)
            {
                curList = (ItemSet)subSet[i];
                for (int j = 0; j < li.Count; j++)
                {

                    liCurList = (ItemSet)li[j];
                    if (liCurList.Equals(curList))
                    {
                        return false;

                    }

                }
            }
            return true; ;
        }
Пример #7
0
        private static ItemSet AprioriGenerate(ItemSet li, int k, double support)
        {

            ItemSet curList = null;
            ItemSet durList = null;
            ItemSet candi = null;
            ItemSet result = new ItemSet();
            for (int i = 0; i < li.Count; i++)
            {
                for (int j = 0; j < li.Count; j++)
                {
                    bool flag = true;
                    curList = (ItemSet)li[i];
                    durList = (ItemSet)li[j];
                    for (int n = 2; n < k; n++)
                    {

                        if (((DataItem)curList[n - 2]).Id == ((DataItem)durList[n - 2]).Id)
                        {
                            flag = true;
                        }
                        else
                        {
                            flag = false;
                            break;
                        }


                    }

                    if (flag && ((DataItem)curList[k - 1]).Id < ((DataItem)durList[k - 1]).Id)
                    {
                        flag = true;
                    }
                    else
                    {
                        flag = false;
                    }
                    if (flag)
                    {
                        candi = new ItemSet();
                        for (int m = 0; m < k; m++)
                        {
                            candi.Add(durList[m]);
                        }
                        candi.Add(curList[k - 1]);
                        if (HasInFrequentSubset(candi, li, k))
                        {
                            candi.Clear();
                        }
                        else
                        {
                            result.Add(candi);
                        }
                    }

                }
            }
            return result;

        }