SOFTRON
N
e
w
M
a
g
i
c
W
o
r
l
d
O
f
T
e
c
h
n
o
l
o
g
y
service@softron.in
support@softron.in
Visitors: , Visitors Today:
Home
Company
Clients
Product
IT Consultancy
Services
Our team
Training
Privacy Policy
Support
Payment Method
About Us
Online Marketing & Film Making
Online Advertising
Advertising Film
Film Production
Chrome Effects
Automation
Industry Planner
Advance Technology
Software Technology
Source Code
Blog
Article
News
Open Source
SEO
Click For Menu
Home
Company
- - - Clients
- - - Product
- - - IT Consultancy
- - - Services
- - - Our team
- - - Training
- - - Privacy Policy
- - - Support
- - - Payment Method
- - - About Us
Online Marketing & Film Making
- - - Online Advertising
- - - Advertising Film
- - - Film Production
- - - Chrome Effects
Automation
- - - Industry Planner
- - - Advance Technology
- - - Software Technology
Source Code
Blog
Article
News
Open Source
SEO
Source Code : C # implementation Apriori algorithm (source code)
C # implementation Apriori algorithm (source code)
/*Program.cs*/
using System;
using System.Collections.
Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Text.RegularExpressions;
using System.Collections;
namespace testApriori1
{
publicclass TestApriori
{
staticvoid Main(string[] args)
{
Console.WriteLine(DateTime.Now);
ArrayList D = GetEventsFromDB();
//
ArrayList I = GetItems1FromDB();
//
float s = 0.01f;//
List
L =
new List
();//
L = Apriori(D, I, s);
for (int i = 0; i < L.Count; i++)
{
Console.WriteLine(L[i].Items);
Console.WriteLine(L[i].Sup);
}
Console.WriteLine(DateTime.Now);
Console.Read();
}
#region-----Apriori-----
///
/// Apriori
///
///
///
///
///
static List
Apriori(ArrayList D,ArrayList I,
float sup)
{
List
L =
new List
();//
if (I.Count == 0)
return
L;
else
{
int[] Icount = newint[I.Count];//,0
ArrayList Ifrequent =
new ArrayList();//
//?
Regex r=
new Regex(",");
for (int i = 0; i < D.Count; i++)
{
string[] subD=r.Split(D[i].
ToString());
for (int j = 0; j < I.Count;j++ )
{
string[] subI = r.Split(I[j].ToString());
bool subIInsubD=true;
for(int m=0;m
{
bool subImInsubD=false;
for(int n=0;n
if (subI[m] == subD[n])
{
subImInsubD =
true;
continue
;
}
if(subImInsubD==false) subIInsubD=false;
}
if(subIInsubD==true) Icount[j]++;
}
}
//L
for (int i = 0; i < Icount.Length;i++ )
{
if (Icount[i] >= sup * D.Count)
{
Ifrequent.Add(I[i]);
ItemSet iSet =
new ItemSet();
iSet.Items=I[i].ToString();
iSet.Sup = Icount[i];
L.Add(iSet);
}
}
I.Clear();
I = AprioriGen(Ifrequent);
L.AddRange(Apriori(D, I, sup));
return L;
}
}
#endregion-----Apriori-----
#region-------Apriori-gen---------
///
/// Apriori-gen
///
///
///
static ArrayList AprioriGen(ArrayList L)
{
ArrayList Lk=
new ArrayList();
Regex r=
new Regex(",");
for(int i=0;i
{
string[] subL1 = r.Split(L[i].ToString());
for(int j=i+1;j
{
string[] subL2 = r.Split(L[j].ToString());
//Ltemp
string temp = L[j].ToString();//
for(int m=0;m
{
bool subL1mInsubL2=false;
for(int n=0;n
{
if (subL1[m] == subL2[n]) subL1mInsubL2 = true;
}
if (subL1mInsubL2 == false) temp = temp + "," + subL1[m];
}
//temp?L?+1temp
string[] subTemp = r.Split(temp);
if (subTemp.Length == subL1.Length + 1)
{
bool isExists = false;
for (int m = 0; m < Lk.Count; m++)
{
bool isContained = true;
for (int n = 0; n < subTemp.Length; n++)
{
if (!Lk[m].ToString().Contains(subTemp[n])) isContained=false;
}
if (isContained == true) isExists = true;
}
if(isExists==false) Lk.Add(temp);
}
}
}
return Lk;
}
#endregion-------Apriori-gen---------
#region----------------
///
///
///
///
static ArrayList GetItems1FromDB()
{
string commandString = "select distinct Model from dbo.vAssocSeqLineItems";
DataSet ds = ExcuteDataSetByCommandString(commandString);
int countItems1 = 0;
countItems1 = ds.Tables[0].Rows.Count;
ArrayList Items1 =
new ArrayList();
for (int i = 0; i < countItems1; i++)
Items1.Add(ds.Tables[0].Rows[i][
"Model"].ToString());
return Items1;
}
#endregion----------------
#region----------------
///
///
///
///
static ArrayList GetEventsFromDB()
{
string commandString="
select count(OrderNumber) from vAssocSeqOrders"
;
DataSet ds=ExcuteDataSetByCommandString(commandString);
int countEvent = Convert.ToInt32(ds.Tables[0].Rows[0][0]);//
ArrayList
events=new ArrayList();
string temp=null;
string orderNumber=null;//OrderNumber,Order
//
commandString =
"select OrderNumber,Model from vAssocSeqLineItems";
ds = ExcuteDataSetByCommandString(commandString);
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
if (orderNumber == null)//
{
orderNumber = ds.Tables[0].Rows[i][
"OrderNumber"].ToString();
temp = ds.Tables[0].Rows[i][
"Model"].ToString();
}
elseif (orderNumber == ds.Tables[0].Rows[i]["OrderNumber"].ToString())//Order
{
temp = temp +
","+ ds.Tables[0].Rows[i]["Model"];
}
else//Order
{
events.Add(temp);
orderNumber = ds.Tables[0].Rows[i][
"OrderNumber"].ToString();
temp=ds.Tables[0].Rows[i][
"Model"].ToString();
}
if (i == ds.Tables[0].Rows.Count - 1) events.Add(temp);
}
return events;
}
#endregion----------------
#region-----commandString------
///
/// commandString
///
///
///
static DataSet ExcuteDataSetByCommandString(string commandString)
{
string connectionString = "server=.;database=AdventureWorksDW;trusted_connection=true";
SqlConnection sqlCon =
new SqlConnection(
connectionString);
sqlCon.Open();
SqlCommand sqlCmd =
new SqlCommand();
sqlCmd.Connection = sqlCon;
sqlCmd.CommandText = commandString;
DataSet ds =
new DataSet();
SqlDataAdapter sqlAdapter =
new SqlDataAdapter(sqlCmd);
sqlAdapter.Fill(ds);
sqlCon.Close();
return ds;
}
#endregion-----commandString------
}
}
Copyright © 2011 - All Rights Reserved -
Softron.in
Template by
Softron Technology