public IList <Itemsets> GenerateLikelihoods(int supportCount, Disease disease) { IList <SymptomDisease> symptomDiseases = _context.SymptomDiseases.ToList(); IList <ISet <int> > initialItemsets = CreateItemsets(); IList <Itemsets> frequentItemsets = new List <Itemsets>(); Itemsets L1 = new Itemsets(); IList <SymptomDisease> symptoms = _context.SymptomDiseases.Where(symDis => symDis.DiseaseID == disease.Id).ToList(); foreach (SymptomDisease symptom in symptoms) { int count = symptomDiseases .Where(symDis => symDis.SymptomID.Equals(symptom.SymptomID)) .Count(); L1.AddItemset(new SortedSet <int>() { symptom.SymptomID }, count); } RemoveUnsupportedSets(L1, supportCount); int itemsetSize = 2; bool emptyCandidate = L1.SetsFrequency.Count() == 0; frequentItemsets.Add(L1); while (!emptyCandidate) { Itemsets Lk_1 = frequentItemsets[^ 1];
public static void RunAss() { string input = "contextZart.txt"; string output = "jegeroutfilen.txt"; double minsupp = 0.6; double minconf = 0.6; TransactionDatabase database = new TransactionDatabase(); try { database.loadFile(input); } catch (Exception e) { Console.WriteLine(e.StackTrace.ToString()); } AlgoAprioriTIDClose aclose = new AlgoAprioriTIDClose(); Itemsets patterns = aclose.runAlgorithm(database, minsupp, null); aclose.printStats(); AlgoClosedRules algoClosedrules = new AlgoClosedRules(); AssRules.ClosedRules rules = algoClosedrules.runAlgorithm(patterns, minconf, output); algoClosedrules.printStatistics(); //Console.WriteLine("Printer reglerne:"); if (rules != null) { rules.printRules(database.size()); } }
private double DetermineLikelihood(Disease disease, ISet <int> setOfSymptoms) { double likelihood = 0d; IList <Itemsets> symptomFrequencies = diseaseItemsets[disease.DiseaseName]; if (setOfSymptoms.Count() <= symptomFrequencies.Count()) { Itemsets searchingItemsets = symptomFrequencies[setOfSymptoms.Count() - 1]; foreach (ISet <int> items in searchingItemsets.SetsFrequency.Keys) { if (setOfSymptoms.SetEquals(items)) { likelihood = searchingItemsets.SetsFrequency[items]; } } } ISet <int> diseaseSymptoms = _context.SymptomDiseases.Where(symDis => symDis.DiseaseID == disease.Id) .Select(symDis => symDis.SymptomID).ToHashSet(); if (likelihood == 0d) { if (setOfSymptoms.SetEquals(diseaseSymptoms) || setOfSymptoms.IsSubsetOf(diseaseSymptoms)) { likelihood = 1d; if (setOfSymptoms.Count() < 3) { likelihood = likelihood * diseaseSymptoms.Count() / setOfSymptoms.Count(); } } else { likelihood = 0d; } } return(likelihood); }
/** * Run the algorithm * @param minsupp the minsup threshold * @param outputFile an output file path, if the result should be saved otherwise * leave it null and this method will keep the result into memory and return it. * @return the set of itemsets found if the user chose to save the result to memory * @throws IOException exception if error writing the output file */ public Itemsets runAlgorithm(TransactionDatabase database, double minsupp, String outputFile) { // record start time //startTimestamp = AlgoClosedRules.CurrentMillis(); // reset number of itemsets found itemsetCount = 0; // if the user want to keep the result into memory if (outputFile == null) { writer = null; patterns = new Itemsets("FREQUENT CLOSED ITEMSETS"); } else // if the user want to save the result to a file { patterns = null; writer = new StreamWriter(outputFile); } this.minSuppRelative = (int)Math.Ceiling(minsupp * database.size()); if (this.minSuppRelative == 0) // protection { this.minSuppRelative = 1; } // (1) count the tid set of each item in the database in one database // pass mapItemTIDS = new Dictionary <int, HashSet <int> >(); // key : item value: tidset of the item // for each transaction for (int j = 0; j < database.getTransactions().Count(); j++) { List <int> transaction = database.getTransactions().ElementAt(j); // for each item in the transaction for (int i = 0; i < transaction.Count(); i++) { // update the tidset of the item HashSet <int> ids; if (!mapItemTIDS.TryGetValue(transaction.ElementAt(i), out ids)) { ids = new HashSet <int>(); mapItemTIDS.Add(transaction.ElementAt(i), ids); } ids.Add(j); } } // save the database size databaseSize = database.getTransactions().Count(); // To build level 1, we keep only the frequent items. // We scan the database one time to calculate the support of each // candidate. k = 1; List <Itemset> level = new List <Itemset>(); // For each item //Udkommenteret indtil videre /*Iterator<Entry<Integer, Set<Integer>>> iterator = mapItemTIDS.entrySet().iterator(); * while (iterator.hasNext()) { * Map.Entry<Integer, Set<Integer>> entry = (Map.Entry<Integer, Set<Integer>>) iterator.next(); * * if (entry.getValue().size() >= minSuppRelative) { // if the item is * // frequent * Integer item = entry.getKey(); * Itemset itemset = new Itemset(item); * itemset.setTIDs(mapItemTIDS.get(item)); * level.add(itemset); * } else { * iterator.remove(); // if the item is not frequent we don't * // need to keep it into memory. * } * } * * // sort itemsets of size 1 according to lexicographical order. * Collections.sort(level, new IComparer<Itemset>() { * public int compare(Itemset o1, Itemset o2) { * return o1.get(0) - o2.get(0); * } * });*/ IEnumerator <KeyValuePair <int, HashSet <int> > > iterator = mapItemTIDS.GetEnumerator(); while (iterator.MoveNext()) { KeyValuePair <int, HashSet <int> > entry = (KeyValuePair <int, HashSet <int> >)iterator.Current; if (entry.Value.Count() >= minSuppRelative) // if the item is { // frequent int item = entry.Key; Itemset itemset = new Itemset(item); itemset.setTIDs(mapItemTIDS[item]); level.Add(itemset); } else { //iterator.Dispose(); // if the item is not frequent we don't // need to keep it into memory. } } // sort itemsets of size 1 according to lexicographical order. //Collections.sort(level, new IComparerAnonymousInnerClassHelper(this)); level.Sort( delegate(Itemset o1, Itemset o2) { return(o1.get(0) - o2.get(0)); }); // Generate candidates with size k = 1 (all itemsets of size 1) k = 2; // While the level is not empty //TODO: Tjek om 10 ikke skal ændres //setMaxItemsetSize(Int32.MaxValue); while (level.Any() && k <= maxItemsetSize) { // We build the level k+1 with all the candidates that have // a support higher than the minsup threshold. List <Itemset> levelK = generateCandidateSizeK(level); // We check all sets of level k-1 for closure checkIfItemsetsK_1AreClosed(level, levelK); level = levelK; // We keep only the last level... k++; } // save end time endTimestamp = AlgoClosedRules.CurrentTimeMillis(); // close the output file if the result was saved to a file if (writer != null) { writer.Close(); } return(patterns); // Return all frequent itemsets found! }
public ClosedRules runAlgorithm(Itemsets closedItemsets, double minconf, string outputFile) { this.closedItemsets = closedItemsets; // if the user want to keep the result into memory if (outputFile == null) { writer = null; rules = new ClosedRules("Closed association rules"); } else { // if the user want to save the result to a file //rules = null; writer = new System.IO.StreamWriter(outputFile); } startTimestamp = CurrentTimeMillis(); this.minconf = minconf; //For each frequent itemset of size >=2 for (int k = 2; k < closedItemsets.getLevels().Count; k++) { foreach (Itemset lk in closedItemsets.getLevels().ElementAt(k)) { //} //for(Itemset lk : closedItemsets.getLevels().get(k)){ // create H1 HashSet <Itemset> H1 = new HashSet <Itemset>(); foreach (int item in lk.getItems()) { //for(Integer item : lk.getItems()){ // THIS PART WAS CHANGED Itemset itemset = new Itemset(item); H1.Add(itemset); } HashSet <Itemset> H1_for_recursion = new HashSet <Itemset>(); foreach (Itemset hm_P_1 in H1) { //for(Itemset hm_P_1 : H1){ Itemset itemset_Lk_minus_hm_P_1 = lk.cloneItemSetMinusAnItemset(hm_P_1); //WRONG TODO int supLkMinus_hm_P_1 = calculateSupport(itemset_Lk_minus_hm_P_1); // THIS COULD BE DONE ANOTHER WAY ? int supLk = calculateSupport(lk); // IT COULD PERHAPS BE IMPROVED.... double conf = ((double)supLk) / ((double)supLkMinus_hm_P_1); if (conf >= minconf) { ClosedRule rule = new ClosedRule(itemset_Lk_minus_hm_P_1, hm_P_1, lk.getAbsoluteSupport(), conf); save(rule); H1_for_recursion.Add(hm_P_1);// for recursion } } // call apGenRules apGenrules(k, 1, lk, H1_for_recursion); } } endTimeStamp = CurrentTimeMillis(); // if the user chose to save to a file, we close the file. if (writer != null) { writer.Close(); } return(rules); }