DataSharp/Data/Analysis/Apriori.cs
/// <summary>
/// Generate candidate itemsets with k items from frequent (k-1)-itemsets.
/// </summary>
/// <param name="frequentItemsets">The frequent (k-1)-itemsets.</param>
/// <returns>Candidate itemsets with k items</returns>
Generate candidate itemsets with k items from frequent (k-1)-itemsets.
Generate candidate itemsets with k items from frequent ( k - 1 ) - itemsets .
private static Dictionary<SortedSet<string>, int> GenerateNextCandidates(IEnumerable<SortedSet<string>> frequentItemsets)
        {
            Dictionary<SortedSet<string>, int> candidateItemsets = new Dictionary<SortedSet<string>, int>();
            SortedSet<string>[] itemsets = frequentItemsets.ToArray();
            for (int i = 0; i < itemsets.Length - 1; i++)
            {
                for (int j = i + 1; j < itemsets.Length; j++)
                {
                    SortedSet<string> frequentItemset1 = itemsets[i];
                    SortedSet<string> frequentItemset2 = itemsets[j];
                    SortedSet<string> potentialCandidateItemset = new SortedSet<string>(frequentItemset1);
                    potentialCandidateItemset.UnionWith(frequentItemset2);
                    if (potentialCandidateItemset.Count == frequentItemset1.Count + 1)
                    {
                        if (SetHelper.ContainsSet(candidateItemsets.Keys, potentialCandidateItemset))
                            continue;
                        if (!HasInfrequentSubset(potentialCandidateItemset, frequentItemsets))
                            candidateItemsets.Add(potentialCandidateItemset, 0);
                    }
                }
            }
            return candidateItemsets;
        }
private static Dictionary < SortedSet < string > , int > GenerateNextCandidates ( IEnumerable < SortedSet < string > > frequentItemsets ) { Dictionary < SortedSet < string > , int > candidateItemsets = new Dictionary < SortedSet < string > , int > ( ) ; SortedSet < string > [ ] itemsets = frequentItemsets . ToArray ( ) ; for ( int i = 0 ; i < itemsets . Length - 1 ; i ++ ) { for ( int j = i + 1 ; j < itemsets . Length ; j ++ ) { SortedSet < string > frequentItemset1 = itemsets [ i ] ; SortedSet < string > frequentItemset2 = itemsets [ j ] ; SortedSet < string > potentialCandidateItemset = new SortedSet < string > ( frequentItemset1 ) ; potentialCandidateItemset . UnionWith ( frequentItemset2 ) ; if ( potentialCandidateItemset . Count == frequentItemset1 . Count + 1 ) { if ( SetHelper . ContainsSet ( candidateItemsets . Keys , potentialCandidateItemset ) ) continue ; if ( ! HasInfrequentSubset ( potentialCandidateItemset , frequentItemsets ) ) candidateItemsets . Add ( potentialCandidateItemset , 0 ) ; } } } return candidateItemsets ; }
Generate candidate itemsets with k items from frequent (k-1)-itemsets.
Generate candidate itemsets with k items from frequent ( k - 1 ) - itemsets .
// Initialize an empty Dictionary<SortedSet<string>, int> instance to store the candidate itemsets. // Get the array of frequent (k-1)-itemsets. // Traverse through the frequent (k-1)-itemsets. // Get 2 frequent (k-1)-itemsets. // Get the superset of 2 selected frequent (k-1)-itemsets. // Generate candidate k-itemsets if the number of elements in the superset is k, // where (k-1) is the number of elements in the selected frequent itemsets. // Skip this potential candidate itemset if it exists in the collection to return. // Add this potential candidate itemset to the collection of candidate k-itemsets if its every subset with (k-1) elements is contained in the frequent (k-1)-itemsets. // Return the candidate itemsets with k items.
Candidate itemsets with k items
Candidate itemsets with k items
frequentItemsets
IEnumerable<SortedSet<string>>
The frequent (k-1)-itemsets.
The frequent ( k - 1 ) - itemsets .