㈠ 各位老師:誰有c#中apriori演算法的代碼可否把代碼發給我
放不完
//生成字元串的K項子集
static ArrayList SubSet(int k, string st)
{
int len = st.Length;
ArrayList al = new ArrayList();
string temp = "";
for (int i = 0; i < Math.Pow(2, len); i++)
{
int oneCount = 0;
String str = Convert.ToString(i, 2);
String str1 = str.PadLeft(len, '0');
/*try
{
int toBinary = Int32.Parse(str1);
}
catch (FormatException e)
{
Console.WriteLine(e.Message);
}*/
char[] toCharArray = str1.ToCharArray(); ;
temp = "";
for (int j = 0; j < toCharArray.Length; j++)
{
if (toCharArray[j] == '1')
{
oneCount++;
temp = temp + st[j];
}
}
if (oneCount == k)
{
al.Add (temp);
}
}
return al;
}
//生成候選項集
static ArrayList aprior_gen(ArrayList L)
{
//連接步驟
ArrayList LK = new ArrayList();
for (int i = 0; i < L.Count; i++)
{
string subL1 = L[i].ToString();
for (int j = i + 1; j < L.Count; j++)
{
string subL2 = L[j].ToString();
string temp = L[j].ToString();
Console.WriteLine(temp);
for (int m = 0; m < subL1.Length; m++)
{
bool L1mL2 = false;
for (int n = 0; n < subL2.Length; n++)
{
if (subL1[m] == subL2[n]) L1mL2 = true;
}
if (L1mL2 == false)
temp = temp + subL1[m];
}
//剪枝
if (temp.Length == subL1.Length + 1)
{
ArrayList temSub = SubSet(subL1.Length, temp);
for (int ts = 0; ts < temSub.Count; ts++)
{
if (!L.Contains(temSub[ts])) break;
}
LK.Add(temp);
}
}
} return LK;
}
//從資料庫中獲取數據並存於ArrayList中
static ArrayList getEventsFromDB()
{
string CommandString = "select name from goods";
DataSet ds = executeCommandString(CommandString);
//int eventCount=Convert .ToInt32 (ds.Tables [0].Rows [0][0]);
ArrayList events = new ArrayList();
string temp = null;
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
temp = ds.Tables[0].Rows[i]["name"].ToString();
events.Add(temp);
}
Console.WriteLine("資料庫中的記錄如下");
for (int j = 0; j < events.Count; j++)
{
Console.WriteLine(events[j]);
}
return events;
}
//生成頻繁項目集
/* static ArrayList apriorFrequent(float sup, ArrayList D)
{
//ArrayList D=getEventsFromDB ();
//ArrayList outL=new ArrayList ();//輸出頻繁項目集用outL存儲
Hashtable ht=find_frequent_1_item ();
ArrayList L1=new ArrayList ();//一項頻繁項目集
ArrayList L=new ArrayList ();//存放總的頻繁項目集
ArrayList allK=new ArrayList ();//存放資料庫的子集
foreach (DictionaryEntry de in ht) L1 .Add (ht .Keys );
for(int k=2;L[k -1]!=null;k++)
{
ArrayList cand=aprior_gen (L[k-1]);//候選集
for (int i=0;i<D.Count ;i++)
{
allK.Add(SubSet(k, D[i]));
}
//ArrayList C= SubSet (k,D[i]);
for(int m=0;m<cand .Count ;m++)
{
int cunt = 0;
for (int n = 0; n < allK.Count; n++)
{
if (cand[m] == allK[n]) cunt++;
}
if (cunt == sup) L.AddRange(cand [m]);
}
}
}*/
static List<ItemSet> apriorFrequent(int sup, ArrayList D)
{
Hashtable ht = find_frequent_1_item();
ArrayList L1 = new ArrayList();//一項頻繁項目集
foreach (DictionaryEntry de in ht) L1.Add(de.Key);
List<ItemSet> L = new List<ItemSet>();//存放總的頻繁項目集
int cunt;
foreach (DictionaryEntry de in ht)
{
if (Convert.ToInt32(de.Value) >= sup)
{
ItemSet iSet = new ItemSet();
iSet.Items = de.Key.ToString();
iSet.sup = Convert.ToInt32(de.Value);
L.Add(iSet);
}
}
/*Console.WriteLine("List L中存了一項頻繁項目集以後的內容如下");
foreach (ItemSet s in L)
{
Console.WriteLine(s.Items );
}*/
ArrayList iFrequent = new ArrayList();
iFrequent = L1;
/*Console.WriteLine("ArrayList iFrequent中存放一項頻繁項目集以後的內容如下");
for (int s = 0; s < iFrequent.Count; s++)
{
Console.WriteLine(iFrequent [s]);
}*/
ArrayList allK = new ArrayList();//存放資料庫的子集
for (int k = 2; iFrequent.Count != 0; k++)
{
ArrayList cand = aprior_gen(iFrequent);//候選集
Console.WriteLine("cand中存放候選項集如下");
for (int ca = 0; ca < cand.Count; ca++)
{
Console.WriteLine(cand [ca]);
}
//Console.ReadLine();
allK.Clear();
for (int i = 0; i < D.Count; i++)
{
string stemp = D[i].ToString();
allK.AddRange (SubSet(k, stemp ));
}
Console.WriteLine("allK中存放的資料庫的所有K項集如下");
for (int al = 0; al < allK.Count; al++)
{
Console.WriteLine(allK[al]);
}
//Console.ReadLine();
iFrequent.Clear();
for (int m = 0; m < cand.Count; m++)
{
cunt = 0;
for (int n = 0; n < allK.Count; n++)
{
string str1;
str1 = Convert .ToString (cand [m]);
string str2;
str2 = Convert.ToString(allK [n]);
int vv = 0;
for (int p = 0; p < str1.Length;p++ )
{
for(int q=0;q<str2 .Length ;q++)
if(str1 [p]==str2 [q])
{
vv++ ; break ;
}
}
if(vv==str1.Length )cunt++;
}
if (cunt >= sup)
{
if(!iFrequent .Contains (cand[m]))
{
iFrequent.Add(cand[m]);
ItemSet iSet = new ItemSet();
iSet.Items = cand[m].ToString();
iSet.sup = cunt;
L.Add(iSet);
}
}
}
} return L;
}
}
}
㈡ python哪個包實現apriori
要用apriori不需要哪個包,要有一個實現apriori功能的.py文件,將這個文件放置在你要調用的文件相同的地址,然後用fromaprioriimport*來使用。。
apriori.py下載地址:鏈接:https://pan..com/s/1XpKHfUXGDIXj7CcYJL0anA
㈢ 急求用C實現的Apriori演算法的 代碼
http://www.csc.liv.ac.uk/~frans/Notes/KDD/AssocRuleMine/apriori.html
.h
====================================
/*----------------------------------------------------------------------
File : apriori.h
Contents: apriori algorithm for finding frequent item sets
(specialized version for FIMI 2003 workshop)
Author : Christian Borgelt
History : 15.08.2003 file created from normal apriori.c
16.08.2003 parameter for transaction filtering added
18.08.2003 dynamic filtering decision based on times added
21.08.2003 transaction sort changed to heapsort
20.09.2003 output file made optional
----------------------------------------------------------------------*/
/*
Modified by : Frédéric Flouvat
Modifications : store the positive and negative border into an
an input trie for ABS
process stastical informations on dataset to stop
the apriori classical iterations
Author : Frédéric Flouvat
----------------------------------------------------------------------*/
#ifndef APRIRORI_H
#define APRIRORI_H
#include <iostream>
using namespace std;
#define MAXIMAL
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <time.h>
#include <assert.h>
#include "tract.h"
#include "istree.h"
#include "Application.h"
/*----------------------------------------------------------------------
Preprocessor Definitions
----------------------------------------------------------------------*/
#define PRGNAME "fim/apriori"
#define DESCRIPTION "frequent item sets miner for FIMI 2003"
#define VERSION "version 1.7 (2003.12.02) " \
"(c) 2003 Christian Borgelt"
/* --- error codes --- */
#define E_OPTION (-5) /* unknown option */
#define E_OPTARG (-6) /* missing option argument */
#define E_ARGCNT (-7) /* too few/many arguments */
#define E_SUPP (-8) /* invalid minimum support */
#define E_NOTAS (-9) /* no items or transactions */
#define E_UNKNOWN (-18) /* unknown error */
#ifndef QUIET /* if not quiet version */
#define MSG(x) x /* print messages */
#else /* if quiet version */
#define MSG(x) /* suppress messages */
#endif
#define SEC_SINCE(t) ((clock()-(t)) /(double)CLOCKS_PER_SEC)
#define RECCNT(s) (tfs_reccnt(is_tfscan(s)) \
+ ((tfs_delim(is_tfscan(s)) == TFS_REC) ? 0 : 1))
#define BUFFER(s) tfs_buf(is_tfscan(s))
/*----------------------------------------------------------------------
Constants
----------------------------------------------------------------------*/
#ifndef QUIET /* if not quiet version */
/* --- error messages --- */
static const char *errmsgs[] = {
/* E_NONE 0 */ "no error\n",
/* E_NOMEM -1 */ "not enough memory\n",
/* E_FOPEN -2 */ "cannot open file %s\n",
/* E_FREAD -3 */ "read error on file %s\n",
/* E_FWRITE -4 */ "write error on file %s\n",
/* E_OPTION -5 */ "unknown option -%c\n",
/* E_OPTARG -6 */ "missing option argument\n",
/* E_ARGCNT -7 */ "wrong number of arguments\n",
/* E_SUPP -8 */ "invalid minimal support %d\n",
/* E_NOTAS -9 */ "no items or transactions to work on\n",
/* -10 to -15 */ NULL, NULL, NULL, NULL, NULL, NULL,
/* E_ITEMEXP -16 */ "file %s, record %d: item expected\n",
/* E_DUPITEM -17 */ "file %s, record %d: plicate item %s\n",
/* E_UNKNOWN -18 */ "unknown error\n"
};
#endif
/*----------------------------------------------------------------------
Global Variables
----------------------------------------------------------------------*/
#ifndef QUIET
static char *prgname; /* program name for error messages */
#endif
static ITEMSET *itemset = NULL; /* item set */
static TASET *taset = NULL; /* transaction set */
static TATREE *tatree = NULL; /* transaction tree */
static ISTREE *istree = NULL; /* item set tree */
static FILE *in = NULL; /* input file */
static FILE *out = NULL; /* output file */
extern "C" TATREE * apriori( char*fn_in, char*fn_out, int supp, int & level,
Trie * bdPapriori, Trie * bdn, set<Element> * relist, double ratioNfC, double & eps, int ismax,
vector< unsigned int > * stat, int & maxBdP, bool & generatedFk, bool verbose ) ;
#endif
.c
============================================
/*----------------------------------------------------------------------
File : apriori.c
Contents: apriori algorithm for finding frequent item sets
(specialized version for FIMI 2003 workshop)
Author : Christian Borgelt
History : 15.08.2003 file created from normal apriori.c
16.08.2003 parameter for transaction filtering added
18.08.2003 dynamic filtering decision based on times added
21.08.2003 transaction sort changed to heapsort
20.09.2003 output file made optional
----------------------------------------------------------------------*/
/*
Modified by : Frédéric Flouvat
Modifications : store the positive and negative border into an
an input trie for ABS
process stastical informations on dataset to stop
the apriori classical iterations
Author : Frédéric Flouvat
----------------------------------------------------------------------*/
#include "apriori.h"
/*----------------------------------------------------------------------
Main Functions
----------------------------------------------------------------------*/
static void error (int code, ...)
{ /* --- print an error message */
#ifndef QUIET /* if not quiet version */
va_list args; /* list of variable arguments */
const char *msg; /* error message */
assert(prgname); /* check the program name */
if (code < E_UNKNOWN) code = E_UNKNOWN;
if (code < 0) { /* if to report an error, */
msg = errmsgs[-code]; /* get the error message */
if (!msg) msg = errmsgs[-E_UNKNOWN];
fprintf(stderr, "\n%s: ", prgname);
va_start(args, code); /* get variable arguments */
vfprintf(stderr, msg, args);/* print error message */
va_end(args); /* end argument evaluation */
}
#endif
#ifndef NDEBUG /* if debug version */
if (istree) ist_delete(istree);
if (tatree) tat_delete(tatree);
if (taset) tas_delete(taset, 0);
if (itemset) is_delete(itemset);
if (in) fclose(in); /* clean up memory */
if (out) fclose(out); /* and close files */
#endif
exit(code); /* abort the program */
} /* error() */
/*--------------------------------------------------------------------*/
TATREE * apriori( char*fn_in, char*fn_out, int supp, int & level, Trie * bdPapriori,
Trie * bdn , set<Element> * relist , double ratioNfC, double & eps,int ismax,
vector< unsigned int > * stat, int & maxBdP, bool & generatedFk, bool verbose )
{
int i, k, n; /* loop variables, counters */
int tacnt = 0; /* number of transactions */
int max = 0; /* maximum transaction size */
int empty = 1; /* number of empty item sets */
int *map, *set; /* identifier map, item set */
char *usage; /* flag vector for item usage */
clock_t t, tt, tc, x; /* timer for measurements */
double actNfC = 1 ;
double avgNfC = 0 ;
int nbgen = 0 ;
int nbfreq = 0 ;
level = 1 ;
bool endApriori = false ; // boolean to stop the initial classial apriori approach
int bdnsize = 0 ; // number of itemsets found infrequent
/* --- create item set and transaction set --- */
itemset = is_create(); /* create an item set and */
if (!itemset) error(E_NOMEM); /* set the special characters */
taset = tas_create(itemset); /* create a transaction set */
if (!taset) error(E_NOMEM); /* to store the transactions */
if( verbose ) MSG(fprintf(stderr, "\n")); /* terminate the startup message */
/* --- read transactions --- */
if( verbose )MSG(fprintf(stderr, "reading %s ... ", fn_in));
t = clock(); /* start the timer and */
in = fopen(fn_in, "r"); /* open the input file */
if (!in) error(E_FOPEN, fn_in);
for (tacnt = 0; 1; tacnt++) { /* transaction read loop */
k = is_read(itemset, in); /* read the next transaction */
if (k < 0) error(k, fn_in, RECCNT(itemset), BUFFER(itemset));
if (k > 0) break; /* check for error and end of file */
k = is_tsize(itemset); /* update the maximal */
if (k > max) max = k; /* transaction size */
if (taset && (tas_add(taset, NULL, 0) != 0))
error(E_NOMEM); /* add the loaded transaction */
} /* to the transaction set */
fclose(in); in = NULL; /* close the input file */
n = is_cnt(itemset); /* get the number of items */
if( verbose ) MSG(fprintf(stderr, "[%d item(s),", n));
if( verbose ) MSG(fprintf(stderr, " %d transaction(s)] done ", tacnt));
if( verbose ) MSG(fprintf(stderr, "[%.2fs].\n", SEC_SINCE(t)));
/* --- sort and recode items --- */
if( verbose ) MSG(fprintf(stderr, "sorting and recoding items ... "));
t = clock(); /* start the timer */
map = (int*)malloc(is_cnt(itemset) *sizeof(int));
if (!map) error(E_NOMEM); /* create an item identifier map */
n = is_recode(itemset, supp, 2, map); /* 2: sorting mode */
tas_recode(taset, map, n); /* recode the loaded transactions */
max = tas_max(taset); /* get the new maximal t.a. size */
// use in the other part of the implementation to have the corresponding
// identifiant to an internal id
stat->reserve( n+2 ) ;
stat->push_back( 0 ) ;
for(int j= 0; j< n ; j++ )
{
stat->push_back( 0 ) ;
relist->insert( Element( atoi( is_name( itemset, j ) ) ,j) );
}
if( verbose ) MSG(fprintf(stderr, "[%d item(s)] ", n));
if( verbose ) MSG(fprintf(stderr, "done [%.2fs].\n", SEC_SINCE(t)));
/* --- create a transaction tree --- */
if( verbose ) MSG(fprintf(stderr, "creating transaction tree ... "));
t = clock(); /* start the timer */
tatree = tat_create(taset,1); /* create a transaction tree */
if (!tatree) error(E_NOMEM); /* (compactify transactions) */
tt = clock() -t; /* note the construction time */
if( verbose ) MSG(fprintf(stderr, "done [%.2fs].\n", SEC_SINCE(t)));
/* --- create an item set tree --- */
if( verbose ) MSG(fprintf(stderr, "checking subsets of size 1"));
t = clock(); tc = 0; /* start the timer and */
istree = ist_create(n, supp); /* create an item set tree */
if (!istree) error(E_NOMEM);
for (k = n; --k >= 0; ) /* set single item frequencies */
ist_setcnt(istree, k, is_getfrq(itemset, k));
ist_settac(istree, tacnt); /* set the number of transactions */
usage = (char*)malloc(n *sizeof(char));
if (!usage) error(E_NOMEM); /* create a item usage vector */
/* --- check item subsets --- */
while (ist_height(istree) < max && ( ( ismax == -1 && endApriori == false )
|| ist_height(istree) < ismax )
)
{
nbgen = 0 ;
nbfreq = 0 ;
level ++ ;
i = ist_check(istree,usage);/* check current item usage */
if (i < max) max = i; /* update the maximum set size */
if (ist_height(istree) >= i) break;
k = ist_addlvl(istree, nbgen); /* while max. height is not reached, */
if (k < 0) error(E_NOMEM); /* add a level to the item set tree */
if (k != 0) break; /* if no level was added, abort */
if( verbose ) MSG(fprintf(stderr, " %d", ist_height(istree)));
if ((i < n) /* check item usage on current level */
&& (i *(double)tt < 0.1 *n *tc)) {
n = i; x = clock(); /* if items were removed and */
tas_filter(taset, usage); /* the counting time is long enough, */
tat_delete(tatree); /* remove unnecessary items */
tatree = tat_create(taset, 1);
if (!tatree) error(E_NOMEM);
tt = clock() -x; /* rebuild the transaction tree and */
} /* note the new construction time */
x = clock(); /* start the timer */
ist_countx(istree, tatree, nbfreq, istree->supp ); /* count the transaction tree */
tc = clock() -x; /* in the item set tree */
actNfC = 1-double(nbfreq)/double(nbgen) ;
avgNfC = avgNfC + actNfC ;
if( verbose )
{
cout<<" \t Fk : "<<nbfreq<<" Ck : "<<nbgen<<" NFk/Ck "<<actNfC<<" avg NFk/Ck "<<avgNfC/(level-1)<<endl;
}
bdnsize += nbgen - nbfreq ;
if( level >=4 && ( bdnsize / nbgen < 1.5 ) && ( bdnsize > 100 ) )
{
if( actNfC < ratioNfC )
{
eps = 0 ;
endApriori = true ;
}
else if( actNfC > 0.25 )
endApriori = true ;
}
} /* and note the new counting time */
if( verbose ) MSG(fprintf(stderr, " done [%.2fs].\n", SEC_SINCE(t)));
/* --- filter item sets --- */
t = clock(); /* start the timer */
#ifdef MAXIMAL /* filter maximal item sets */
if( verbose ) MSG(fprintf(stderr, "filtering maximal item sets ... "));
if( ratioNfC == 0 || nbgen < k+1 || ist_height(istree)>= max )
ist_filter2(istree, IST_MAXFRQ, 0);
else
ist_filter2(istree, IST_MAXFRQ, bdn);
if( verbose ) MSG(fprintf(stderr, " done [%.2fs].\n", SEC_SINCE(t)));
empty = (n <= 0) ? 1 : 0; /* check whether the empty item set */
#endif /* is maximal */
#ifdef CLOSED /* filter closed item sets */
if( verbose ) MSG(fprintf(stderr, "filtering closed item sets ... "));
ist_filter(istree, IST_CLOSED);
if( verbose ) MSG(fprintf(stderr, " done [%.2fs].\n", SEC_SINCE(t)));
for (k = n; --k >= 0; ) /* check for an item in all t.a. */
if (is_getfrq(itemset, k) == tacnt) break;
empty = (k <= 0) ? 1 : 0; /* check whether the empty item set */
#endif /* is closed */
/* --- print item sets --- */
for (i = ist_height(istree); --i >= 0; )
map[i] = 0; /* clear the item set counters */
if( verbose ) MSG(fprintf(stderr, "writing %s ... ", (fn_out) ? fn_out : "<none>"));
t = clock(); /* start the timer and */
if (fn_out) { /* if an output file is given, */
out = fopen(fn_out, "w"); /* open the output file */
if (!out) error(E_FOPEN, fn_out);
if (empty) fprintf(out, " (%d)\n", tacnt);
} /* report empty item set */
ist_init(istree); /* init. the item set extraction */
set = is_tract(itemset); /* get the transaction buffer */
for (n = empty; 1; n++) { /* extract item sets from the tree */
k = ist_set(istree, set, &supp);
if (k <= 0) break; /* get the next frequent item set */
map[k-1]++; /* count the item set */
if (fn_out) { /* if an output file is given */
for (i = 0; i < k; i++) { /* traverse the items */
fputs(is_name(itemset, set[i]), out);
fputc(' ', out); /* print the name of the next item */
} /* followed by a separator */
fprintf(out, "(%d)\n", supp);
} /* print the item set's support */
else
{
short unsigned * is = new short unsigned[k] ;
for (i = 0; i < k; i++) /* traverse the items */
{
is[i] = set[i] ;
}
if( k < level || nbgen < k+1 || ist_height(istree)>= max )
{
bdPapriori->insert(is, k ,supp ) ;
(*stat)[ 0 ] ++;
(*stat)[ k+1 ]++;
if( maxBdP < k )
maxBdP = k ;
}
else
{
generatedFk = true ;
}
delete[] is;
}
}
if (fn_out) { /* if an output file is given */
if (fflush(out) != 0) error(E_FWRITE, fn_out);
if (out != stdout) fclose(out);
out = NULL; /* close the output file */
}
if( verbose ) MSG(fprintf(stderr, "[%d set(s)] done ", n));
if( verbose ) MSG(fprintf(stderr, "[%.2fs].\n", SEC_SINCE(t)));
/* --- print item set statistics --- */
k = ist_height(istree); /* find last nonzero counter */
if ((k > 0) && (map[k-1] <= 0)) k--;
if( verbose ){
printf("%d\n", empty); /* print the numbers of item sets */
for (i = 0; i < k; i++) printf("%d\n", map[i]);
}
/* --- clean up --- */
#ifndef NDEBUG /* if this is a debug version */
free(usage); /* delete the item usage vector */
free(map); /* and the identifier map */
ist_delete(istree); /* delete the item set tree, */
if (taset) tas_delete(taset, 0); /* the transaction set, */
is_delete(itemset); /* and the item set */
#endif
return tatree ;
}
㈣ 你好 我也是做畢設 用java實現APRIORI演算法 能把代碼發我看看嗎
package com.apriori;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
public class Apriori {
private final static int SUPPORT = 2; // 支持度閾值
private final static double CONFIDENCE = 0.7; // 置信度閾值
private final static String ITEM_SPLIT=";"; // 項之間的分隔符
private final static String CON="->"; // 項之間的分隔符
private final static List<String> transList=new ArrayList<String>(); //所有交易
static{//初始化交易記錄
transList.add("1;2;5;");
transList.add("2;4;");
transList.add("2;3;");
transList.add("1;2;4;");
transList.add("1;3;");
transList.add("2;3;");
transList.add("1;3;");
transList.add("1;2;3;5;");
transList.add("1;2;3;");
}
public Map<String,Integer> getFC(){
Map<String,Integer> frequentCollectionMap=new HashMap<String,Integer>();//所有的頻繁集
frequentCollectionMap.putAll(getItem1FC());
Map<String,Integer> itemkFcMap=new HashMap<String,Integer>();
itemkFcMap.putAll(getItem1FC());
while(itemkFcMap!=null&&itemkFcMap.size()!=0){
Map<String,Integer> candidateCollection=getCandidateCollection(itemkFcMap);
Set<String> ccKeySet=candidateCollection.keySet();
//對候選集項進行累加計數
for(String trans:transList){
for(String candidate:ccKeySet){
boolean flag=true;// 用來判斷交易中是否出現該候選項,如果出現,計數加1
String[] candidateItems=candidate.split(ITEM_SPLIT);
for(String candidateItem:candidateItems){
if(trans.indexOf(candidateItem+ITEM_SPLIT)==-1){
flag=false;
break;
}
}
if(flag){
Integer count=candidateCollection.get(candidate);
candidateCollection.put(candidate, count+1);
}
}
}
//從候選集中找到符合支持度的頻繁集項
itemkFcMap.clear();
for(String candidate:ccKeySet){
Integer count=candidateCollection.get(candidate);
if(count>=SUPPORT){
itemkFcMap.put(candidate, count);
}
}
//合並所有頻繁集
frequentCollectionMap.putAll(itemkFcMap);
}
return frequentCollectionMap;
}
private Map<String,Integer> getCandidateCollection(Map<String,Integer> itemkFcMap){
Map<String,Integer> candidateCollection=new HashMap<String,Integer>();
Set<String> itemkSet1=itemkFcMap.keySet();
Set<String> itemkSet2=itemkFcMap.keySet();
for(String itemk1:itemkSet1){
for(String itemk2:itemkSet2){
//進行連接
String[] tmp1=itemk1.split(ITEM_SPLIT);
String[] tmp2=itemk2.split(ITEM_SPLIT);
String c="";
if(tmp1.length==1){
if(tmp1[0].compareTo(tmp2[0])<0){
c=tmp1[0]+ITEM_SPLIT+tmp2[0]+ITEM_SPLIT;
}
}else{
boolean flag=true;
for(int i=0;i<tmp1.length-1;i++){
if(!tmp1[i].equals(tmp2[i])){
flag=false;
break;
}
}
if(flag&&(tmp1[tmp1.length-1].compareTo(tmp2[tmp2.length-1])<0)){
c=itemk1+tmp2[tmp2.length-1]+ITEM_SPLIT;
}
}
//進行剪枝
boolean hasInfrequentSubSet = false;
if (!c.equals("")) {
String[] tmpC = c.split(ITEM_SPLIT);
for (int i = 0; i < tmpC.length; i++) {
String subC = "";
for (int j = 0; j < tmpC.length; j++) {
if (i != j) {
subC = subC+tmpC[j]+ITEM_SPLIT;
}
}
if (itemkFcMap.get(subC) == null) {
hasInfrequentSubSet = true;
break;
}
}
}else{
hasInfrequentSubSet=true;
}
if(!hasInfrequentSubSet){
candidateCollection.put(c, 0);
}
}
}
return candidateCollection;
}
private Map<String,Integer> getItem1FC(){
Map<String,Integer> sItem1FcMap=new HashMap<String,Integer>();
Map<String,Integer> rItem1FcMap=new HashMap<String,Integer>();//頻繁1項集
for(String trans:transList){
String[] items=trans.split(ITEM_SPLIT);
for(String item:items){
Integer count=sItem1FcMap.get(item+ITEM_SPLIT);
if(count==null){
sItem1FcMap.put(item+ITEM_SPLIT, 1);
}else{
sItem1FcMap.put(item+ITEM_SPLIT, count+1);
}
}
}
Set<String> keySet=sItem1FcMap.keySet();
for(String key:keySet){
Integer count=sItem1FcMap.get(key);
if(count>=SUPPORT){
rItem1FcMap.put(key, count);
}
}
return rItem1FcMap;
}
public Map<String,Double> getRelationRules(Map<String,Integer> frequentCollectionMap){
Map<String,Double> relationRules=new HashMap<String,Double>();
Set<String> keySet=frequentCollectionMap.keySet();
for (String key : keySet) {
double countAll=frequentCollectionMap.get(key);
String[] keyItems = key.split(ITEM_SPLIT);
if(keyItems.length>1){
List<String> source=new ArrayList<String>();
Collections.addAll(source, keyItems);
List<List<String>> result=new ArrayList<List<String>>();
buildSubSet(source,result);//獲得source的所有非空子集
for(List<String> itemList:result){
if(itemList.size()<source.size()){//只處理真子集
List<String> otherList=new ArrayList<String>();
for(String sourceItem:source){
if(!itemList.contains(sourceItem)){
otherList.add(sourceItem);
}
}
String reasonStr="";//前置
String resultStr="";//結果
for(String item:itemList){
reasonStr=reasonStr+item+ITEM_SPLIT;
}
for(String item:otherList){
resultStr=resultStr+item+ITEM_SPLIT;
}
double countReason=frequentCollectionMap.get(reasonStr);
double itemConfidence=countAll/countReason;//計算置信度
if(itemConfidence>=CONFIDENCE){
String rule=reasonStr+CON+resultStr;
relationRules.put(rule, itemConfidence);
}
}
}
}
}
return relationRules;
}
private void buildSubSet(List<String> sourceSet, List<List<String>> result) {
// 僅有一個元素時,遞歸終止。此時非空子集僅為其自身,所以直接添加到result中
if (sourceSet.size() == 1) {
List<String> set = new ArrayList<String>();
set.add(sourceSet.get(0));
result.add(set);
} else if (sourceSet.size() > 1) {
// 當有n個元素時,遞歸求出前n-1個子集,在於result中
資料來自於http://blog.csdn.net/zjd950131/article/details/8071414 沒有摘抄完
㈤ 急求用C實現的Apriori演算法的 代碼 要求一定可執行!!!!!!!!!!
好好搜搜,網上應該可以下載到吧?
如果找不到,我知道有個java的軟體weka,
裡面應該有 Apriori 演算法的,你把它轉換為 C 語言的就行了。
㈥ [Apriori.zip] - Apriori演算法源碼,在vc++6.0環境下運行,編譯,有操作界面
我想weka應該很適合你吧^^
用來跑一跑自己的演算法或者直接用它的api做二次開發都是很方便的,比如你提到的~只是原始演算法和自己演算法的對比一下是不難實現的,在自己的代碼里分別初始化兩個演算法對象模型,一起training一起testing,最後把得出的結果放一起就行了。至於圖形界面怎麼組織就按自己的需要做就好啦。
如果不想寫代碼的話就用weka自己的圖形界面weka explorer或者work flow跑幾遍也行,因為weka自己的圖形化表示已經很多樣很直觀啦^^
推薦一本書的話就是這個啦:
Data Mining: Practical Machine Learning Tools and Techniques (Second Edition) 作者是Ian Witten
就是weka的配套教材啦,例子很豐富,由淺入深的,很好上手的。
有進一步的問題就去weka list里找答案吧,很棒的討論組,起碼對我幫助很大(連接在參考資料里)。
希望對你有幫助^^
㈦ 求個實現關聯規則挖掘的Apriori演算法的代碼
Apriori演算法改進與實現研究
文章摘要:傳統的關聯規則挖掘的Apriori演算法的時間開銷要大,建議挖掘ORAR的基於關系代數理論的關聯規則演算法,不僅要掃描一次資料庫使用大量的模擬實驗證明改善關系matrix.Through ORAR演算法是非常有效的,根據收集運行時間集中頻繁項減少大量開挖。
關鍵詞:Apriori演算法; ORAR;改進;關系矩陣的關聯規則
㈧ 用JAVA實現的Apriori演算法
http://www.pudn.com/downloads13/website/detail52797.html?name=apriori演算法的java代碼.rar
這個地方能下載!
㈨ 急需C++實現的Apriori演算法代碼
用C++ 實現的 可以 到http://download.csdn.net/down/188143/chanjuanzz下載 不過要注冊扣積分的
演算法實現
(一)核心類
Apriori演算法的核心實現類為AprioriAlgorithm,實現的Java代碼如下所示:
package org.shirdrn.datamining.association;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
/**
* <B>關聯規則挖掘:Apriori演算法</B>
*
* <P>該演算法基本上按照Apriori演算法的基本思想來實現的。
*
* @author shirdrn
* @date 2009/07/22 22:56:23
* @msn shirdrn#hotmail.com(#→@)
* @qq 187071722
*/
public class AprioriAlgorithm {
private Map<Integer, Set<String>> txDatabase; // 事務資料庫
private Float minSup; // 最小支持度
private Float minConf; // 最小置信度
private Integer txDatabaseCount; // 事務資料庫中的事務數
private Map<Integer, Set<Set<String>>> freqItemSet; // 頻繁項集集合
private Map<Set<String>, Set<Set<String>>> assiciationRules; // 頻繁關聯規則集合
public AprioriAlgorithm(
Map<Integer, Set<String>> txDatabase,
Float minSup,
Float minConf) {
this.txDatabase = txDatabase;
this.minSup = minSup;
this.minConf = minConf;
this.txDatabaseCount = this.txDatabase.size();
freqItemSet = new TreeMap<Integer, Set<Set<String>>>();
assiciationRules = new HashMap<Set<String>, Set<Set<String>>>();
}
/**
* 掃描事務資料庫,計算頻繁1-項集
* @return
*/
public Map<Set<String>, Float> getFreq1ItemSet() {
Map<Set<String>, Float> freq1ItemSetMap = new HashMap<Set<String>, Float>();
Map<Set<String>, Integer> candFreq1ItemSet = this.getCandFreq1ItemSet();
Iterator<Map.Entry<Set<String>, Integer>> it = candFreq1ItemSet.entrySet().iterator();
while(it.hasNext()) {
Map.Entry<Set<String>, Integer> entry = it.next();
// 計算支持度
Float supported = new Float(entry.getValue().toString())/new Float(txDatabaseCount);
if(supported>=minSup) {
freq1ItemSetMap.put(entry.getKey(), supported);
}
}
return freq1ItemSetMap;
}
/**
* 計算候選頻繁1-項集
* @return
*/
public Map<Set<String>, Integer> getCandFreq1ItemSet() {
Map<Set<String>, Integer> candFreq1ItemSetMap = new HashMap<Set<String>, Integer>();
Iterator<Map.Entry<Integer, Set<String>>> it = txDatabase.entrySet().iterator();
// 統計支持數,生成候選頻繁1-項集
while(it.hasNext()) {
Map.Entry<Integer, Set<String>> entry = it.next();
Set<String> itemSet = entry.getValue();
for(String item : itemSet) {
Set<String> key = new HashSet<String>();
key.add(item.trim());
if(!candFreq1ItemSetMap.containsKey(key)) {
Integer value = 1;
candFreq1ItemSetMap.put(key, value);
}
else {
Integer value = 1+candFreq1ItemSetMap.get(key);
candFreq1ItemSetMap.put(key, value);
}
}
}
return candFreq1ItemSetMap;
}
/**
* 根據頻繁(k-1)-項集計算候選頻繁k-項集
*
* @param m 其中m=k-1
* @param freqMItemSet 頻繁(k-1)-項集
* @return
*/
public Set<Set<String>> aprioriGen(int m, Set<Set<String>> freqMItemSet) {
Set<Set<String>> candFreqKItemSet = new HashSet<Set<String>>();
Iterator<Set<String>> it = freqMItemSet.iterator();
Set<String> originalItemSet = null;
while(it.hasNext()) {
originalItemSet = it.next();
Iterator<Set<String>> itr = this.getIterator(originalItemSet, freqMItemSet);
while(itr.hasNext()) {
Set<String> identicalSet = new HashSet<String>(); // 兩個項集相同元素的集合(集合的交運算)
identicalSet.addAll(originalItemSet);
Set<String> set = itr.next();
identicalSet.retainAll(set); // identicalSet中剩下的元素是identicalSet與set集合中公有的元素
if(identicalSet.size() == m-1) { // (k-1)-項集中k-2個相同
Set<String> differentSet = new HashSet<String>(); // 兩個項集不同元素的集合(集合的差運算)
differentSet.addAll(originalItemSet);
differentSet.removeAll(set); // 因為有k-2個相同,則differentSet中一定剩下一個元素,即differentSet大小為1
differentSet.addAll(set); // 構造候選k-項集的一個元素(set大小為k-1,differentSet大小為k)
candFreqKItemSet.add(differentSet); // 加入候選k-項集集合
}
}
}
return candFreqKItemSet;
}
/**
* 根據一個頻繁k-項集的元素(集合),獲取到頻繁k-項集的從該元素開始的迭代器實例
* @param itemSet
* @param freqKItemSet 頻繁k-項集
* @return
*/
private Iterator<Set<String>> getIterator(Set<String> itemSet, Set<Set<String>> freqKItemSet) {
Iterator<Set<String>> it = freqKItemSet.iterator();
while(it.hasNext()) {
if(itemSet.equals(it.next())) {
break;
}
}
return it;
}
/**
* 根據頻繁(k-1)-項集,調用aprioriGen方法,計算頻繁k-項集
*
* @param k
* @param freqMItemSet 頻繁(k-1)-項集
* @return
*/
public Map<Set<String>, Float> getFreqKItemSet(int k, Set<Set<String>> freqMItemSet) {
Map<Set<String>, Integer> candFreqKItemSetMap = new HashMap<Set<String>, Integer>();
// 調用aprioriGen方法,得到候選頻繁k-項集
Set<Set<String>> candFreqKItemSet = this.aprioriGen(k-1, freqMItemSet);
// 掃描事務資料庫
Iterator<Map.Entry<Integer, Set<String>>> it = txDatabase.entrySet().iterator();
// 統計支持數
while(it.hasNext()) {
Map.Entry<Integer, Set<String>> entry = it.next();
Iterator<Set<String>> kit = candFreqKItemSet.iterator();
while(kit.hasNext()) {
Set<String> kSet = kit.next();
Set<String> set = new HashSet<String>();
set.addAll(kSet);
set.removeAll(entry.getValue()); // 候選頻繁k-項集與事務資料庫中元素做差元算
if(set.isEmpty()) { // 如果拷貝set為空,支持數加1
if(candFreqKItemSetMap.get(kSet) == null) {
Integer value = 1;
candFreqKItemSetMap.put(kSet, value);
}
else {
Integer value = 1+candFreqKItemSetMap.get(kSet);
candFreqKItemSetMap.put(kSet, value);
}
}
}
}
// 計算支持度,生成頻繁k-項集,並返回
return support(candFreqKItemSetMap);
}
/**
* 根據候選頻繁k-項集,得到頻繁k-項集
*
* @param candFreqKItemSetMap 候選k項集(包含支持計數)
*/
public Map<Set<String>, Float> support(Map<Set<String>, Integer> candFreqKItemSetMap) {
Map<Set<String>, Float> freqKItemSetMap = new HashMap<Set<String>, Float>();
Iterator<Map.Entry<Set<String>, Integer>> it = candFreqKItemSetMap.entrySet().iterator();
while(it.hasNext()) {
Map.Entry<Set<String>, Integer> entry = it.next();
// 計算支持度
Float supportRate = new Float(entry.getValue().toString())/new Float(txDatabaseCount);
if(supportRate<minSup) { // 如果不滿足最小支持度,刪除
it.remove();
}
else {
freqKItemSetMap.put(entry.getKey(), supportRate);
}
}
return freqKItemSetMap;
}
/**
* 挖掘全部頻繁項集
*/
public void mineFreqItemSet() {
// 計算頻繁1-項集
Set<Set<String>> freqKItemSet = this.getFreq1ItemSet().keySet();
freqItemSet.put(1, freqKItemSet);
// 計算頻繁k-項集(k>1)
int k = 2;
while(true) {
Map<Set<String>, Float> freqKItemSetMap = this.getFreqKItemSet(k, freqKItemSet);
if(!freqKItemSetMap.isEmpty()) {
this.freqItemSet.put(k, freqKItemSetMap.keySet());
freqKItemSet = freqKItemSetMap.keySet();
}
else {
break;
}
k++;
}
}
/**
* <P>挖掘頻繁關聯規則
* <P>首先挖掘出全部的頻繁項集,在此基礎上挖掘頻繁關聯規則
*/
public void mineAssociationRules() {
freqItemSet.remove(1); // 刪除頻繁1-項集
Iterator<Map.Entry<Integer, Set<Set<String>>>> it = freqItemSet.entrySet().iterator();
while(it.hasNext()) {
Map.Entry<Integer, Set<Set<String>>> entry = it.next();
for(Set<String> itemSet : entry.getValue()) {
// 對每個頻繁項集進行關聯規則的挖掘
mine(itemSet);
}
}
}
/**
* 對從頻繁項集集合freqItemSet中每迭代出一個頻繁項集元素,執行一次關聯規則的挖掘
* @param itemSet 頻繁項集集合freqItemSet中的一個頻繁項集元素
*/
public void mine(Set<String> itemSet) {
int n = itemSet.size()/2; // 根據集合的對稱性,只需要得到一半的真子集
for(int i=1; i<=n; i++) {
// 得到頻繁項集元素itemSet的作為條件的真子集集合
Set<Set<String>> properSubset = ProperSubsetCombination.getProperSubset(i, itemSet);
// 對條件的真子集集合中的每個條件項集,獲取到對應的結論項集,從而進一步挖掘頻繁關聯規則
for(Set<String> conditionSet : properSubset) {
Set<String> conclusionSet = new HashSet<String>();
conclusionSet.addAll(itemSet);
conclusionSet.removeAll(conditionSet); // 刪除條件中存在的頻繁項
confide(conditionSet, conclusionSet); // 調用計算置信度的方法,並且挖掘出頻繁關聯規則
}
}
}
/**
* 對得到的一個條件項集和對應的結論項集,計算該關聯規則的支持計數,從而根據置信度判斷是否是頻繁關聯規則
* @param conditionSet 條件頻繁項集
* @param conclusionSet 結論頻繁項集
*/
public void confide(Set<String> conditionSet, Set<String> conclusionSet) {
// 掃描事務資料庫
Iterator<Map.Entry<Integer, Set<String>>> it = txDatabase.entrySet().iterator();
// 統計關聯規則支持計數
int conditionToConclusionCnt = 0; // 關聯規則(條件項集推出結論項集)計數
int conclusionToConditionCnt = 0; // 關聯規則(結論項集推出條件項集)計數
int supCnt = 0; // 關聯規則支持計數
while(it.hasNext()) {
Map.Entry<Integer, Set<String>> entry = it.next();
Set<String> txSet = entry.getValue();
Set<String> set1 = new HashSet<String>();
Set<String> set2 = new HashSet<String>();
set1.addAll(conditionSet);
set1.removeAll(txSet); // 集合差運算:set-txSet
if(set1.isEmpty()) { // 如果set為空,說明事務資料庫中包含條件頻繁項conditionSet
// 計數
conditionToConclusionCnt++;
}
set2.addAll(conclusionSet);
set2.removeAll(txSet); // 集合差運算:set-txSet
if(set2.isEmpty()) { // 如果set為空,說明事務資料庫中包含結論頻繁項conclusionSet
// 計數
conclusionToConditionCnt++;
}
if(set1.isEmpty() && set2.isEmpty()) {
supCnt++;
}
}
// 計算置信度
Float conditionToConclusionConf = new Float(supCnt)/new Float(conditionToConclusionCnt);
if(conditionToConclusionConf>=minConf) {
if(assiciationRules.get(conditionSet) == null) { // 如果不存在以該條件頻繁項集為條件的關聯規則
Set<Set<String>> conclusionSetSet = new HashSet<Set<String>>();
conclusionSetSet.add(conclusionSet);
assiciationRules.put(conditionSet, conclusionSetSet);
}
else {
assiciationRules.get(conditionSet).add(conclusionSet);
}
}
Float conclusionToConditionConf = new Float(supCnt)/new Float(conclusionToConditionCnt);
if(conclusionToConditionConf>=minConf) {
if(assiciationRules.get(conclusionSet) == null) { // 如果不存在以該結論頻繁項集為條件的關聯規則
Set<Set<String>> conclusionSetSet = new HashSet<Set<String>>();
conclusionSetSet.add(conditionSet);
assiciationRules.put(conclusionSet, conclusionSetSet);
}
else {
assiciationRules.get(conclusionSet).add(conditionSet);
}
}
}
/**
* 經過挖掘得到的頻繁項集Map
*
* @return 挖掘得到的頻繁項集集合
*/
public Map<Integer, Set<Set<String>>> getFreqItemSet() {
return freqItemSet;
}
/**
* 獲取挖掘到的全部的頻繁關聯規則的集合
* @return 頻繁關聯規則集合
*/
public Map<Set<String>, Set<Set<String>>> getAssiciationRules() {
return assiciationRules;
}
}
(二)輔助類
ProperSubsetCombination類是一個輔助類,在挖掘頻繁關聯規則的過程中,用於生成一個頻繁項集元素的非空真子集,實現代碼如下:
package org.shirdrn.datamining.association;
import java.util.BitSet;
import java.util.HashSet;
import java.util.Set;
/**
* <B>求頻繁項集元素(集合)的非空真子集集合</B>
* <P>從一個集合(大小為n)中取出m(m屬於2~n/2的閉區間)個元素的組合實現類,獲取非空真子集的集合
*
* @author shirdrn
* @date 2009/07/22 22:56:23
* @msn shirdrn#hotmail.com(#→@)
* @qq 187071722
*/
public class ProperSubsetCombination {
private static String[] array;
private static BitSet startBitSet; // 比特集合起始狀態
private static BitSet endBitSet; // 比特集合終止狀態,用來控制循環
private static Set<Set<String>> properSubset; // 真子集集合
/**
* 計算得到一個集合的非空真子集集合
*
* @param n 真子集的大小
* @param itemSet 一個頻繁項集元素
* @return 非空真子集集合
*/
public static Set<Set<String>> getProperSubset(int n, Set<String> itemSet) {
String[] array = new String[itemSet.size()];
ProperSubsetCombination.array = itemSet.toArray(array);
properSubset = new HashSet<Set<String>>();
startBitSet = new BitSet();
endBitSet = new BitSet();
// 初始化startBitSet,左側占滿1
for (int i=0; i<n; i++) {
startBitSet.set(i, true);
}
// 初始化endBit,右側占滿1
for (int i=array.length-1; i>=array.length-n; i--) {
endBitSet.set(i, true);
}
// 根據起始startBitSet,將一個組合加入到真子集集合中
get(startBitSet);
while(!startBitSet.equals(endBitSet)) {
int zeroCount = 0; // 統計遇到10後,左邊0的個數
int oneCount = 0; // 統計遇到10後,左邊1的個數
int pos = 0; // 記錄當前遇到10的索引位置
// 遍歷startBitSet來確定10出現的位置
for (int i=0; i<array.length; i++) {
if (!startBitSet.get(i)) {
zeroCount++;
}
if (startBitSet.get(i) && !startBitSet.get(i+1)) {
pos = i;
oneCount = i - zeroCount;
// 將10變為01
startBitSet.set(i, false);
startBitSet.set(i+1, true);
break;
}
}
// 將遇到10後,左側的1全部移動到最左側
int counter = Math.min(zeroCount, oneCount);
int startIndex = 0;
int endIndex = 0;
if(pos>1 && counter>0) {
pos--;
endIndex = pos;
for (int i=0; i<counter; i++) {
startBitSet.set(startIndex, true);
startBitSet.set(endIndex, false);
startIndex = i+1;
pos--;
if(pos>0) {
endIndex = pos;
}
}
}
get(startBitSet);
}
return properSubset;
}
/**
* 根據一次移位操作得到的startBitSet,得到一個真子集
* @param bitSet
*/
private static void get(BitSet bitSet) {
Set<String> set = new HashSet<String>();
for(int i=0; i<array.length; i++) {
if(bitSet.get(i)) {
set.add(array[i]);
}
}
properSubset.add(set);
}
}
測試用例
對上述Apriori演算法的實現進行了簡單的測試,測試用例如下所示:
package org.shirdrn.datamining.association;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;
import org.shirdrn.datamining.association.AprioriAlgorithm;
import junit.framework.TestCase;
/**
* <B>Apriori演算法測試類</B>
*
* @author shirdrn
* @date 2009/07/22 22:56:23
* @msn shirdrn#hotmail.com(#→@)
* @qq 187071722
*/
public class TestAprioriAlgorithm extends TestCase {
private AprioriAlgorithm apriori;
private Map<Integer, Set<String>> txDatabase;
private Float minSup = new Float("0.50");
private Float minConf = new Float("0.70");
@Override
protected void setUp() throws Exception {
create(); // 構造事務資料庫
apriori = new AprioriAlgorithm(txDatabase, minSup, minConf);
}
/**
* 構造模擬事務資料庫txDatabase
*/
public void create() {
txDatabase = new HashMap<Integer, Set<String>>();
Set<String> set1 = new TreeSet<String>();
set1.add("A");
set1.add("B");
set1.add("C");
set1.add("E");
txDatabase.put(1, set1);
Set<String> set2 = new TreeSet<String>();
set2.add("A");
set2.add("B");
set2.add("C");
txDatabase.put(2, set2);
Set<String> set3 = new TreeSet<String>();
set3.add("C");
set3.add("D");
txDatabase.put(3, set3);
Set<String> set4 = new TreeSet<String>();
set4.add("A");
set4.add("B");
set4.add("E");
txDatabase.put(4, set4);
}
/**
* 測試挖掘頻繁1-項集
*/
public void testFreq1ItemSet() {
System.out.println("挖掘頻繁1-項集 : " + apriori.getFreq1ItemSet());
}
/**
* 測試aprioriGen方法,生成候選頻繁項集
*/
public void testAprioriGen() {
System.out.println(
"候選頻繁2-項集 : " +
this.apriori.aprioriGen(1, this.apriori.getFreq1ItemSet().keySet())
);
}
/**
* 測試挖掘頻繁2-項集
*/
public void testGetFreq2ItemSet() {
System.out.println(
"挖掘頻繁2-項集 :" +
this.apriori.getFreqKItemSet(2, this.apriori.getFreq1ItemSet().keySet())
);
}
/**
* 測試挖掘頻繁3-項集
*/
public void testGetFreq3ItemSet() {
System.out.println(
"挖掘頻繁3-項集 :" +
this.apriori.getFreqKItemSet(
3,
this.apriori.getFreqKItemSet(2, this.apriori.getFreq1ItemSet().keySet()).keySet()
)
);
}
/**
* 測試挖掘全部頻繁項集
*/
public void testGetFreqItemSet() {
this.apriori.mineFreqItemSet(); // 挖掘頻繁項集
System.out.println("挖掘頻繁項集 :" + this.apriori.getFreqItemSet());
}
/**
* 測試挖掘全部頻繁關聯規則
*/
public void testMineAssociationRules() {
this.apriori.mineFreqItemSet(); // 挖掘頻繁項集
this.apriori.mineAssociationRules();
System.out.println("挖掘頻繁關聯規則 :" + this.apriori.getAssiciationRules());
}
}
測試結果:
挖掘頻繁1-項集 : {[E]=0.5, [A]=0.75, [B]=0.75, [C]=0.75}
候選頻繁2-項集 : [[E, C], [A, B], [B, C], [A, C], [E, B], [E, A]]
挖掘頻繁2-項集 :{[A, B]=0.75, [B, C]=0.5, [A, C]=0.5, [E, B]=0.5, [E, A]=0.5}
挖掘頻繁3-項集 :{[E, A, B]=0.5, [A, B, C]=0.5}
挖掘頻繁項集 :{1=[[E], [A], [B], [C]], 2=[[A, B], [B, C], [A, C], [E, B], [E, A]], 3=[[E, A, B], [A, B, C]]}
挖掘頻繁關聯規則 :{[E]=[[A], [B], [A, B]], [A]=[[B]], [B]=[[A]], [B, C]=[[A]], [A, C]=[[B]], [E, B]=[[A]], [E, A]=[[B]]}
從測試結果看到,使用Apriori演算法挖掘得到的全部頻繁項集為:
{1=[[E], [A], [B], [C]], 2=[[A, B], [B, C], [A, C], [E, B], [E, A]], 3=[[E, A, B], [A, B, C]]}
使用Apriori演算法挖掘得到的全部頻繁關聯規則為:
{E}→{A}、{E}→{B}、{E}→{A,B}、{A}→{B}、{B}→{A}、{B,C}→{A}、{A,C}→{B}、{B,E}→{A}、{A,E}→{B}。
㈩ 請問你有用maprece實現Apriori演算法的代碼嗎
matlab實現apriori演算法源代碼 一、實驗目的 通過實驗,加深數據挖掘中一個重要方法——關聯分析的認識,其經典演算法為apriori演算法,了解影響apriori演算法性能的因素,掌握基於apriori演算法理論的關聯分析的原理和方法。 二、實驗內容 對一數據集用apriori演算法做關聯分析,用matlab實現。 三、方法手段 關聯規則挖掘的一個典型例子是購物籃分析。市場分析員要從大量的數據中發現顧客放入其購物籃中的不同商品之間的關系。如果顧客買牛奶,他也購買麵包的可能性有多大? 什麼商品組或集合顧客多半會在一次購物時同時購買?例如,買牛奶的顧客有80%也同時買麵包,或買鐵錘的顧客中有70%的人同時也買鐵釘,這就是從購物籃數據中提取的關聯規則。分析結果可以幫助經理設計不同的商店布局。一種策略是:經常一塊購買的商品可以放近一些,以便進一步刺激這些商品一起銷售,例如,如果顧客購買計算機又傾向於同時購買財務軟體,那麼將硬體擺放離軟體陳列近一點,可能有助於增加兩者的銷售。另一種策略是:將硬體和軟體放在商店的兩端,可能誘發購買這些商品的顧客一路挑選其他商品。 關聯規則是描述資料庫中數據項之間存在的潛在關系的規則,形式為 1212 ......mnAAABBB,其中(1,2...,)iAim,(1,2...,)jAjn是資料庫中的數據項.數據項之間的關聯規則即根據一個事務中某些項的出現,可推導出另一些項在同一事務中也出現。 四、Apriori演算法 1.演算法描述 Apriori演算法的第一步是簡單統計所有含一個元素的項集出現的頻率,來決定最大的一維項目集。在第k步,分兩個階段,首先用一函數sc_candidate(候選),通過第(k-1)步中生成的最大項目集Lk-1來生成侯選項目集Ck。然後搜索資料庫計算侯選項目集Ck的支持度. 為了更快速地計算Ck中項目的支持度, 文中使用函數count_support計算支持度。 Apriori演算法描述如下: (1) C1={candidate1-itemsets}; (2) L1={c∈C1|c.count≥minsupport}; (3) for(k=2,Lk-1≠Φ,k++) //直到不能再生成最大項目集為止 (4) Ck=sc_candidate(Lk-1); //生成含k個元素的侯選項目集 (5) for all transactions t∈D //辦理處理 (6) Ct=count_support(Ck,t); //包含在事務t中的侯選項目集 (7) for all candidates c∈Ct (8) c.count=c.count+1; (9) next (10) Lk={c∈Ck|c.count≥minsupport}; (11) next (12) resultset=resultset∪Lk 其中, D表示資料庫;minsupport表示給定的最小支持度;resultset表示所有最大項目集。 全國注冊建築師、建造師考試備考資料 歷年真題 考試心得 模擬試題 Sc_candidate函數 該函數的參數為Lk-1,即: 所有最大k-1維項目集,結果返回含有k個項目的侯選項目集Ck。事實上,Ck是k維最大項目集的超集,通過函數count_support計算項目的支持度,然後生成Lk。 該函數是如何完成這些功能的, 詳細說明如下: 首先, 通過對Lk-1自連接操作生成Ck,稱join(連接)步,該步可表述為: insert into Ck select P.item1,P.item2,...,P.itemk-1,Q.itemk-1 from Lk-1P,Lk-1Q where P.item1=Q.item1,...,P.itemk-2=Q.itemk-2,P.itemk-1<Q.itemk-1 若用集合表示:Ck={X∪X'|X,X'∈Lk-1,|X∩X'|=k-2} 然後,是prune(修剪)步,即對任意的c,c∈Ck, 刪除Ck中所有那些(k-1)維子集不在Lk-1 中的項目集,得到侯選項目集Ck。表述為: for all itemset c∈Ck for all (k-1)維子集s of c if(s不屬於Lk-1) then delete c from Ck; 用集合表示:Ck={X∈Ck|X的所有k-1維子集在Lk-1中} 2.Apriori演算法的舉例 示例說明Apriori演算法運作過程,有一資料庫D, 其中有四個事務記錄, 分別表示為 TID Items T1 I1,I3,I4 T2 I2,I3,I5 T3 I1,I2,I3,I5 T4 I2,I5 在Apriori演算法中每一步創建該步的侯選集。統計每個侯選項目集的支持度,並和預定 義的最小支持度比較,來確定該步的最大項目集。 首先統計出一維項目集,即C1.這里預定義最小支持度minsupport=2,侯選項目集中滿足最小支持度要求的項目集組合成最大的1-itemsets。為生成最大的2-itemsets,使用了sc_candidate函數中join步,即:L1joinL1,並通過prune步刪除那些C2的那些子集不在L1中的項目集。生成了侯選項目集C2。搜索D中4個事務,統計C2中每個侯選項目集的支持度。然後和最小支持度比較,生成L2。侯選項目集C3是由L2生成.要求自連接的兩個最大2-itemsets中,第一個項目相同,在L2中滿足該條件的有{I2,I3},{I2,I5}.這兩個集合經過join步後, 產生集合{I2,I3,I5}.在prune步中,測試{I2,I3,I5}的子集{I3,I5},{I2,I3},{I2,I5}是否在L2中,由L2可以知道{I3,I5},{I2,I3},{I2,I5}本身就是最大2-itemsets.即{I2,I3,I5}的子集都是最大項目集.那麼{I2,I3,I5}為侯選3-itemset.然後搜索資料庫中所有事務記錄,生成最大的3-tiemsets L3。此時, 從L3中不能再生成侯選4-itemset 。Apriori演算法結束. 演算法的圖例說明 五、實驗結果 test.txt格式及內容如下: 實驗結果如下: 六、實驗總結 Apriori演算法可以很有效地找出數據集中存在的關聯規則且能找出最大項的關聯規則,但從以上的演算法執行過程可以看到Apriori演算法的缺點: 第一,在每一步產生侯選項目集時循環產生的組合過多,沒有排除不應該參與組合的元素;第二,每次計算項集的支持度時,都對資料庫D中的全部記錄進行了一遍掃描比較,如果是一個大型的資料庫的話,這種掃描比較會大大增加計算機系統的I/O開銷。而這種代價是隨著資料庫的記錄的增加呈現出幾何級數的增加。因此人們開始尋求一種能減少這種系統1/O開銷的更為快捷的演算法。 七、實驗程序 function my_apriori(X,minsup) clc; %%%%主函數,輸入X數據集,判斷產生大於minsup最小支持度的關聯規則 %%%%%%%%%%%%%%%%%%%%%%%%%%打開test.txt文件 file = textread('test.txt','%s','delimiter','\n','whitespace',''); [m,n]=size(file); for i=1:m words=strread(file{i},'%s','delimiter',' '); words=words'; X{i}=words; end %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% minsup=0.3; %預先定義支持度 [m,N]=size(X); %求X的維數 temp=X{1}; %用已暫存變數存儲所有不同項集 for i=2:N temp=union(temp,X{i}); %找出所有不同項(種類) end %%%%%%%%%%%%%%%%%%%%找出k-頻繁項 L=Sc_candidate(temp); %找出2-項候選項集 sum=1; %統計滿足條件的最多項集 while(~isempty(L{1})) %循環終止條件為第k次頻繁項集為空 sum=sum+1; C=count_support(L,X,minsup); %挑選出滿足最小支持度的k-頻繁項 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% sprintf('%s%d%s','滿足要求的',sum,'次頻繁項集依次為') %顯 for i=1:size(C,1) %示 disp(C{i,1}); %部 end %分 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%% L=gen_rule(C); %依次產生k-頻繁項(依據apriori演算法規則) End %%%%%%%%%%%%%%%%%%%%%%%%各個子程序如下 function y=cell_union(X,Y) %實現兩cell元組合並功能,由k-1項集增加到k項集函數 [m,n]=size(X); if(~iscellstr(X)) %判斷X是否元組 L{1}=X; L{1,2}=Y; else L=X; L{1,n+1}=Y; end y=L; %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% function y=count_support(L,X,minsup) %找出符合大於支持度sup的候選集,L為候選集,X為總數據集 X=X';%轉置 %%%%%%%%%%%%%%%%%統計頻繁項 [m,n]=size(L); [M,N]=size(X); count=zeros(m,1); for i=1:m for j=1:M if(ismember(L{i},X{j})) count(i)=count(i)+1; end end end %%%%%%%%%%%刪除數據表中不頻繁的項 p=1; C=cell(1); for i=1:m if(count(i)>minsup*M) %小於支持度的項為不頻繁數,將刪除,大於的保留 C{p}=L{i}; p=p+1; end end y=C'; %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% function y=gen_rule(C) %apriori演算法規則判斷是否產生k-候選項集 if(~isempty(C{1})) %判斷C是否為空 [M,N]=size(C); [m,n]=size(C{1}); temp1=C; L=cell(1); for i=1:M temp2{i}=temp1{i}{n}; temp1{i}{n}=[]; end p=1; for i=1:M for j=i+1:M if(isequal(temp1{i},temp1{j})) %判斷前k-1項候選集是否相等 L{p}=cell_union(C{i},temp2{j}); %若相等,則增加至k-項集 p=p+1; end end end y=L'; else y=cell(1);%否則y返回空 end %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% function y=Sc_candidate(C) %產生2-項候選集函數 C=C'; %轉置 [m,n]=size(C); bcount=zeros(m*(m-1)/2,1); L=cell(m*(m-1)/2,1); p=1; for i=1:m-1 %注意 for j=i+1:m L{p}=cell_union(C{i},C{j}); %產生2-項候選集 p=p+1; end end y=L; function y=count_support(L,X,minsup) %找出符合大於支持度sup的候選集,L為候選集,X為總數據集