DataSharp/Data/Analysis/Apriori.cs
/// <summary>
/// Computes all the frequent k-itemsets from the transactions extracted before.
/// </summary>
Computes all the frequent k-itemsets from the transactions extracted before.
Computes all the frequent k - itemsets from the transactions extracted before .
public override LinkedList<Dictionary<SortedSet<string>, int>> ComputeFrequentItemsets()
        {
            BuildTransactionSets();
            Dictionary<SortedSet<string>, int> nextFrequentItemsets = ComputeFrequentOneItemsets();
            while (nextFrequentItemsets.Count != 0)
            {
                frequentItemsets.AddLast(nextFrequentItemsets);
                Dictionary<SortedSet<string>, int> nextCandidateItemsets = GenerateNextCandidates(nextFrequentItemsets.Keys);
                KeyValuePair<SortedSet<string>, int>[] candidates = nextCandidateItemsets.ToArray();
                for (int i = 0; i < candidates.Length; i++)
                {
                    SortedSet<string> key = candidates[i].Key;
                    foreach (SortedSet<string> s in transactions)
                    {
                        if (key.IsSubsetOf(s))
                            nextCandidateItemsets[key]++;
                    }
                }
                var extractedItemsets =
                    from c in nextCandidateItemsets
                    where c.Value >= minSupportCount
                    select new { Key = c.Key, Value = c.Value };
                Dictionary<SortedSet<string>, int> extractedFrequentItemsets = new Dictionary<SortedSet<string>, int>();
                foreach (var itemset in extractedItemsets)
                    extractedFrequentItemsets.Add(itemset.Key, itemset.Value);
                nextFrequentItemsets = extractedFrequentItemsets;
            }
            return frequentItemsets;
        }
public override LinkedList < Dictionary < SortedSet < string > , int > > ComputeFrequentItemsets ( ) { BuildTransactionSets ( ) ; Dictionary < SortedSet < string > , int > nextFrequentItemsets = ComputeFrequentOneItemsets ( ) ; while ( nextFrequentItemsets . Count != 0 ) { frequentItemsets . AddLast ( nextFrequentItemsets ) ; Dictionary < SortedSet < string > , int > nextCandidateItemsets = GenerateNextCandidates ( nextFrequentItemsets . Keys ) ; KeyValuePair < SortedSet < string > , int > [ ] candidates = nextCandidateItemsets . ToArray ( ) ; for ( int i = 0 ; i < candidates . Length ; i ++ ) { SortedSet < string > key = candidates [ i ] . Key ; foreach ( SortedSet < string > s in transactions ) { if ( key . IsSubsetOf ( s ) ) nextCandidateItemsets [ key ] ++ ; } } var extractedItemsets = from c in nextCandidateItemsets where c . Value >= minSupportCount select new { Key = c . Key , Value = c . Value } ; Dictionary < SortedSet < string > , int > extractedFrequentItemsets = new Dictionary < SortedSet < string > , int > ( ) ; foreach ( var itemset in extractedItemsets ) extractedFrequentItemsets . Add ( itemset . Key , itemset . Value ) ; nextFrequentItemsets = extractedFrequentItemsets ; } return frequentItemsets ; }
Computes all the frequent k-itemsets from the transactions extracted before.
Computes all the frequent k - itemsets from the transactions extracted before .
// Extract all the transactions and store them before computing. // The nextFrequentItemsets variable tracks the frequent itemsets with the largest k we archived during the processing. // The k is 1 at first. // Loop until find the frequent itemsets that can not contain any more item. // Add the frequent 1-itemsets to the collection of frequent itemsets. // Get the next candidateItemsets. // Traverese through all the candidate k-itemsets. // Write operation is forbidden in the foreach-clause, but it is necessary to update the counter of the occurace of every candidate itemset. // So, use the IEnumberable<T>.ToArray() method to create an array that contains the same contents,  // and then we traverse through the array and update the counter. // Get the itemset as a key that will be used. // Update the counter if the itemset is a subset (not strictly) of some transaction. // Extract itemsets with occurance greater than the minimum support count. // This LINQ expression will be explained in the document. // Generate next frequent k-itemsets. // Create the Dictionary<TKey, TValue> object from the itemsets extracted above. // Update the nextFrequentItemsets with the max-k archived.