📄 apriori.cs
字号:
using System;
using System.Data;
using System.Text;
using System.Collections;
using VISUAL_BASIC_DATA_MINING_NET;
using VISUAL_BASIC_DATA_MINING_NET.CustomEvents;
/// <summary>
/// The VISUAL_BASIC_DATA_MINING_NET namespace contains namespaces and classes used by this assembly.
/// </summary>
namespace VISUAL_BASIC_DATA_MINING_NET
{
/// <summary>
/// The VISUAL_BASIC_DATA_MINING_NET.APriori namespace contains class and methods used to implement the APriori
/// Market Based Analysis Data Mining Algorithm.
/// </summary>
/// <remarks>
namespace APriori
{
/// <summary>
/// Apriori class implements the APriori algorithm for market based analysis.
/// </summary>
/// <remarks>
/// This class implements the APriori algorithm and creates association between items in a transaction.
/// </remarks>
/// <example >
/// using APriori;
///
/// Apriori shoppingCart = new Apriori();
/// </example>
/// <include file='APrioriExamples.xml' path='Documentation/SourceCode[@name="Copyright"]/*' />
/// <include file='APrioriExamples.xml' path='Documentation/SourceCode[@name="StartingSampleA"]/*' />
public class Apriori
{
/// <summary>
/// An ItemsetArrayList variable that stores a collection of frequent itemsets.
/// </summary>
protected ItemsetArrayList itemsetsFrequentCollection;
/// <summary>
/// An ItemsetArrayList variable that stores a collection of candidate itemsets.
/// </summary>
protected ItemsetArrayList itemsetsCandidateCollection;
/// <summary>
/// The default parameterless constructor for the Apriori class.
/// </summary>
public Apriori()
{
this.itemsetsCandidateCollection = new ItemsetArrayList();
this.itemsetsFrequentCollection = new ItemsetArrayList();
}
/// <summary>
/// The public OnProgressMonitorEvent raises the ProgressMonitorEvent event by invoking
/// the delegates. The sender is always this, the current instance of the class.
/// </summary>
/// <param name="e">
/// A CustomEvents.ProgressMonitorEventArgs object.
/// </param>
/// <remarks>
/// This method is used to invoke a dalegate that notifies clients about the progress of an executing code.
/// </remarks>
public void OnProgressMonitorEvent(ProgressMonitorEventArgs e)
{
if (ProgressMonitorEvent != null)
{
// Invokes the delegates.
ProgressMonitorEvent(this, e);
}
}
/// <summary>
/// A custom event that notifies clients about the progress of the executing code.
/// </summary>
public event ProgressMonitorEventHandler ProgressMonitorEvent;
/// <summary>
/// Finds a collection of DataRows matching a filter expression.
/// </summary>
/// <param name="find">
/// The expression used to filter the DataRows in a table.
/// </param>
/// <param name="data">
/// A DataTable object containing the data to be filtered.
/// </param>
/// <returns>
/// Returns a collection of DataRow objects.
/// </returns>
protected DataRow [] FindItems(string find, DataTable data)
{
return data.Select(find);
}
/// <summary>
/// Counts the number of times a string is found in another string.
/// </summary>
/// <param name="find">
/// The string to find.
/// </param>
/// <param name="data">
/// The string to search.
/// </param>
/// <returns>
/// Returns an integer count of the number of times a string is found in another string.
/// </returns>
protected int FindItems(string find, string data)
{
string [] splitstring = data.Split(new Char[] {','});
int length = splitstring.Length;
int countFound = 0;
string [] found = new string[length];
for(int counter = 0; counter < length; counter++)
{
found[counter] = splitstring[counter].Trim();
}
foreach (string member in found)
{
if (member == find)
{
countFound++;
}
}
return countFound;
}
/// <summary>
/// Counts the number of times a string is found in another string.
/// </summary>
/// <param name="find">
/// The string to find.
/// </param>
/// <param name="data">
/// The string to search.
/// </param>
/// <returns>
/// An ItemsetArrayList object.
/// </returns>
protected int FindItems(ItemsetArrayList find, string data)
{
string [] splitstring = data.Split(new Char[] {','});
int length = find.Count;
string [] found = new string[splitstring.Length];
int minimumValue = 0;
int [] search = new int[length];
for(int counter =0; counter < splitstring.Length; counter++)
{
found[counter] = splitstring[counter].Trim();
}
for(int count = 0; count < length; count++)
{
foreach (string member in found)
{
if (member == (string)find[count])
{
search[count]++;
}
}
}
switch(length)
{
case 0:
{
minimumValue = 0;
break;
}
case 1:
{
minimumValue = search[0];
break;
}
default:
{
for(int counter = 0; counter < search.Length; counter++)
{
if(counter == 0)
{
minimumValue = search[counter];
}
else
{
if (search[counter] < minimumValue)
{
minimumValue = search[counter];
}
}
}
break;
}
}
return minimumValue;
}
/// <summary>
/// Retrieves the support count of an item in the transactions database.
/// </summary>
/// <param name="find">
/// An item to retrieve the support count for.
/// </param>
/// <param name="Transactions_Data">
/// The database of transactions to be analyzed using the APriori Algorithm.
/// </param>
/// <returns>
/// Returns an integer value representing the support count for the item.
/// </returns>
public int SupportCount(string find, Database Transactions_Data)
{
int count = 0;
DataTable datatable = Transactions_Data.Transactions.Tables[0];
foreach(DataRow datarow in datatable.Rows)
{
count = count + FindItems(find, (datarow["Transactions"]).ToString());
}
return count;
}
/// <summary>
/// Gets the support count of an item.
/// </summary>
/// <param name="find">
/// The item to find the support count for.
/// </param>
/// <param name="transactionsData">
/// The database of transactions to analyze for associations.
/// </param>
/// <returns>
/// Returns the support count of an item as an integer.
/// </returns>
public int SupportCount(ItemsetArrayList find, Database transactionsData)
{
int count = 0;
int total = 0;
DataTable dataTable = transactionsData.Transactions.Tables["TransactionTable"];
foreach(DataRow datarow in dataTable.Rows)
{
count = this.FindItems(find, (datarow["Transactions"]).ToString());
total = count + total;
}
return total;
}
/// <summary>
/// Creates a collection of unique items from a database of transactions.
/// </summary>
/// <param name="dataBase">
/// The ADO.NET in-memory database to search for unique items.
/// </param>
[ReservedAttribute(false,"December 25, 2002")]
public ItemsetCandidate CreateOneItemsets(Database dataBase)
{
DataTable dataTable = dataBase.Transactions.Tables["TransactionTable"];
ItemsetCandidate candidateItemset = new ItemsetCandidate();
ItemsetArrayList uniqueItems = new ItemsetArrayList(1);;
ItemsetArrayList candidateItems;
ItemsetArrayList items;
StringBuilder item = new StringBuilder(10);
int itemSupportCount = 0;
int counter = 1;
string msg = "Creating One Itemsets";
ProgressMonitorEventArgs e = new ProgressMonitorEventArgs(1,100,80,"Apriori.CreateOneItemsets(Database)",msg );
this.OnProgressMonitorEvent(e);
foreach (DataRow dataRow in dataTable.Rows)
{
item.Append(dataRow["Transactions"].ToString());
if(counter < (dataTable.Rows.Count))
{
item.Append(", ");
counter++;
}
}
candidateItems = ItemsetArrayList.ConvertToItemsetArrayList(item.ToString(), new Char[] {','});
for(int count = 0; count < candidateItems.Count; count++)
{
item = new StringBuilder(10);
item.Append(((string) candidateItems[count]).Trim());
if(!(item.ToString() == ""))
{
if(!uniqueItems.Contains(item.ToString()))
{
itemSupportCount = this.SupportCount(item.ToString(),dataBase);
dataBase.AddItemset(item.ToString(),1,itemSupportCount);
items = new ItemsetArrayList(1);
uniqueItems.Add(item.ToString());
items.Add(item.ToString());
items.Level = 1;
items.SupportCount = itemSupportCount;
items.TrimToSize();
candidateItemset.Items.Add(items);
}
}
}
candidateItemset.Items.TrimToSize();
candidateItemset.Level = 1;
return candidateItemset;
}
/// <summary>
/// Generates both frequent and candidate itemsets and adds them to their collections.
/// </summary>
/// <param name="Candidate_Itemset">
/// The candidate itemset to join.
/// </param>
/// <param name="TransactionsData">
/// The database of transactions data to be analyzed.
/// </param>
/// <param name="minimum_support">
/// The minimum support for the candidate itemsets in the transaction database.
/// </param>
public void AprioriGenerator(ItemsetCandidate Candidate_Itemset, Database TransactionsData, int minimum_support)
{
string start = "Generating Level " + Candidate_Itemset.Level + " Candidates : " + Candidate_Itemset.Items. Count + " Items";
ProgressMonitorEventArgs e = new ProgressMonitorEventArgs(1,100,25,"Apriori.AprioriGenerator()",start );
this.OnProgressMonitorEvent(e);
//add a candidate itemset to the candidate itemsets collection
ItemsetCandidate candidateItemset = this.JoinCandidateItemsets(Candidate_Itemset, TransactionsData, minimum_support);
if(candidateItemset.Items.Count > 0)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -