Click or drag to resize
Accord.NET (logo)

AprioriT Class

A-priori algorithm for association rule mining.
Inheritance Hierarchy
SystemObject
  Accord.MachineLearning.RulesAprioriT
    Accord.MachineLearning.RulesApriori

Namespace:  Accord.MachineLearning.Rules
Assembly:  Accord.MachineLearning (in Accord.MachineLearning.dll) Version: 3.8.0
Syntax
public class Apriori<T> : IUnsupervisedLearning<AssociationRuleMatcher<T>, SortedSet<T>, SortedSet<T>[]>, 
	IUnsupervisedLearning<AssociationRuleMatcher<T>, T[], T[][]>
Request Example View Source

Type Parameters

T
The dataset item type. Default is int.

The AprioriT type exposes the following members.

Constructors
  NameDescription
Public methodAprioriT
Initializes a new instance of the AprioriT class.
Top
Properties
  NameDescription
Public propertyFrequent
Gets the set of most frequent items and the respective number of times their appear in in the training dataset.
Public propertyToken
Gets or sets a cancellation token that can be used to stop the learning algorithm while it is running.
Top
Methods
  NameDescription
Public methodEquals
Determines whether the specified object is equal to the current object.
(Inherited from Object.)
Protected methodFinalize
Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection.
(Inherited from Object.)
Public methodGetHashCode
Serves as the default hash function.
(Inherited from Object.)
Public methodGetType
Gets the Type of the current instance.
(Inherited from Object.)
Public methodLearn(SortedSetT, Double)
Learns a model that can map the given inputs to the desired outputs.
Public methodLearn(T, Double)
Learns a model that can map the given inputs to the desired outputs.
Protected methodMemberwiseClone
Creates a shallow copy of the current Object.
(Inherited from Object.)
Public methodToString
Returns a string that represents the current object.
(Inherited from Object.)
Top
Extension Methods
  NameDescription
Public Extension MethodHasMethod
Checks whether an object implements a method with the given name.
(Defined by ExtensionMethods.)
Public Extension MethodIsEqual
Compares two objects for equality, performing an elementwise comparison if the elements are vectors or matrices.
(Defined by Matrix.)
Public Extension MethodTo(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.)
Public Extension MethodToTOverloaded.
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.)
Top
Remarks

References:

  • Anita Wasilewska, Lecture Notes. Available on http://www3.cs.stonybrook.edu/~cse634/lecture_notes/07apriori.pdf
>

Examples
C#
// 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 
// };
C#
// 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,
// }
See Also