Apriori Class |
Namespace: Accord.MachineLearning.Rules
The Apriori type exposes the following members.
Name | Description | |
---|---|---|
Frequent |
Gets the set of most frequent items and the respective
number of times their appear in in the training dataset.
(Inherited from AprioriT.) | |
Token |
Gets or sets a cancellation token that can be used to
stop the learning algorithm while it is running.
(Inherited from AprioriT.) |
Name | Description | |
---|---|---|
Equals | Determines whether the specified object is equal to the current object. (Inherited from Object.) | |
Finalize | Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.) | |
GetHashCode | Serves as the default hash function. (Inherited from Object.) | |
GetType | Gets the Type of the current instance. (Inherited from Object.) | |
Learn(SortedSetT, Double) |
Learns a model that can map the given inputs to the desired outputs.
(Inherited from AprioriT.) | |
Learn(T, Double) |
Learns a model that can map the given inputs to the desired outputs.
(Inherited from AprioriT.) | |
MemberwiseClone | Creates a shallow copy of the current Object. (Inherited from Object.) | |
ToString | Returns a string that represents the current object. (Inherited from Object.) |
Name | Description | |
---|---|---|
HasMethod |
Checks whether an object implements a method with the given name.
(Defined by ExtensionMethods.) | |
IsEqual |
Compares two objects for equality, performing an elementwise
comparison if the elements are vectors or matrices.
(Defined by Matrix.) | |
To(Type) | Overloaded.
Converts an object into another type, irrespective of whether
the conversion can be done at compile time or not. This can be
used to convert generic types to numeric types during runtime.
(Defined by ExtensionMethods.) | |
ToT | Overloaded.
Converts an object into another type, irrespective of whether
the conversion can be done at compile time or not. This can be
used to convert generic types to numeric types during runtime.
(Defined by ExtensionMethods.) |
References:
// Example from https://en.wikipedia.org/wiki/Apriori_algorithm // Assume that a large supermarket tracks sales data by stock-keeping unit // (SKU) for each item: each item, such as "butter" or "bread", is identified // by a numerical SKU. The supermarket has a database of transactions where each // transaction is a set of SKUs that were bought together. // Let the database of transactions consist of following itemsets: SortedSet<int>[] dataset = { // Each row represents a set of items that have been bought // together. Each number is a SKU identifier for a product. new SortedSet<int> { 1, 2, 3, 4 }, // bought 4 items new SortedSet<int> { 1, 2, 4 }, // bought 3 items new SortedSet<int> { 1, 2 }, // bought 2 items new SortedSet<int> { 2, 3, 4 }, // ... new SortedSet<int> { 2, 3 }, new SortedSet<int> { 3, 4 }, new SortedSet<int> { 2, 4 }, }; // We will use Apriori to determine the frequent item sets of this database. // To do this, we will say that an item set is frequent if it appears in at // least 3 transactions of the database: the value 3 is the support threshold. // Create a new a-priori learning algorithm with support 3 Apriori apriori = new Apriori(threshold: 3, confidence: 0); // Use the algorithm to learn a set matcher AssociationRuleMatcher<int> classifier = apriori.Learn(dataset); // Use the classifier to find orders that are similar to // orders where clients have bought items 1 and 2 together: int[][] matches = classifier.Decide(new[] { 1, 2 }); // The result should be: // // new int[][] // { // new int[] { 4 }, // new int[] { 3 } // }; // Meaning the most likely product to go alongside the products // being bought is item 4, and the second most likely is item 3. // We can also obtain the association rules from frequent itemsets: AssociationRule<int>[] rules = classifier.Rules; // The result will be: // { // [1] -> [2]; support: 3, confidence: 1, // [2] -> [1]; support: 3, confidence: 0.5, // [2] -> [3]; support: 3, confidence: 0.5, // [3] -> [2]; support: 3, confidence: 0.75, // [2] -> [4]; support: 4, confidence: 0.66, // [4] -> [2]; support: 4, confidence: 0.8, // [3] -> [4]; support: 3, confidence: 0.75, // [4] -> [3]; support: 3, confidence: 0.6 // };
// Example from http://www3.cs.stonybrook.edu/~cse634/lecture_notes/07apriori.pdf // For this example, we will consider a database consisting of 9 transactions: // - Minimum support count required will be 2 (i.e. min_sup = 2 / 9 = 22%); // - Minimum confidence required should be 70%; // The dataset of transactions can be as follows: string[][] dataset = { new string[] { "1", "2", "5" }, new string[] { "2", "4" }, new string[] { "2", "3" }, new string[] { "1", "2", "4" }, new string[] { "1", "3" }, new string[] { "2", "3" }, new string[] { "1", "3" }, new string[] { "1", "2", "3", "5" }, new string[] { "1", "2", "3" }, }; // Create a new A-priori learning algorithm with the requirements var apriori = new Apriori<string>(threshold: 2, confidence: 0.7); // Use apriori to generate a n-itemset generation frequent pattern AssociationRuleMatcher<string> classifier = apriori.Learn(dataset); // Generate association rules from the itemsets: AssociationRule<string>[] rules = classifier.Rules; // The result should be: // { // [5] -> [1]; support: 2, confidence: 1, // [5] -> [2]; support: 2, confidence: 1, // [4] -> [2]; support: 2, confidence: 1, // [5] -> [1 2]; support: 2, confidence: 1, // [1 5] -> [2]; support: 2, confidence: 1, // [2 5] -> [1]; support: 2, confidence: 1, // }