I am doing this to learn association rules. The data d is in format {id, value}, for example:
{{0, 29}, {1, 30}, {1, 31}, {1, 32}, {2, 33}, {2, 34}, {2, 35}, {3, 36}, {3, 37}, {3, 38},
{3, 39}, {3, 40}, {3, 41}, {3, 42}, {3, 43}, {3, 44}, {3, 45}, {3, 46}, {4, 38}, {4, 39},
{4, 47}, {4, 48}, {5, 38}, {5, 39}, {5, 48}}
Bigger data example (task I am using it for has x100 amount data)
I want to find number of id's that contain values {x, y} and P(x|y). Something like:
Contains[d_, x_] := Take[Transpose[Select[d, #[[2]] == x &]], 1][[1]]
ContainsBoth[d_, x_, y_] := Length[Intersection[Contains[d, x], Contains[d, y]]]
ProbabilityOfXGivenY[d_, x_, y_] := ContainsBoth[d, x, y]/Length[Contains[d, y]]
I would like to print out a table or export into a file

As my effort, I can write a brute force solution, that works well for input size of 100 instances.
GetRow[d_, {x_, y_}] := {x, y, ContainsBoth[d, x, y],
100 ProbabilityOfXGivenY[d, x, y] // N,
100 ProbabilityOfXGivenY[d, y, x] // N}
Select[GetRow[d, #] & /@
Select[Tuples[DeleteDuplicates[Transpose[d][[2]]], 2],
(#[[1]] < #[[2]]) &], #[[3]] > 1 &];
TableForm[%, TableHeadings -> {Automatic, {"x", "y", "\[Exists]{x,y}", "P(x|y)", "P(y|x)"}}]
edit: I slightly improved my bruteforce solution:
d = Import["https://raw.github.com/gist/4040530/
fb175e2134559351354572a4aa0554d462cae31b/gistfile1.txt", "Package"];
data = Take[d, 100000];
MultiSet[hm_, key_, value_] := hm[key] = value;
ContainsBoth[hm_, x_, y_] := Length[Intersection[MS[x], MS[y]]]
P[hm_, x_, y_] := 100 (ContainsBoth[hm, x, y]/Length[hm[y]])
GetRow[d_, {x_, y_}] := {x, y, ContainsBoth[d, x, y], P[d, x, y] // N, P[d, y, x] // N}
Timing[
Clear[MS];
MultiSet[MS, #[[1, 2]], #[[All, 1]]] & /@ SplitBy[SortBy[data, Last], Last];
parsedData = {#[[1, 1]], #[[All, 2]]} & /@ SplitBy[SortBy[data, First], First];
nrnodes = Length[pairs = DeleteDuplicates[Flatten[Subsets[#, {2}] & /@
parsedData[[All, 2]], 1]]];
result = GetRow[MS, #] & /@ pairs;
nrnodes]
This completes in 41.091 s on my computer.
Export[FileNameJoin[{NotebookDirectory[], "datax.csv"}], Reverse[SortBy[result, #[[3]] &]]]



Select[Tuples[DeleteDuplicatesandTake[Transpose[Select[d, #[[2]] == x &]], 1][[1]]are like the dumbest things ever, but I am not sure how to store the data in Mathematica. – Margus Nov 8 '12 at 20:05