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); } }
static void Main(string[] args) { var events = File.ReadAllLines(@"C:\Users\leosm\Documents\Projects\TCC\DataSetByCNPJ\cartelFull.csv"); var list = events.Select(x => { var split = x.Split(';'); return(new Participante { CodItemCompra = split[0], CnpjParticipante = split[1] }); }); var groups = list.GroupBy(x => x.CodItemCompra).ToDictionary(x => x.Key, x => x.Select(e => e.CnpjParticipante).ToArray()).ToArray(); var dataset = groups.Select(x => x.Value.ToArray()).ToArray(); // Create a new A-priori learning algorithm with the requirements var apriori = new Apriori <string>(threshold: 3, confidence: 0.7); // Use apriori to generate a n-itemset generation frequent pattern AssociationRuleMatcher <string> classifier = apriori.Learn(dataset); // Generate association rules from the itemsets: AssociationRule <string>[] rules = classifier.Rules; }
public string getRecommendedBook(string CustomerId) { if (CustomerId == null) { return("error - must have CustomerId"); } var allLoans = db.Loans.Include(l => l.Book).Include(l => l.Customer).ToList(); var customers = db.Customers.ToList(); List <int[]> tempDataset = new List <int[]>(); foreach (var c in customers) { var booksPerC = allLoans.Where(x => x.CustomerId == c.Id).Select(b => b.BookId).ToList(); tempDataset.Add(booksPerC.ToArray()); } int[][] dataset = tempDataset.ToArray(); Apriori apriori = new Apriori(threshold: 1, confidence: 0); AssociationRuleMatcher <int> classifier = apriori.Learn(dataset); var booksPerSpecC = allLoans.Where(x => x.Customer.PersonalID == (string)CustomerId).Select(b => b.BookId).ToArray(); int[][] matches = classifier.Decide(booksPerSpecC); if (matches.Length > 0) { int BestBookID = matches[0][0]; string BestBookName = db.Books.Single(x => x.Id == BestBookID).Name; return(BestBookName); } return("There is no recommended book for this customer."); }
public void UpdateRecommendedProducts() { var orders1 = _context.OrderClient.Include(order => order.ProductOrders).ThenInclude(po => po.Product); var orders = _context.ApplicationUser.Include(user => user.Orders).ThenInclude(order => order.ProductOrders) .ThenInclude(po => po.Product).Select(delegate(ApplicationUser user) { List <int> orderList = new List <int>(); foreach (ClientOrder co in user.Orders) { foreach (ProductOrder po in co.ProductOrders) { orderList.Add(po.Product.ProductID); } } return(orderList); }).ToArray(); SortedSet <int>[] dataset = ToSortedSet(orders); // We will use Apriori to determine the frequent item sets of this database. // To do this, we will say that an item set is frequent if it appears in at // least 20% transactions of the database: the value _minSupport * dataset.Length is the support threshold. // Create a new a-priori learning algorithm with support 3 Apriori <int> apriori = new Apriori <int>(Convert.ToInt32(_minSupport * dataset.Length), _minConfidence); // Use the algorithm to learn a set matcher classifier = apriori.Learn(dataset); }
public void Predict(string SaleID, string CustomerID) { Dictionary <string, int> mapActors = new Dictionary <string, int>(); SortedSet <int>[] dataset = SalesTransactionConverter(mapActors); Apriori apriori = new Apriori(threshold: 3, confidence: 0); AssociationRuleMatcher <int> classifier = apriori.Learn(dataset); var sale = _context.Sale.Include(s => s.Movies).Include("Movies.Movie").FirstOrDefault(s => s.SaleID == SaleID); HashSet <string> actorsSet = new HashSet <string>(); List <int> sample = new List <int>(); foreach (var movie in sale.Movies) { string[] actors = movie.Movie.Actors.Split(","); foreach (var actor in actors) { if (!actorsSet.Contains(actor)) { actorsSet.Add(actor); sample.Add(mapActors.GetValueOrDefault(actor)); } } } int[][] matches = classifier.Decide(sample.ToArray()); }
public static void Main() { BL.DataHandle db = new BL.DataHandle(); Item[][] a = db.GetOrders().Select(order => order.Items.ToArray()).ToArray(); Apriori <Item> ab = new Apriori <Item>(15, 0); //var clsfr = ab.Learn(a); //var itemsDistinctByBarcode = db.GetAllItems().GroupBy(i => i.BarcodeNumber).Select(grp => grp.FirstOrDefault()); //AssociationRuleMatcher<Item> cl = new AssociationRuleMatcher<Item>(itemsDistinctByBarcode.Count(), // clsfr.Rules.Where(r => r.Confidence > 0.6).ToArray()); //int abcde; //System.Collections.Generic.Dictionary<Item, int> counter = new System.Collections.Generic.Dictionary<Item, int>(); //foreach (Order o in db.GetOrders()){ // foreach (Item item in o.Items) // { // if (counter.TryGetValue(item,out abcde)) // { // } // } //} //var abcd = cl.Decide(itemsDistinctByBarcode.ToList().GetRange(0,10).ToArray()); AssociationRule <Item>[] rules = ab.Learn(a).Rules; foreach (var rule in rules) { Console.WriteLine(rule); } Console.ReadLine(); //BL.DataHandle.GenerateQRcodes(); //FlushData.FlushAll(); //Console.WriteLine("press key to exit"); //Console.ReadLine(); }
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); } }
public async Task <string> getRecommendedMovie(string customerId) { if (customerId == null) { throw new ArgumentException("customer id is required!"); } var loans = await _context.Loan .Include(l => l.Movie) .Include(l => l.Customer) .ToListAsync(); var customers = await _context.Customer.ToListAsync(); List <int[]> tempDatasset = new List <int[]>(); // Creating a unsymmetric matrix that each row contains the movies that a certain // customer has lent foreach (var customer in customers) { var moviesIdsPerCustomer = loans.Where(l => l.CustomerId == customer.CustomerId) .Select(m => m.MovieId).ToList(); tempDatasset.Add(moviesIdsPerCustomer.ToArray()); } int[][] dataset = tempDatasset.ToArray(); // threshold represents that only 1 movie that the specified customer has lent // needs to be found in the other loans of other customers in order to recommend it // for him Apriori apriori = new Apriori(threshold: 1, confidence: 0); // machine learning section AssociationRuleMatcher <int> classifier = apriori.Learn(dataset); // movies ids of the specified customer according to his loans var moviesPerSpecifiedCustomer = loans .Where(l => l.Customer.PersonalId == (string)customerId) .Select(b => b.MovieId).ToArray(); // make an intersection between his movies and try to find from the dataset // all rows that contains atleast 1 of his movies that he had lent int[][] matches = classifier.Decide(moviesPerSpecifiedCustomer); if (matches.Length > 0) { int bestMovieId = matches[0][0]; string bestMovieName = _context.Movie.Single(m => m.MovieId == bestMovieId).Name; return(bestMovieName); } return("There is no recommended movie for this customer."); }
public AssociationRuleMatcher <int> CreateAprioriClassifier() { // Create a new a-priori learning algorithm with support 3 Apriori apriori = new Apriori(threshold: 2, confidence: 0); int[][] histories = GetAllViewingHistories(); // Use the algorithm to learn a set matcher AssociationRuleMatcher <int> classifier = apriori.Learn(histories); return(classifier); }
public IList <ProductResult> RecommendProducts(int userId) { var salesByUser = this.GetAllSalesByUser(); List <int[]> tempDataset = new List <int[]>(); int[] currUserSales = null; foreach (var userSales in salesByUser) { if (userSales.UserID == userId) { currUserSales = userSales.Products.ToArray(); } tempDataset.Add(userSales.Products.ToArray()); } if (currUserSales == null || currUserSales.Length == 0) { return(new List <ProductResult>()); } int[][] dataset = tempDataset.ToArray(); // We will use Apriori to determine the frequent item sets of this database. // To do this, we will say that an item set is frequent if it appears in at // least 3 transactions of the database: the value 3 is the support threshold. // Create a new a-priori learning algorithm with support 3 Apriori apriori = new Apriori(threshold: 1, confidence: 0.5); //// Use the algorithm to learn a set matcher AssociationRuleMatcher <int> classifier = apriori.Learn(dataset); // Use the classifier to find orders that are similar to // orders where clients have bought items 1 and 2 together: int[][] matches = classifier.Decide(currUserSales); List <ProductResult> recommededProducts = new List <ProductResult>(); if (matches.Length > 0) { int[] tmpRecommendedProducts = matches[0]; foreach (var product in tmpRecommendedProducts) { recommededProducts.Add(this.GetProduct(product)); } } return(recommededProducts); }
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]); }
public void GetSkillsSuggestion() { SortedSet <int>[] dataset = { // Each row represents a set of items that have been bought // together. Each number is a SKU identifier for a product. new SortedSet <int> { 1, 2, 3, 4 }, // bought 4 items new SortedSet <int> { 1, 2, 4 }, // bought 3 items new SortedSet <int> { 1, 2 }, // bought 2 items new SortedSet <int> { 2, 3, 4 }, // ... new SortedSet <int> { 2, 3 }, new SortedSet <int> { 3, 4 }, new SortedSet <int> { 2, 4 }, }; // We will use Apriori to determine the frequent item sets of this database. // To do this, we will say that an item set is frequent if it appears in at // least 3 transactions of the database: the value 3 is the support threshold. // Create a new a-priori learning algorithm with support 3 Apriori apriori = new Apriori(threshold: 3, confidence: 0); // Use the algorithm to learn a set matcher AssociationRuleMatcher <int> classifier = apriori.Learn(dataset); // Use the classifier to find orders that are similar to // orders where clients have bought items 1 and 2 together: int[][] matches = classifier.Decide(new[] { 1, 2 }); // The result should be: // // new int[][] // { // new int[] { 4 }, // new int[] { 3 } // }; }
private ICollection <long> GetRecommendedGames(GetRecommendedGamesQueryDto query, long[][] transactions) { var transactionalPercentage = transactions.Length * 100 / _recommendationConfiguration.PercentageOfTransaction; var apriori = new Apriori <long>(transactionalPercentage, _recommendationConfiguration.Confident); var classifier = apriori.Learn(transactions); var matches = classifier.Decide(new SortedSet <long> { query.GameId }); return(matches.SelectMany(t => t.Select(i => i)).Distinct().Take(query.Take).ToList()); }
public IAnalysisResult AnalyzeData(List <ImportHeader> headers, List <ImportData> data, List <object> args) { if (args.Count < 1) { throw new Exception("Мінімальне значення затребуваності не задано."); } var confidence = (double)args[0]; var apriori = new Apriori <string>(2, confidence); var dataSet = GetDataSet(data); var classifier = apriori.Learn(dataSet); return(new AssociationRulesSearchResult { Result = Enums.OperationResult.Success, AnalysisMethod = Enums.AnalysisMethods.AssociationRulesSearch, Rules = classifier.Rules }); }
public List <AssociationRule <int> > AI() { Trace.WriteLine("Starting AI:"); var allBuys = dal.getApprovedBuys().ToArray(); int[][] dataset = new int[allBuys.Count()][]; for (int i = 0; i < allBuys.Count(); i++) { dataset[i] = new int[] { (int)allBuys[i].date.DayOfWeek, (int)allBuys[i].productID }; } Apriori apriori = new Apriori(threshold: 3, confidence: 0.2); AssociationRuleMatcher <int> classifier = apriori.Learn(dataset); AssociationRule <int>[] rules = classifier.Rules; return(rules.ToList()); }
// GET: Projects/Details/5 public async Task <IActionResult> Details(int?id) { if (id == null) { return(NotFound()); } // Machine learning is awesome! List <IGrouping <int, int> > projectIdsPerUsersGroups = _context.Sale.Include("Project").Where(x => !x.Project.IsDeleted).GroupBy(x => x.BuyerId, y => y.ProjectId).ToList(); SortedSet <int>[] projectsPerUserDataset = new SortedSet <int> [projectIdsPerUsersGroups.Count]; int idx = 0; foreach (var group in projectIdsPerUsersGroups) { projectsPerUserDataset[idx++] = new SortedSet <int>(group); } Apriori apriori = new Apriori(threshold: 3, confidence: 0); AssociationRuleMatcher <int> classifier = apriori.Learn(projectsPerUserDataset); AssociationRule <int>[] rules = classifier.Rules; ViewData["suggested"] = rules.Where(x => x.Y.First() != id).Select(x => _context.Project.First(y => y.Id == x.Y.First())); var project = await _context.Project .Include(x => x.AcademicInstitute) .Include(x => x.FieldOfStudy) .Include(x => x.Owner) .FirstOrDefaultAsync(m => m.Id == id && ((!m.Owner.IsDeleted && !m.IsDeleted) || ClaimsExtension.IsAdmin(HttpContext))); if (project == null) { return(NotFound()); } return(View(project)); }
public void RecommendedSpots() { // First I bring the entire usrs from the DB List <GeneralUser> allUsers = _context.Users.ToList(); // Second I am listing whole spots shown in the entire system users history List <int[]> allHistorySpots = new List <int[]>(); allUsers.ForEach(user => { if (!string.IsNullOrEmpty(user.History)) { // we are inserting to the allHistory list chunks of the user history -- [ [spotID ,spotID ,spotID ,spotID ] , [spotID ,spotID ,spotID ,spotID ] , [spotID, spotID] ] allHistorySpots.Add(user.History.Split(",").Select(int.Parse).ToArray()); } }); int[][] dataSet = allHistorySpots.ToArray(); // Apriori Algorithm is used to determine the frequent spot in the entire transactions found in the DB. // Create a new a-priori learning algorithm with the properties we set at the C-TOR Apriori apriori = new Apriori(_threshold, _minConfidence); // And now we will create the learning on the array we prepared before classifier = apriori.Learn(dataSet); }
public void CreateAprioriRules() { var associationBookList = bookSessionInfoManager.List().GroupBy(x => x.SessionId).ToArray(); SortedSet <int>[] dataset = new SortedSet <int> [associationBookList.Count()]; for (int i = 0; i < associationBookList.Count(); i++) { SortedSet <int> sortedSet = new SortedSet <int>() { }; foreach (var item1 in associationBookList[i]) { sortedSet.Add(item1.BookId); } dataset[i] = sortedSet; } Apriori apriori = new Apriori(threshold: 3, confidence: 0.5); AssociationRuleMatcher <int> classifier = apriori.Learn(dataset); int[][] matches = classifier.Decide(new[] { 1 }); AssociationRule <int>[] rules = classifier.Rules; InsertSimilarBook(rules); //foreach (var item in rules) //{ // item.X.ToString(); // item.ToString(); //} //return rules; }
public string RecommendOnProduct(Dictionary <int, string> dictCurrentCart) { if (dictCurrentCart.Count == 0) { return(string.Empty); } // We will use Apriori to determine the frequent item sets of this database. // To do this, we will say that an item set is frequent if it appears in at // least 3 transactions of the database: the value 3 is the support threshold. // Create a new a-priori learning algorithm with support 3 Apriori apriori = new Apriori(threshold: 2, confidence: 0); // Use the algorithm to learn a set matcher AssociationRuleMatcher <int> classifier = apriori.Learn(dataset); // Use the classifier to find orders that are similar to // orders where clients have bought items 1 and 2 together: var products = new int[dictCurrentCart.Count]; int index = 0; foreach (var product in dictCurrentCart) { products[index] = product.Key; index++; } int[][] matches = classifier.Decide(products); if (matches.Length == 0) { return(string.Empty); } else if (!allProducts.Keys.Contains(matches[0][0])) // the first product recommended { return(string.Empty); } AssociationRule <int>[] rules = classifier.Rules; string recProduct; bool res = allProducts.TryGetValue(matches[0][0], out recProduct); if (res) { return(recProduct); } return(string.Empty); // The result should be: // // new int[][] // { // new int[] { 4 }, // new int[] { 3 } // }; // Meaning the most likely product to go alongside the products // being bought is item 4, and the second most likely is item 3. // We can also obtain the association rules from frequent itemsets: // The result will be: // { // [1] -> [2]; support: 3, confidence: 1, // [2] -> [1]; support: 3, confidence: 0.5, // [2] -> [3]; support: 3, confidence: 0.5, // [3] -> [2]; support: 3, confidence: 0.75, // [2] -> [4]; support: 4, confidence: 0.66, // [4] -> [2]; support: 4, confidence: 0.8, // [3] -> [4]; support: 3, confidence: 0.75, // [4] -> [3]; support: 3, confidence: 0.6 // }; }
public void ClassifierTest1() { #region doc_learn_2 // Example from http://www3.cs.stonybrook.edu/~cse634/lecture_notes/07apriori.pdf // For this example, we will consider a database consisting of 9 transactions: // - Minimum support count required will be 2 (i.e. min_sup = 2 / 9 = 22%); // - Minimum confidence required should be 70%; // The dataset of transactions can be as follows: 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" }, }; // Create a new A-priori learning algorithm with the requirements var apriori = new Apriori <string>(threshold: 2, confidence: 0.7); // Use apriori to generate a n-itemset generation frequent pattern AssociationRuleMatcher <string> classifier = apriori.Learn(dataset); // Generate association rules from the itemsets: AssociationRule <string>[] rules = classifier.Rules; // The result should be: // { // [5] -> [1]; support: 2, confidence: 1, // [5] -> [2]; support: 2, confidence: 1, // [4] -> [2]; support: 2, confidence: 1, // [5] -> [1 2]; support: 2, confidence: 1, // [1 5] -> [2]; support: 2, confidence: 1, // [2 5] -> [1]; support: 2, confidence: 1, // } #endregion 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("2", actual[0][0]); Assert.AreEqual("1", actual[1][0]); Assert.AreEqual("2", actual[1][1]); actual = classifier.Decide(new string[] { "2", "5" }); Assert.AreEqual("1", actual[0][0]); Assert.AreEqual("1", actual[1][0]); Assert.AreEqual("2", actual[1][1]); actual = classifier.Decide(new string[] { "0", "5" }); Assert.AreEqual("1", actual[0][0]); Assert.AreEqual("2", actual[0][1]); Assert.AreEqual("2", actual[1][0]); Assert.AreEqual("1", actual[2][0]); }
public void AprioriExampleTest1() { #region doc_learn_1 // Example from https://en.wikipedia.org/wiki/Apriori_algorithm // Assume that a large supermarket tracks sales data by stock-keeping unit // (SKU) for each item: each item, such as "butter" or "bread", is identified // by a numerical SKU. The supermarket has a database of transactions where each // transaction is a set of SKUs that were bought together. // Let the database of transactions consist of following itemsets: SortedSet <int>[] dataset = { // Each row represents a set of items that have been bought // together. Each number is a SKU identifier for a product. new SortedSet <int> { 1, 2, 3, 4 }, // bought 4 items new SortedSet <int> { 1, 2, 4 }, // bought 3 items new SortedSet <int> { 1, 2 }, // bought 2 items new SortedSet <int> { 2, 3, 4 }, // ... new SortedSet <int> { 2, 3 }, new SortedSet <int> { 3, 4 }, new SortedSet <int> { 2, 4 }, }; // We will use Apriori to determine the frequent item sets of this database. // To do this, we will say that an item set is frequent if it appears in at // least 3 transactions of the database: the value 3 is the support threshold. // Create a new a-priori learning algorithm with support 3 Apriori apriori = new Apriori(threshold: 3, confidence: 0); // Use the algorithm to learn a set matcher AssociationRuleMatcher <int> classifier = apriori.Learn(dataset); // Use the classifier to find orders that are similar to // orders where clients have bought items 1 and 2 together: int[][] matches = classifier.Decide(new[] { 1, 2 }); // The result should be: // // new int[][] // { // new int[] { 4 }, // new int[] { 3 } // }; // Meaning the most likely product to go alongside the products // being bought is item 4, and the second most likely is item 3. // We can also obtain the association rules from frequent itemsets: AssociationRule <int>[] rules = classifier.Rules; // The result will be: // { // [1] -> [2]; support: 3, confidence: 1, // [2] -> [1]; support: 3, confidence: 0.5, // [2] -> [3]; support: 3, confidence: 0.5, // [3] -> [2]; support: 3, confidence: 0.75, // [2] -> [4]; support: 4, confidence: 0.66, // [4] -> [2]; support: 4, confidence: 0.8, // [3] -> [4]; support: 3, confidence: 0.75, // [4] -> [3]; support: 3, confidence: 0.6 // }; #endregion Assert.AreEqual(8, rules.Length); Assert.AreEqual(rules[0].ToString(), "[1] -> [2]; support: 3, confidence: 1"); Assert.AreEqual(rules[1].ToString(), "[2] -> [1]; support: 3, confidence: 0.5"); Assert.AreEqual(rules[2].ToString(), "[2] -> [3]; support: 3, confidence: 0.5"); Assert.AreEqual(rules[3].ToString(), "[3] -> [2]; support: 3, confidence: 0.75"); Assert.AreEqual(rules[4].ToString(), "[2] -> [4]; support: 4, confidence: 0.666666666666667"); Assert.AreEqual(rules[5].ToString(), "[4] -> [2]; support: 4, confidence: 0.8"); Assert.AreEqual(rules[6].ToString(), "[3] -> [4]; support: 3, confidence: 0.75"); Assert.AreEqual(rules[7].ToString(), "[4] -> [3]; support: 3, confidence: 0.6"); var str = matches.ToCSharp(); int[][] expectedMatches = new int[][] { new int[] { 4 }, new int[] { 3 } }; 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[] { 2, 3 }, 3), Tuple.Create(new[] { 2, 4 }, 4), Tuple.Create(new[] { 3, 4 }, 3), }; 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); } }
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]); }