-
Notifications
You must be signed in to change notification settings - Fork 95
/
EclatAlgorithm.m
194 lines (147 loc) · 7.06 KB
/
EclatAlgorithm.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
(*
Implementation of the Eclat algorithm in Mathematica
Copyright (C) 2022 Anton Antonov
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
Written by Anton Antonov,
ʇǝu˙oǝʇsod@ǝqnɔuouoʇuɐ,
Windermere, Florida, USA.
*)
(*
Mathematica is (C) Copyright 1988-2022 Wolfram Research, Inc.
Protected by copyright law and international treaties.
Unauthorized reproduction or distribution subject to severe civil
and criminal penalties.
Mathematica is a registered trademark of Wolfram Research, Inc.
*)
(* Version 2.0 *)
(* :Title: EclatAlgorithm *)
(* :Context: EclatAlgorithm` *)
(* :Author: antonov *)
(* :Date: 2022-06-16 *)
(* :Package Version: 0.1 *)
(* :Mathematica Version: 12.1 *)
(* :Copyright: (c) 2022 antonov *)
(* :Keywords: *)
(* :Discussion: *)
If[Length[DownValues[CrossTabulate`CrossTabulate]] == 0,
Echo["CrossTabulate.m", "Importing from GitHub:"];
Import["https://raw.githubusercontent.com/antononcube/MathematicaForPrediction/master/CrossTabulate.m"]
];
If[Length[DownValues[SSparseMatrix`ToSSparseMatrix]] == 0,
Echo["SSparseMatrix.m", "Importing from GitHub:"];
Import["https://raw.githubusercontent.com/antononcube/MathematicaForPrediction/master/SSparseMatrix.m"]
];
(***********************************************************)
(* Package definition *)
(***********************************************************)
BeginPackage["EclatAlgorithm`"];
(* Exported symbols added here with SymbolName::usage *)
Eclat::usage = "Eclat[data, minSupport, opts] finds frequent sets using the Eclat algorithm.";
Begin["`Private`"];
Needs["CrossTabulate`"];
Needs["SSparseMatrix`"];
(***********************************************************)
(* EclatIntersect *)
(***********************************************************)
Clear[EclatIntersect];
EclatIntersect[aTransactions_?AssociationQ, items : {_String ..}] :=
Block[{},
If[KeyExistsQ[aTransactions, items],
aTransactions[items],
(*ELSE*)
Fold[#1*aTransactions[#2] &, aTransactions[First@items], Rest[items]]
]
];
EclatIntersect[aTransactions_?AssociationQ, items1 : {_String ..}, items2 : {_String ..}] :=
EclatIntersect[aTransactions, items1] * EclatIntersect[aTransactions, items2];
(***********************************************************)
(* EclatExtendTransactions *)
(***********************************************************)
Clear[EclatExtendTransactions];
EclatExtendTransactions[aTransactions_?AssociationQ, items : {_String ..}] :=
Append[aTransactions, items -> EclatIntersect[aTransactions, items]];
EclatExtendTransactions[aTransactions_?AssociationQ, items1 : {_String ..}, items2 : {_String ..}] :=
Append[aTransactions, Union[Join[items1, items2]] -> EclatIntersect[aTransactions, items1, items2]];
(***********************************************************)
(* EclatSupport *)
(***********************************************************)
Clear[EclatSupport];
EclatSupport[aTransactions_?AssociationQ, items : {_String ..}] :=
Total[EclatIntersect[aTransactions, items], 2];
EclatSupport[aTransactions_?AssociationQ, items1 : {_String ..}, items2 : {_String ..}] :=
Total[EclatIntersect[aTransactions, items1, items2], 2];
(***********************************************************)
(* Eclat *)
(***********************************************************)
Clear[Eclat];
Options[Eclat] = {"MaxNumberOfItems" -> Infinity, "Separator" -> "."};
aECLATTransactions = None;
Eclat[dsTransactions_Dataset, minSupport_?NumberQ, opts : OptionsPattern[]] :=
Block[{t, sep = OptionValue[Eclat, "Separator"]},
t = Normal[dsTransactions[All, Association @ KeyValueMap[#1 -> ToString[#1] <> sep <> ToString[#2] &, #] &][Values]];
Eclat[t, minSupport, opts]
]/; minSupport > 0;
Eclat[lsTransactions : {_List ..}, minSupportArg_?NumberQ, opts : OptionsPattern[]] :=
Block[{minSupport = minSupportArg, t},
If[minSupport < 1, minSupport = Floor[minSupport * Length[lsTransactions]]];
t = Join @@ MapIndexed[Thread[{#2[[1]], #1}] &, lsTransactions];
t = ToSSparseMatrix[CrossTabulate[t]];
Eclat[t, minSupport, opts]
]/; minSupportArg > 0;
Eclat[matTransactions_SSparseMatrix, minSupportArg_?NumericQ, opts : OptionsPattern[]] :=
Block[{minSupport = minSupportArg, aTransactions},
If[minSupport < 1, minSupport = Floor[minSupport * RowsCount[matTransactions]]];
aTransactions =
AssociationThread[ColumnNames[matTransactions] -> Map[Identity, Transpose[SparseArray[matTransactions]]]];
Eclat[aTransactions, minSupport, opts]
]/; minSupportArg > 0;
Eclat[aTransactions : Association[(_ -> _?SparseArrayQ) ..], minSupport_?NumericQ, opts : OptionsPattern[]] :=
Block[{P = List /@ Sort[Keys[aTransactions]], res},
P = Select[P, EclatSupport[aTransactions, #] >= minSupport &];
aECLATTransactions = aTransactions;
res = EclatRec[aTransactions, P, minSupport, {}, 0, opts];
AssociationThread[res, EclatSupport[aECLATTransactions, #] & /@ res]
]/; minSupport > 0;
(*---------------------------------------------------------*)
Clear[EclatRec];
Options[EclatRec] = Options[Eclat];
EclatRec[aTransactions_?AssociationQ, P_List, minSupport_?NumericQ, Farg_List, k_Integer, opts : OptionsPattern[]] :=
Block[{F = Farg, maxNumberOfItems, P2 = {}, Xab, tXab, PRINT},
maxNumberOfItems = OptionValue[EclatRec, "MaxNumberOfItems"];
PRINT[Style[Row[{"rec : ", k}], Purple, Bold]];
PRINT["P: ", P];
Do[
AppendTo[F, Xa];
PRINT["F: ", F];
P2 = {};
Do[
Xab = Union[Xa, Xb];
PRINT["{Xa,Xb} : ", {Xa, Xb}];
If[Length[Xab] <= maxNumberOfItems,
aECLATTransactions = EclatExtendTransactions[aECLATTransactions, Xa, Complement[Xab, Xa]];
tXab = EclatSupport[aECLATTransactions, Xab];
(*tXab=EclatSupport[aECLATTransactions,Xab];*)
PRINT["Xab->tXab : ", Xab -> tXab];
If[tXab >= minSupport,
AppendTo[P2, Xab];
]
];
PRINT["P2: ", P2]
, {Xb, Select[P, Order[Xa, #] > 0 &]}];
If[Length[P2] > 0,
F = EclatRec[aTransactions, P2, minSupport, F, k + 1, opts]
]
, {Xa, Select[P, Length[#] <= maxNumberOfItems &]}];
F
];
End[]; (* `Private` *)
EndPackage[]