Vous êtes sur la page 1sur 16

Apriori Algorithm Report

Report of Apriori algorithm


 Developed by :
Dave Yogesh D. (07IT07 (07))
Panchal Bhargav D. (07IT21 (20))
Pambhar Sagar M. (07IT19 (18))
Patel Chintan J. (07IT26)
Patel Prayag . (07IT35)
Patel Jaimin D. (07It29 (28))
Seheju Vineet.(07IT51 (50))
Patel Sandeep G. (07It38(37))
Panchal Milan.(07IT22(21))
Kapdiya Deep.(07IT11)
Apriori Algorithm Report

Program.cs
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;

class Program
{
static void Main11()
{
DateTime dt;
dt = DateTime.Now;
//TimeSpan start = dt.TimeOfDay;
Console.WriteLine(dt);
//string file = @"c:\test1.csv";
string sup = "2";
string com = "0";

double support = double.Parse(sup);


double comf = double.Parse(com);
CSVReader cr = new CSVReader();
ItemSet data = cr.Read();

Console.WriteLine(r.Count);

Program p = new Program();


ItemSet a= p.apriori( data, support,comf);
for (int i = 0; i < a.Count;i++ )
{
ItemSet cur = (ItemSet)a[i];
for (int j = 0; j < cur.Count; j++) {
ItemSet now = (ItemSet)cur[j];
foreach (DataItem item in now)
{

Console.Write( " "+ item.Id + ":" +


item.ItemName+" ");

}
Console.WriteLine(" "+now.ICount);
}

}
// TimeSpan end = dt.TimeOfDay;
DateTime dt1 = DateTime.Now;
Console.WriteLine(dt1);
//TimeSpan final = end - start;
//Console.WriteLine(final);
Console.Read();
}
Apriori Algorithm Report
private ItemSet FindOneColSet(ItemSet data, double
support,double comf)
{
ItemSet cur=null;
ItemSet result = new ItemSet();

ItemSet set=null;
ItemSet newset=null;
DataItem cd=null;
DataItem td=null;
bool flag = true;

for (int i = 0; i < data.Count; i++) {


cur = (ItemSet)data[i];
for (int j = 0; j < cur.Count; j++) {
cd = (DataItem)cur[j];

for (int n = 0; n < result.Count; n++) {


set = (ItemSet)result[n];
td= (DataItem)set[0];
if (cd.Id == td.Id)
{
set.ICount++;
flag = false;
break;

}
flag=true;
}
if (flag) {
newset = new ItemSet();
newset.Add(cd);
result.Add(newset);
newset.ICount = 1;

}
ItemSet finalResult = new ItemSet();
for (int i = 0; i < result.Count; i++)
{
ItemSet con = (ItemSet)result[i];
if (con.ICount >= support && con.ICount>=comf)
{

finalResult.Add(con);
}
Apriori Algorithm Report

}
//finalResult.Sort();
return finalResult;

private ItemSet apriori(ItemSet data, double support,double


comf)
{

ItemSet result = new ItemSet();


ItemSet li = new ItemSet();
ItemSet conList = new ItemSet();
ItemSet subConList = new ItemSet();
ItemSet subDataList = new ItemSet();
int k = 2;
li.Add( new ItemSet());

li.Add(this.FindOneColSet(data,support,comf));

while (((ItemSet)li[k-1]).Count != 0)
{

conList = AprioriGenerate((ItemSet)li[k - 1],k-1,


support);
for (int i = 0; i < data.Count; i++)
{
subDataList = SubSet((ItemSet)data[i], k);
for (int j = 0; j < subDataList.Count; j++)
{
for (int n = 0; n < conList.Count; n++)
{
((ItemSet)subDataList[j]).Sort();
((ItemSet)conList[n]).Sort();
if
(((ItemSet)subDataList[j]).Equals(conList[n]))
{
((ItemSet)conList[n]).ICount++;

}
}

li.Add(new ItemSet());
for (int i = 0; i < conList.Count; i++)
{
ItemSet con = (ItemSet)conList[i];
if (con.ICount >= support)
{
Apriori Algorithm Report

((ItemSet)li[k]).Add(con);
}

k++;
}
for (int i = 0; i < li.Count; i++)
{

result.Add(li[i]);

}
return result;

private ItemSet AprioriGenerate(ItemSet li,int k, double


support)
{

ItemSet curList = null;


ItemSet durList = null;
ItemSet candi = null;
ItemSet result = new ItemSet();
for (int i = 0; i < li.Count; i++)
{
for (int j = 0; j < li.Count; j++)
{
bool flag = true ;
curList = (ItemSet)li[i];
durList = (ItemSet)li[j];
for (int n = 2; n < k; n++)
{

if (((DataItem)curList[n - 2]).Id ==
((DataItem)durList[n - 2]).Id)
{

flag = true;

}
else {
break;

flag = false;

}
Apriori Algorithm Report

if (flag && ((DataItem)curList[k - 1] ).Id<


((DataItem)durList[k - 1]).Id)
{

flag = true;
}
else {
flag = false;
}
if (flag)
{
candi = new ItemSet();

for(int m=0;m<k;m++){
candi.Add(durList[m]);

}
candi.Add(curList[k-1]);

if (HasInFrequentSubset(candi, li,k))
{
candi.Clear();

}
else
{
result.Add(candi);
}
}

}
}
return result;

private bool HasInFrequentSubset(ItemSet candidate, ItemSet


li,int k)
{
ItemSet subSet = SubSet(candidate,k);
ItemSet curList = null;
ItemSet liCurList = null;

for (int i = 0; i < subSet.Count; i++)


Apriori Algorithm Report
{
curList = (ItemSet)subSet[i];
for (int j = 0; j < li.Count; j++)
{

liCurList = (ItemSet)li[j];
if (liCurList.Equals(curList))
{
return false;

}
}
return true;;
}
private ItemSet SubSet(ItemSet set)
{
ItemSet subSet = new ItemSet();

ItemSet itemSet = new ItemSet();


int num = 1 << set.Count;

int bit;
int mask = 0; ;
for (int i = 0; i < num; i++)
{
itemSet = new ItemSet();
for (int j = 0; j < set.Count; j++)
{
mask = 1 << j;

bit = i & mask;


if (bit > 0)
{

itemSet.Add(set[j]);

}
}
if (itemSet.Count > 0)
{
subSet.Add(itemSet);
}

return subSet;
}
Apriori Algorithm Report
private ItemSet SubSet(ItemSet set, int t)
{
ItemSet subSet = new ItemSet();

ItemSet itemSet = new ItemSet();


int num = 1 << set.Count;

int bit;
int mask = 0; ;
for (int i = 0; i < num; i++)
{
itemSet = new ItemSet();
for (int j = 0; j < set.Count; j++)
{
mask = 1 << j;
bit = i & mask;
if (bit > 0)
{

itemSet.Add(set[j]);

}
}
if (itemSet.Count == t)
{
subSet.Add(itemSet);
}

return subSet;
}
}

Itemset.cs
using System;
using System.Data;
using System.Configuration;
using System.Web;

using System.Collections;

/// <summary>
/// Summary description for ItemSet
/// </summary>
public class ItemSet : ArrayList
{
public ItemSet()
{
Apriori Algorithm Report
//
// TODO: Add constructor logic here
//

private int icount = 0;

public int ICount


{
get { return icount; }
set { icount = value; }
}
public override object Clone()
{

ArrayList al = (ArrayList)base.Clone();
ItemSet set = new ItemSet();

for (int i = 0; i < al.Count; i++) {

set.Add(al[i]);
}

set.ICount = this.icount;
return set;
}
public override bool Equals(object obj)
{

ArrayList al = (ArrayList)obj;

//al.Sort(StringComparer.CurrentCulture);
//this.Sort(StringComparer.CurrentCulture);
if (al.Count != this.Count) return false;
for (int i = 0; i < al.Count; i++)
{

if (!al[i].Equals(this[i])) {
return false;
}
}
return true;

}
}

Dataitem.cs
using System;
using System.Collections.Generic;
using System.Text;
Apriori Algorithm Report

public class DataItem : IComparable


{
public DataItem()
{

}
public DataItem(int id)
{
this.Id = id;

}
public DataItem(int id, string itemName)
{
this.Id = id;
this.ItemName = itemName;

}
private string itemName;

public string ItemName


{
get { return itemName; }
set { itemName = value; }
}
private int id;

public int Id
{
get { return id; }
set { id = value; }
}

public override bool Equals(object obj)


{
DataItem di = (DataItem)obj;
return di.Id.Equals(this.Id);

#region IComparable Members

public int CompareTo(object obj)


{
DataItem di = (DataItem)obj;
return this.Id.CompareTo(di.Id);

#endregion
}
Apriori Algorithm Report

CsvReader.cs

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;

class CSVReader
{

public CSVReader() {

public ItemSet Read()


{
SqlConnection con = new SqlConnection(@"Data
Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\Yogesh Dave
07IT\Desktop\DMTEST\DMTEST\Database1.mdf;Integrated
Security=True;User Instance=True");
ItemSet rowSet = new ItemSet();
ItemSet colSet = null;
string col = "";
string st="";

string s1 = "select * from data";


string s2 = "select * from item";
SqlDataAdapter da=new SqlDataAdapter(s1, con);
SqlDataAdapter da2 = new SqlDataAdapter(s2,con);
DataSet ds=new DataSet();
DataSet ds2 = new DataSet();
da.Fill(ds, "data");
da2.Fill(ds2,"item");
DataRow dRow;
DataRow dRow2;

string[] head = new


string[ds2.Tables["item"].Rows.Count];

for (int i = 0; i < ds2.Tables["item"].Rows.Count; i++)


{
dRow2 = ds2.Tables["item"].Rows[i];
st= dRow2.ItemArray.GetValue(1).ToString();
head[i] = st;

}
Apriori Algorithm Report
for (int i = 0; i < ds.Tables["data"].Rows.Count; i+
+)
{
dRow = ds.Tables["data"].Rows[i];

colSet = new ItemSet();


int te = 0;
for (int y = 1; y < 6; y++)
{
col =
dRow.ItemArray.GetValue(y).ToString();
te = col.CompareTo("1");
if (te > 0)
{
colSet.Add(new DataItem(y, head[y -
1]));

rowSet.Add(colSet);

return rowSet;
}
}
Apriori Algorithm Report

Consider a database, D , consisting of 9


transactions.
Suppose min. support count required is 2 (i.e.
min_sup = 2/9 = 22 % )
Let minimum confidence required is 70%.
We have to first find out the frequent itemset
using Apriori algorithm.
Then, Association rules will be generated using
min. support & min. confidence.

 Follow the below mentioned details indicates how Apriori


works.

L1

Compare candidate support count with minimum support count


Apriori Algorithm Report

To discover the set of frequent 2-


itemsets, L2 , the algorithm uses L1 Join L1
to generate a candidate set of 2-itemsets,
C2 .
Next, the transactions in D are scanned
and the support count for each candidate
itemset in C2 is accumulated (as shown
in the middle table).
The set of frequent 2-itemsets, L2 , is then
determined, consisting of those
candidate 2-itemsets in C2 having
minimum support.
The generation of the set of candidate 3-itemsets,
Note: We haven’t used Apriori Property
C3 , involves use of the Apriori Property.
yet.
In order to find C3, we compute L2 Join L2.
C3 = L2 Join L2 = {{I1, I2, I3}, {I1, I2, I5}, {I1, I3, I5},
{I2, I3, I4}, {I2, I3, I5}, {I2, I4, I5}}.
Now, Join step is complete and Prune step will
be used to reduce the size of C3. Prune step helps
to avoid heavy computation due to large Ck.
Apriori Algorithm Report

Based on the Apriori property that all subsets of a


frequent itemset must also be frequent, we can
determine that four latter candidates cannot possibly
be frequent. How ?
For example , lets take {I1, I2, I3}. The 2-item subsets of
it are {I1, I2}, {I1, I3} & {I2, I3}. Since all 2-item subsets
of {I1, I2, I3} are members of L2, We will keep {I1, I2,
I3} in C3.
Lets take another example of {I2, I3, I5} which shows
how the pruning is performed. The 2-item subsets are
{I2, I3}, {I2, I5} & {I3,I5}.
BUT, {I3, I5} is not a member of L2 and hence it is not
frequent violating Apriori Property. Thus We will
have to remove {I2, I3, I5} from C3.
Therefore, C3 = {{I1, I2, I3}, {I1, I2, I5}} after checking
for all members of result of Join operation for
Pruning.
Now, the transactions in D are scanned in order to
determine L3, consisting of those candidates 3-itemsets
in C3 having minimum support.
Apriori Algorithm Report

The algorithm uses L3 Join L3 to generate


a candidate set of 4-itemsets, C4.
Although the join results in {{I1, I2, I3,
I5}}, this itemset is pruned since its
subset {{I2, I3, I5}} is not frequent.
Thus, C4 = φ , and algorithm terminates,
having found all of the frequent items.
This completes our Apriori Algorithm.
What’s Next ?
These frequent itemsets will be used
to generate strong association rules
( where strong association rules
satisfy both minimum support &
minimum confidence).

Vous aimerez peut-être aussi