Ejemplo n.º 1
1
        public void AprioriExampleTest1()
        {
            // Example from https://en.wikipedia.org/wiki/Apriori_algorithm

            SortedSet<int>[] dataset = 
            {
                new SortedSet<int> { 1, 2, 3, 4 },
                new SortedSet<int> { 1, 2, 4 },
                new SortedSet<int> { 1, 2 },
                new SortedSet<int> { 2, 3, 4 },
                new SortedSet<int> { 2, 3 },
                new SortedSet<int> { 3, 4 },
                new SortedSet<int> { 2, 4 },
            };

            var apriori = new Apriori(threshold: 3, confidence: 0);

            var classifier = apriori.Learn(dataset);

            var expected = new Tuple<int[], int>[]
            {
                Tuple.Create(new[] {1},   3),
                Tuple.Create(new[] {2},   6),
                Tuple.Create(new[] {3},   4),
                Tuple.Create(new[] {4},   5),
                Tuple.Create(new[] {1,2}, 3),
                //Tuple.Create(new[] {1,3}, 1),
                //Tuple.Create(new[] {1,4}, 2),
                Tuple.Create(new[] {2,3}, 3),
                Tuple.Create(new[] {2,4}, 4),
                Tuple.Create(new[] {3,4}, 3),
                //Tuple.Create(new[] {2,3,4}, 2),
            };

            var frequent = apriori.Frequent;

            foreach (var tuple in expected)
            {
                var a = frequent.Where(x => x.Key.SetEquals(tuple.Item1)).First();
                Assert.AreEqual(tuple.Item2, a.Value);
            }
        }
Ejemplo n.º 2
0
        static void Main(string[] args)
        {
            /*

            This sample demonstrate how-to use the Apriori algorithm.

            As a test data I use a data from an imagine book store:
            1) We have a list of authors, which the store sells.
            2) We have an information about shopping cart of some users.

            As a result we get the following information:
            1. Order by popularity of the authors/set of authors.
            2. What we can advise the user, basing on his shopping cart. It is ordered by probability (frequent).

            */

            TypeRegister.Register<BookAuthor>((a, b) => a.Name.CompareTo(b.Name));

            var itemsets = new List<Itemset<BookAuthor>>();
            var transactions = new SampleHelper().Transactions.ToList();

            var apriori = new Apriori<BookAuthor>
            {
                MinSupport = (double) 1/9,
                SaveItemset = itemset => itemsets.Add(itemset),
                GetTransactions = ()=> transactions
            };

            apriori.ProcessTransactions();

            var rules = new List<Rule<BookAuthor>>();

            var agrawal = new AgrawalFaster<BookAuthor>
            {
                MinLift = 0.01,
                MinConfidence = 0.01,
                TransactionsCount = transactions.Count(),
                GetItemsets = ()=>itemsets,
                SaveRule = rule=>rules.Add(rule)
            };

            agrawal.Run();

            foreach (var item in itemsets.OrderByDescending(v => v.Support))
            {
                Console.WriteLine(string.Join("; ", item.Value.OrderBy(i => i.Name)) + " #SUP: " + item.Support);
            }

            Console.WriteLine("====");

            foreach (var item in rules.OrderByDescending(r => r.Confidence))
            {
                Console.WriteLine(string.Join("; ", item.Combination) + " => " + string.Join("; ", item.Remaining) + " ===> Confidence: " + item.Confidence + " Lift: " + item.Lift);
            }

            Console.ReadLine();
        }
Ejemplo n.º 3
0
        public void ClassifierTest1()
        {
            // example from http://www3.cs.stonybrook.edu/~cse634/lecture_notes/07apriori.pdf

            string[][] dataset = 
            {
                new string[] { "1", "2", "5" },
                new string[] { "2", "4" },
                new string[] { "2", "3" },
                new string[] { "1", "2", "4" },
                new string[] { "1", "3" },
                new string[] { "2", "3" },
                new string[] { "1", "3" },
                new string[] { "1", "2", "3", "5" },
                new string[] { "1", "2", "3" },
            };

            var apriori = new Apriori<string>(threshold: 2, confidence: 0.7);

            var classifier = apriori.Learn(dataset);

            var rules = classifier.Rules;

            Assert.AreEqual(6, rules.Length);
            Assert.AreEqual(rules[0].ToString(), "[5] -> [1]; support: 2, confidence: 1");
            Assert.AreEqual(rules[1].ToString(), "[5] -> [2]; support: 2, confidence: 1");
            Assert.AreEqual(rules[2].ToString(), "[4] -> [2]; support: 2, confidence: 1");
            Assert.AreEqual(rules[3].ToString(), "[5] -> [1 2]; support: 2, confidence: 1");
            Assert.AreEqual(rules[4].ToString(), "[1 5] -> [2]; support: 2, confidence: 1");
            Assert.AreEqual(rules[5].ToString(), "[2 5] -> [1]; support: 2, confidence: 1");


            string[][] actual;

            actual = classifier.Decide(new string[] { "1", "5" });
            Assert.AreEqual("1", actual[0][0]);
            Assert.AreEqual("2", actual[1][0]);
            Assert.AreEqual("1", actual[2][0]);
            Assert.AreEqual("2", actual[2][1]);
            Assert.AreEqual("2", actual[3][0]);
            actual = classifier.Decide(new string[] { "2", "5" });
            Assert.AreEqual("1", actual[0][0]);
            Assert.AreEqual("2", actual[1][0]);
            Assert.AreEqual("1", actual[2][0]);
            Assert.AreEqual("2", actual[2][1]);
            Assert.AreEqual("1", actual[3][0]);
            actual = classifier.Decide(new string[] { "0", "5" });
            Assert.AreEqual("1", actual[0][0]);
            Assert.AreEqual("2", actual[1][0]);
            Assert.AreEqual("1", actual[2][0]);
            Assert.AreEqual("2", actual[2][1]);
        }