Exemple #1
0
        public Rule[] MineRules(DataTable dataset, string attributesPath, string consequentsPath,
                                string localPath, double minSupport = 0.001, double minConfidence = 0.01)
        {
            _logger.PrintMilestone("Reading attributes.");
            var antecedents = _fsHelper.LoadUnusedConsequents(consequentsPath);
            var attributes  = _fsHelper.GetAttributesList(attributesPath, consequentsPath);

            System.IO.Directory.CreateDirectory(localPath + "\\itemsets");

            foreach (DataRow tRow in dataset.Rows)
            {
                foreach (var att in attributes)
                {
                    att.Add(tRow[att.Name].ToString(), tRow["id"].ToString());
                }
            }
            _logger.PrintExecutionTime("Attributes read.");
            Attribute.TotalElements = dataset.Rows.Count;

            _logger.PrintMilestone("Starting to combine attributes...");
            _logger.BeginLap();


            /// <summary>
            /// Stores last itemset to itemsets collection, moves current to last and clears current.
            /// </summary>
            void SubmitItemset()
            {
                itemsets.AddRange(lastTierItemsets);
                lastTierItemsets    = currentTierItemsets;
                currentTierItemsets = new List <Itemset>();
            }

            // Get initial, 2-tier itemsets to build the rest on them.
            CombineAttributesAndValues(attributes.ToArray(), 0, attributes.Count - 1, 0, 2);
            _logger.PrintLap(string.Format("Generated {0}-tier itemsets ({1} entries).", 2, currentTierItemsets.Count));
            SubmitItemset();
            _fsHelper.SaveItemsets(lastTierItemsets, string.Format(localPath + "\\itemsets\\tier{0}", 2));
            int lastItemsetTier = 2;

            for (int i = 3; i < attributes.Count; i++)
            {
                foreach (Itemset itemset in lastTierItemsets)
                {
                    CombineAttributeWithItemset(itemset, attributes.ToArray());
                }
                SubmitItemset();
                if (lastTierItemsets.Count > 0)
                {
                    _logger.PrintLap(string.Format("Generated {0}-tier itemsets ({1} entries).", i, lastTierItemsets.Count));
                    _fsHelper.SaveItemsets(lastTierItemsets, string.Format(localPath + "\\itemsets\\tier{0}", i));
                }
                else
                {
                    lastItemsetTier = i - 1;
                    _logger.PrintMilestone(string.Format("No itemsets found for Tier {0}. No further itemsets will be generated.", i));
                    break;
                }
            }
            _logger.PrintExecutionTime(string.Format("Generated itemsets, {0} entries total.", itemsets.Count));

            _logger.PrintMilestone("Starting rules generation...");
            _logger.BeginLap();
            for (var i = 2; i <= lastItemsetTier; i++)
            {
                var tier = _fsHelper.LoadItemsets(string.Format("{0}\\itemsets\\tier{1}", localPath, i)).OrderBy(x => x.Support).ToArray();
                foreach (Itemset itemset in tier)
                {
                    BuildRules(itemset);
                }
                _logger.PrintLap(string.Format("Generated {0}-tier rules.", i));
            }
            _logger.PrintMilestone(string.Format("Ended rules generation. Total: {0} rules.", Rules.Count()));
            var ordered = Rules.OrderByDescending(x => x.confidence).ToArray();

            System.IO.File.WriteAllLines(string.Format(localPath + "\\rules\\minsupp{0}_minConf{1}_lines.txt", minSupport, minConfidence), ordered.Select(x => x.ToString()));
            _fsHelper.SaveObject <List <Rule> >(ordered.ToList(), string.Format(localPath + "\\rules\\minsupp{0}_minConf{1}.rules", minSupport, minConfidence));
            return(ordered);
        }
Exemple #2
0
        static void Main(string[] args)
        {
            _logger   = Logger.GetInstance();
            _fsHelper = new FSHelper();
            _logger.PrintMilestone("Reading configuration...");
            string datasetPath             = ConfigurationManager.AppSettings.Get("path_dataset");
            string attributesPath          = ConfigurationManager.AppSettings.Get("path_attributes");
            string unused_consequents_path = ConfigurationManager.AppSettings.Get("path_unused");
            string itemsetPathFormat       = ConfigurationManager.AppSettings.Get("path_itemset_format");
            string rulesPathFormat         = ConfigurationManager.AppSettings.Get("path_rules_format");

            _logger.PrintMilestone("Configuration successfully read.");

            _miner = new Miner();

            _logger.PrintMilestone("Started execution. Reading dataset.");

            var dt = _fsHelper.GetDataSet(datasetPath, ";");

            _logger.PrintExecutionTime("Loaded dataset.");

            _logger.PrintMilestone("Reading attributes.");
            consequents = _fsHelper.LoadUnusedConsequents(unused_consequents_path);
            attributes  = _fsHelper.GetAttributesList(attributesPath, unused_consequents_path);

            foreach (DataRow tRow in dt.Rows)
            {
                foreach (var att in attributes)
                {
                    att.Add(tRow[att.Name].ToString(), tRow["id"].ToString());
                }
            }

            SaveAsFeatures(dt, @"E:\Data\featurebundle.txt");

            dt.Dispose();
            _logger.PrintExecutionTime("Attributes read.");
            Attribute.TotalElements = dt.Rows.Count;

            _logger.PrintMilestone("Starting to combine attributes...");
            _logger.BeginLap();

            // Get initial, 2-tier itemsets to build the rest on them.
            CombineAttributesAndValues(attributes.ToArray(), 0, attributes.Count - 1, 0, 2);
            _logger.PrintLap(string.Format("Generated {0}-tier itemsets ({1} entries).", 2, currentTierItemsets.Count));
            SubmitItemset();
            _fsHelper.SaveItemsets(lastTierItemsets, string.Format(itemsetPathFormat, 2));

            for (int i = 3; i < attributes.Count; i++)
            {
                foreach (Itemset itemset in lastTierItemsets)
                {
                    CombineAttributeWithItemset(itemset, attributes.ToArray());
                }
                SubmitItemset();
                if (lastTierItemsets.Count > 0)
                {
                    _logger.PrintLap(string.Format("Generated {0}-tier itemsets ({1} entries).", i, lastTierItemsets.Count));
                    _fsHelper.SaveItemsets(lastTierItemsets, string.Format(itemsetPathFormat, i));
                }
                else
                {
                    lastItemsetTier = i - 1;
                    _logger.PrintMilestone(string.Format("No itemsets found for Tier {0}. No further itemsets will be generated.", i));
                    break;
                }
            }
            _logger.PrintExecutionTime(string.Format("Generated itemsets, {0} entries total.", itemsets.Count));

            _logger.PrintMilestone("Starting rules generation...");
            _logger.BeginLap();
            for (var i = 2; i <= lastItemsetTier; i++)
            {
                var tier = _fsHelper.LoadItemsets(string.Format(itemsetPathFormat, i)).OrderBy(x => x.Support).ToArray();
                foreach (Itemset itemset in tier)
                {
                    BuildRules(itemset);
                }
                _logger.PrintLap(string.Format("Generated {0}-tier rules.", i));
            }
            _logger.PrintMilestone(string.Format("Ended rules generation. Total: {0} rules.", rules.Count()));
            var ordered = rules.OrderByDescending(x => x.confidence).ToArray();

            System.IO.File.WriteAllLines(string.Format(rulesPathFormat, MinSupport, MinConfidence), ordered.Select(x => x.ToString()));
        }