Ejemplo n.º 1
0
        private void AprioriBrowse_Click(object sender, EventArgs e)
        {
            string         FilePath         = string.Empty;
            OpenFileDialog chooseFileDialog = new OpenFileDialog();

            chooseFileDialog.Filter      = "All files (*.*)|*.*";
            chooseFileDialog.FilterIndex = 1;
            chooseFileDialog.Multiselect = false;

            if (chooseFileDialog.ShowDialog() == DialogResult.OK)
            {
                FilePath = chooseFileDialog.FileName;
                Console.WriteLine("Selected file - " + FilePath);

                itemsets = new FileIO().Read_File();

                foreach (var itemset in itemsets)
                {
                    string outputString = string.Empty;
                    foreach (var output in itemset)
                    {
                        outputString += output + " ";
                    }

                    AprioriData.Items.Add(outputString);
                }
            }
        }
Ejemplo n.º 2
0
        public static ItemSetCollection FindSubsets(ItemSet itemset, int n)
        {
            ItemSetCollection subsets = new ItemSetCollection();

            int subsetCount = (int)Math.Pow(2, itemset.Count);

            for (int i = 0; i < subsetCount; i++)
            {
                if (n == 0 || GetOnCount(i, itemset.Count) == n)
                {
                    string binary = DecimalToBinary(i, itemset.Count);

                    ItemSet subset = new ItemSet();
                    for (int charIndex = 0; charIndex < binary.Length; charIndex++)
                    {
                        if (binary[charIndex] == '1')
                        {
                            subset.Add(itemset[charIndex]);
                        }
                    }
                    subsets.Add(subset);
                }
            }

            return(subsets);
        }
Ejemplo n.º 3
0
        public List <AssociationRule> Mine(ItemSetCollection db, ItemSetCollection L, double confidenceThreshold)
        {
            List <AssociationRule> allRules = new List <AssociationRule>();

            foreach (ItemSet itemset in L)
            {
                ItemSetCollection subsets = Bit.FindSubsets(itemset, 0); //get all subsets
                foreach (ItemSet subset in subsets)
                {
                    double confidence = (db.FindSupport(itemset) / db.FindSupport(subset)) * 100.0;
                    if (confidence >= confidenceThreshold)
                    {
                        AssociationRule rule = new AssociationRule();
                        rule.X.AddRange(subset);
                        rule.Y.AddRange(itemset.Remove(subset));
                        rule.Support    = db.FindSupport(itemset);
                        rule.Confidence = confidence;
                        if (rule.X.Count > 0 && rule.Y.Count > 0)
                        {
                            allRules.Add(rule);
                        }
                    }
                }
            }

            return(allRules);
        }
Ejemplo n.º 4
0
        public void Write_File(ItemSet itemset)
        {
            ItemSetCollection itemsets = Read_File(FilePath);

            itemsets.Add(itemset);

            StringBuilder stringbuilder = new StringBuilder();

            foreach (var i in itemsets)
            {
                string output = string.Empty;
                foreach (var s in i)
                {
                    if (i.IndexOf(s) == i.Count - 1)
                    {
                        output += s;
                    }
                    else
                    {
                        output += s + "/";
                    }
                }
                Console.WriteLine(output);
                stringbuilder.AppendLine(output);
            }

            using (var fileStream = File.OpenWrite(FilePath))
                using (StreamWriter writer = new StreamWriter(fileStream))
                {
                    //Console.WriteLine(stringbuilder.ToString());
                    writer.Write(stringbuilder.ToString());
                }
        }
Ejemplo n.º 5
0
        public ItemSetCollection Read_File(string FilePath)
        {
            ItemSet           itemset;
            ItemSetCollection itemsets    = new ItemSetCollection();
            const Int32       BUFFER_SIZE = 128;

            using (var fileStream = File.OpenRead(FilePath))
                using (var streamReader = new StreamReader(fileStream, Encoding.UTF8, true, BUFFER_SIZE))
                {
                    string line;
                    while ((line = streamReader.ReadLine()) != null)
                    {
                        itemset = new ItemSet();
                        string[] lines = line.Split('/');
                        for (int i = 0; i < lines.Length; i++)
                        {
                            //Console.WriteLine(lines[i]);
                            itemset.Add(lines[i]);
                        }
                        //Console.WriteLine("ItemSet count = " + itemset.Count);
                        itemsets.Add(itemset);
                    }
                }

            return(itemsets);
        }
Ejemplo n.º 6
0
        public ItemSetCollection DoApriori(ItemSetCollection db, double supportThreshold)
        {
            ItemSet           I  = db.GetUniqueItems();
            ItemSetCollection L  = new ItemSetCollection(); //resultant large itemset
            ItemSetCollection Li = new ItemSetCollection(); //large itemset in each iteration
            ItemSetCollection Ci = new ItemSetCollection(); //pruned itemset in each iterations

            //first iteration (1-item itemsets)
            foreach (string item in I)
            {
                Ci.Add(new ItemSet()
                {
                    item
                });
            }

            //next iterations
            int k = 2;

            while (Ci.Count != 0)
            {
                //set Li from Ci (pruning)
                Li.Clear();
                foreach (ItemSet itemset in Ci)
                {
                    itemset.Support = db.FindSupport(itemset);
                    if (itemset.Support >= supportThreshold)
                    {
                        Li.Add(itemset);
                        L.Add(itemset);
                    }
                }

                //set Ci for next iteration (find supersets of Li)
                Ci.Clear();
                Ci.AddRange(Bit.FindSubsets(Li.GetUniqueItems(), k)); //get k-item subsets
                k += 1;
            }

            return(L);
        }
Ejemplo n.º 7
0
        //Default location
        public ItemSetCollection Read_File()
        {
            ItemSet           itemset;
            ItemSetCollection itemsets    = new ItemSetCollection();
            const Int32       BUFFER_SIZE = 128;

            using (var fileStream = File.OpenRead(FilePath))
                //Making sure that it would read lithuanian alphabet
                using (var streamReader = new StreamReader(fileStream, Encoding.UTF8, true, BUFFER_SIZE))
                {
                    string line;
                    while ((line = streamReader.ReadLine()) != null)
                    {
                        itemset = new ItemSet();
                        string[] lines = line.Split('/');
                        for (int i = 0; i < lines.Length; i++)
                        {
                            itemset.Add(lines[i]);
                        }
                        itemsets.Add(itemset);
                    }
                }
            return(itemsets);
        }