private void btnAprioriNew_Click(object sender, EventArgs e) { richAssociationAprioriNew.Text = string.Empty; richLargeAprioriNew.Text = string.Empty; //do apriori double supportThreshold = double.Parse(txtSupportThreshold.Text); System.Diagnostics.Stopwatch calcTime = System.Diagnostics.Stopwatch.StartNew(); ItemsetCollection L = AprioriMining.DoAprioriNew(db, supportThreshold); richLargeAprioriNew.Text = (L.Count + " Large Itemsets (by Apriori)") + "\r\n"; txtTimesAprioriNew.Text = ((calcTime.ElapsedMilliseconds)) + " Milliseconds"; foreach (Itemset itemset in L) { richLargeAprioriNew.Text += itemset.ToString() + "\r\n"; } //do mining double confidenceThreshold = double.Parse(txtConfidenceThreshold.Text); List <AssociationRule> allRules = AprioriMining.Mine(db, L, confidenceThreshold); richAssociationAprioriNew.Text = (allRules.Count + " Association Rules \n"); foreach (AssociationRule rule in allRules) { richAssociationAprioriNew.Text += rule.ToString() + "\r\n"; } }
public void ExecuteCommand(UserRecientBlock item) { var trainingDB = PerformCollection(item); var aprioriResult = AprioriMining.DoApriori(trainingDB, 40.0); var rules = AprioriMining.Mine(trainingDB, aprioriResult, 70.0); UpdateConcertSuggestions(rules); }
//[Test] public void TestMethod1() { ItemsetCollection collection = new ItemsetCollection { list1, list2, list3, list4 }; ItemsetCollection L = AprioriMining.Apriori(collection, 50); ItemsetCollection Check = GetItems(); Assert.AreEqual(Check, L); }
private void btnAprioriNewSS_Click(object sender, EventArgs e) { richAssociationAprioriNewSS.Text = string.Empty; richLargeAprioriNewSS.Text = string.Empty; // do FP-Growth double supportThreshold = double.Parse(txtSupportThreshold.Text); System.Diagnostics.Stopwatch calcTime = System.Diagnostics.Stopwatch.StartNew(); ReturnLApriori();//FPGrowthMining.DoFPGrowthParallel(db, supportThreshold); txtTimesAprioriNewSS.Text = ((calcTime.ElapsedMilliseconds)) + " Milliseconds"; // string[] database = null; try { database = System.IO.File.ReadAllLines("OutputAprioriNew.txt"); } catch { } ItemsetCollection L = new ItemsetCollection(); Itemset items; foreach (string item in database) { items = new Itemset(); string[] itemsupport = item.Split(':'); //items.AddRange(itemsupport[0].Split(',')); foreach (string it in itemsupport[0].Split(',')) { items.Add(int.Parse(it)); } //items.Remove(""); items.Support = double.Parse(itemsupport[1]); L.Add(items); } // richLargeAprioriNewSS.Text = (L.Count + " Large Itemsets (by Apriori cải tiến song song)") + "\r\n"; foreach (Itemset itemset in L) { richLargeAprioriNewSS.Text += itemset.ToString() + "\r\n"; } //do mining double confidenceThreshold = double.Parse(txtConfidenceThreshold.Text); List <AssociationRule> allRules = AprioriMining.Mine(db, L, confidenceThreshold); richAssociationAprioriNewSS.Text = (allRules.Count + " Association Rules \n"); foreach (AssociationRule rule in allRules) { richAssociationAprioriNewSS.Text += rule.ToString() + "\r\n"; } }
public void AprioriTid() //Test of AprioriTid for support = 50% { ItemsetCollection collection = new ItemsetCollection { list1, list2, list3, list4 }; ItemsetCollection L = AprioriMining.AprioriTid(collection, 50); ItemsetCollection Check = GetItems(); Assert.AreEqual(Check.Count, L.Count); //Checking items count in itemsetcollection for (int k = 0; k < Check.Count; k++) //Loop for all itmesets { Assert.AreEqual(Check[k].Support, L[k].Support); //Checking support of itemset Assert.AreEqual(Check[k].Count, L[k].Count); // Checking number count of itmes in itemset Assert.AreEqual(Check[k].Sum(), L[k].Sum()); //Checking summary of all numbers in list } }
public static void DoApriori(double support, double confidence, Itemset _items, ItemsetCollection _db) { Itemset uniqueItems = _db.GetUniqueItems(); ItemsetCollection L = AprioriMining.DoApriori(_db, support); Console.WriteLine((L.Count + " Large Itemsets (by Apriori)\n")); foreach (Itemset itemset in L) { Console.WriteLine(itemset.ToString() + " Длина " + itemset.Count); } Console.WriteLine("\n"); int maxLSize = L[0].Count; Console.WriteLine("maxLSize=" + maxLSize); foreach (Itemset itemset in L) { if (maxLSize < itemset.Count) { maxLSize = itemset.Count; } } Console.WriteLine("maxLSize=" + maxLSize); foreach (Itemset itemset in L) { if (itemset.Count < maxLSize) { itemset.Clear(); } } foreach (Itemset itemset in L) { Console.WriteLine(itemset.ToString() + " Длина " + itemset.Count); } List <AssociationRule> allRules = AprioriMining.Mine(_db, L, confidence); Console.WriteLine(allRules.Count + " Association Rules\n"); foreach (AssociationRule rule in allRules) { Console.WriteLine(rule.ToString() + "Длина=" + rule.getTotalLength() + "\n"); } }
public void Mine() //Test of minig asscociationrules for conffidence = 80 { ItemsetCollection collection = new ItemsetCollection { list1, list2, list3, list4 }; ItemsetCollection L = AprioriMining.Apriori(collection, 50); List <AssociationRule> AllRules = AprioriMining.Mine(collection, L, 80); List <AssociationRule> Check = GetRules(); Assert.AreEqual(Check.Count, AllRules.Count); //Checking rules count in rules collection for (int k = 0; k < Check.Count; k++) //Loop for all rules { Assert.AreEqual(Check[k].Confidence, AllRules[k].Confidence); //Checking confidence of rule Assert.AreEqual(Check[k].X.Count, AllRules[k].X.Count); // Checking numbers count of itmes in X Assert.AreEqual(Check[k].X.Sum(), AllRules[k].X.Sum()); //Checking summary of all numbers in X Assert.AreEqual(Check[k].Y.Count, AllRules[k].Y.Count); // Checking numbers count of itmes in Y Assert.AreEqual(Check[k].Y.Sum(), AllRules[k].Y.Sum()); //Checking summary of all numbers in Y } }
private void TestApriori() { ItemsetCollection collection = new ItemsetCollection { list1, list2, list3, list4 }; ItemsetCollection L = AprioriMining.Apriori(collection, 50); ItemsetCollection Check = GetItems(); Itemset i = new Itemset(); i.Add(1); i.Support = 10; i.Tid = 0; Itemset j = new Itemset(); j.Add(1); j.Support = 10; j.Tid = 0; Assert.AreEqual(Check[0].Sum(), j.ToList()); }
/// <summary> /// Connects to MySQL in order to run AprioriMiner /// </summary> private static void DataFromMysql() { double[] minsupminconf = PromptForMinsupAndConf(); var tablechosen = PromptForDatasetSize(); var tableToMine = string.Empty; switch (tablechosen) { case 1: tableToMine = "itemsets"; break; case 5: tableToMine = "itemsets5k"; break; case 20: tableToMine = "itemsets20k"; break; case 75: tableToMine = "itemsets75k"; break; default: Console.WriteLine("Whooops something very bad has happened... Try again."); return; } Console.WriteLine("Connecting to mysql server.... Trying to find table with {0}K transactions", tablechosen); var database = new ItemsetCollection(); var timeStartQuery = DateTime.Now; Console.Write("Started querying DB at " + timeStartQuery.ToLocalTime()); using (var conn = new MySqlConnection("server=localhost;user=dmuser;database=DataMiningDb;port=3306;password=data;")) { try { conn.Open(); var rows = 0; rows = CountTransactions(conn, tableToMine); var cmd = conn.CreateCommand(); for (var i = 1; i <= rows; i++) { cmd.CommandText = i == 1 ? (@"set profiling=1; SELECT itemID FROM dataminingdb." + tableToMine + " WHERE transId=" + i) : @"SELECT itemID FROM dataminingdb." + tableToMine + " WHERE transId=" + i; var reader = cmd.ExecuteReader(); var listofItemsInRow = new Itemset(); while (reader.Read()) { listofItemsInRow.Add(reader.GetInt32(0)); } database.Add(listofItemsInRow); reader.Close(); // ============== LOG PROFILER DATA cmd.CommandText = "SHOW PROFILES"; var pfdata = cmd.ExecuteReader(); var rs = 0; var list = new List <object[]>(); while (pfdata.Read()) { var rowArray = new object[pfdata.FieldCount]; rs = pfdata.GetValues(rowArray); list.Add(rowArray); } using (var swrtr = new StreamWriter(@"..\..\..\MysqlProfilerLog.csv", true)) { // swrtr.WriteLine(DateTime.Now + " = LOG Success = {0}--------------------\n", rs); // Query Number,Time,SQLStatement foreach (var stringse in list) { if (stringse[2].Equals("SHOW WARNINGS")) { continue; } foreach (var s in stringse) { swrtr.Write(s + ","); if (stringse.ElementAt(2) == s) { swrtr.Write("\n"); } } // swrtr.WriteLine(Environment.NewLine); } } // ============== pfdata.Close(); // Writing profiler info to log file // RecordLog(conn); } // End of For Loop } catch (Exception e) { Console.WriteLine("There was an error: {0}\n\n Consult the help menu.", e.Message); } } // database (of ItemsetCollection should now be populated and ready for mining... var endQueryT = DateTime.Now; Console.WriteLine("\aEnded queries at: {0}\nTime elapsed: {1}\n\nNow running Apriori on fetched data...", endQueryT.ToLocalTime(), endQueryT.Subtract(timeStartQuery)); var large = AprioriMining.DoApriori(database, CreateSetOfUniqueItems(), minsupminconf[0]); var results = "Results: \n\n " + large.Count + " supported Itemsets obtained by Apriori\n\n"; foreach (var itemset in large) { results += itemset.ToString() + "\n"; } Console.WriteLine("DONE! Now mining association rules..."); var allRules = AprioriMining.Mine(database, large, minsupminconf[1]); results += "\nAssociation Rules Found: \n"; if (allRules.Count == 0) { results += "No rules were found over minconf of " + minsupminconf[1] + "%"; } else { foreach (var associationRule in allRules) { results += associationRule.ToString() + "\n"; } } Console.WriteLine(results); var finaltimeEnd = DateTime.Now; Console.Write("Total Apriori time was: {0}", endQueryT.Subtract(finaltimeEnd)); }
private static void DataFromMongo() { double[] minsupminconf = PromptForMinsupAndConf(); var collneeded = PromptForDatasetSize(); var collectionNeeded = string.Empty; switch (collneeded) { case 1: collectionNeeded = "receipts1k"; break; case 5: collectionNeeded = "receipts5k"; break; case 20: collectionNeeded = "receipts20k"; break; case 75: collectionNeeded = "receipts75k"; break; case 0: collectionNeeded = "receipts"; break; default: Console.WriteLine("Whooops something very bad has happened... Try again."); return; } Console.WriteLine("\nConnecting to mongoDB..."); var database = new ItemsetCollection(); var start = DateTime.Now; try { const string ConnStr = "mongodb://localhost:27017"; var clt = new MongoClient(ConnStr); var svr = clt.GetServer(); var dblearn = svr.GetDatabase("learn"); // var tlist = dblearn.GetCollection("receipts"); var list = dblearn.GetCollection <Transaction>(collectionNeeded); Console.WriteLine("Found Data!\nCollection Name: {0}\nSize: {1}", collectionNeeded, list.Count()); var all = list.FindAll(); foreach (var transaction in all) { var itemset = new Itemset(); foreach (var item in transaction.Items) { itemset.Add(item); } database.Add(itemset); } } catch (Exception e) { Console.WriteLine("There was an error, here are the details: \n=====================Error Stuff Here=====================\n" + e.Message + "\n=========================================================="); Console.WriteLine("Is the mongodb server running?? See the Help/Info section for help..."); return; } var endqueryT = DateTime.Now; var totalQueryT = start.Subtract(endqueryT); var itemsunique = CreateSetOfUniqueItems(); Console.WriteLine("Disconnected from MongoDB server at {0}.\n Total query time was: {1}", endqueryT.ToLocalTime(), totalQueryT); Console.WriteLine("Now running Apriori on fetched data..."); var large = AprioriMining.DoApriori(database, itemsunique, minsupminconf[0]); var results = "Results: \n\n " + large.Count + " supported Itemsets obtained by Apriori\n\n"; foreach (var itemset in large) { results += itemset.ToString() + "\n"; } Console.WriteLine("DONE! Now mining association rules..."); var allRules = AprioriMining.Mine(database, large, minsupminconf[1]); results += "\nAssociation Rules Found: \n"; if (allRules.Count == 0) { results += "No rules were found over minconf of " + minsupminconf[1] + "%"; } else { foreach (var associationRule in allRules) { results += associationRule.ToString() + "\n"; } } Console.WriteLine(results); var finaltimeEnd = DateTime.Now; Console.Write("It's now: {0}.\n\nTotal Apriori time was: {1}", finaltimeEnd.ToLocalTime(), endqueryT.Subtract(finaltimeEnd)); }
private void PopulateChart() { try { var filmList = db.Movies.ToList(); Itemset myItemset = new Itemset(); foreach (var i in filmList) { myItemset.Add(i.MyMovieID); } ItemsetCollection myItemsetCollection = new ItemsetCollection(); var MovieList = db.UserMovieLists.Select(u => u); foreach (var u in MovieList) { var query = from a in db.Ratings join b in db.UserMovieLists on a.UserMovieLIstID equals b.UserMovieListID into temp from b in temp.DefaultIfEmpty() where a.UserMovieLIstID == u.UserMovieListID where a.UserRatingID >= 5 select new { a.MyMovieID, }; Itemset tempItemset = new Itemset(); ; if (myItemset != null) { if (query.Count() > 1) { foreach (var r in query.Take(25)) { tempItemset.Add(r.MyMovieID); } myItemsetCollection.Add(tempItemset); } } } List <double> supportList = new List <double>(); double support1 = 30; double support2 = 27.5; double support3 = 25; double support4 = 22.5; double support5 = 20; supportList.Add(support5); supportList.Add(support4); supportList.Add(support3); supportList.Add(support2); supportList.Add(support1); foreach (var ele in supportList) { int timeStart, timeStop, timeElapsed; timeStart = Environment.TickCount; ItemsetCollection L = AprioriMining.Apriori(myItemsetCollection, ele); timeStop = Environment.TickCount; timeElapsed = (timeStop - timeStart) / 1000; this.chart1.Series["Apriori"].Points.AddXY(ele, timeElapsed); } foreach (var ele in supportList) { int timeStart, timeStop, timeElapsed; timeStart = Environment.TickCount; ItemsetCollection L = AprioriMining.AprioriTid(myItemsetCollection, ele); timeStop = Environment.TickCount; timeElapsed = (timeStop - timeStart) / 1000; this.chart1.Series["AprioriTid"].Points.AddXY(ele, timeElapsed); } } catch (Exception exp) { MessageBox.Show(exp.ToString()); } }
private void AddRules2() { try { var filmList = db.Movies.ToList(); Itemset myItemset = new Itemset(); foreach (var i in filmList) { myItemset.Add(i.MyMovieID); } ItemsetCollection myItemsetCollection = new ItemsetCollection(); var MovieList = db.UserMovieLists.Select(u => u); foreach (var u in MovieList) { var query = from a in db.Ratings join b in db.UserMovieLists on a.UserMovieLIstID equals b.UserMovieListID into temp from b in temp.DefaultIfEmpty() where a.UserMovieLIstID == u.UserMovieListID where a.UserRatingID >= 5 select new { a.MyMovieID, }; Itemset tempItemset = new Itemset(); ; if (myItemset != null) { if (query.Count() > 1) { foreach (var r in query.Take(25)) { tempItemset.Add(r.MyMovieID); } myItemsetCollection.Add(tempItemset); } } } ItemsetCollection L = AprioriMining.AprioriTid(myItemsetCollection, 20); List <AssociationRule> allRules = AprioriMining.Mine(myItemsetCollection, L, 20); var ruleX = db.RulesX.Select(x => x); db.RulesX.RemoveRange(ruleX); db.SaveChanges(); var ruleY = db.RulesY.Select(x => x); db.RulesY.RemoveRange(ruleY); db.SaveChanges(); var myRule = db.Rules.Select(x => x); db.Rules.RemoveRange(myRule); db.SaveChanges(); var ruleXmovie = db.RuleXMovie.Select(x => x); db.RuleXMovie.RemoveRange(ruleXmovie); db.SaveChanges(); var ruleYMovie = db.RuleYMovie.Select(x => x); db.RuleYMovie.RemoveRange(ruleYMovie); foreach (var r in allRules) { var rule = new MyRule(); var X = new RuleX(); var Y = new RuleY(); db.RulesX.Add(X); db.SaveChanges(); db.RulesY.Add(Y); db.SaveChanges(); rule.Support = r.Support; rule.Confidene = r.Confidence; rule.RuleXID = X.RuleXID; rule.RuleYID = Y.RuleYID; db.Rules.Add(rule); db.SaveChanges(); foreach (var x in r.X) { var rm = new RuleXMyMovie(); rm.RuleX_RuleXID = X.RuleXID; rm.MyMovie_MyMovieID = x; db.RuleXMovie.Add(rm); db.SaveChanges(); } foreach (var y in r.Y) { var rm = new RuleYMyMovie(); rm.RuleY_RuleYID = Y.RuleYID; rm.MyMovie_MyMovieID = y; db.RuleYMovie.Add(rm); db.SaveChanges(); } } } catch (Exception exp) { MessageBox.Show(exp.ToString()); } }