Mercurial > repos > bgruening > sklearn_clf_metrics
annotate association_rules.py @ 42:9fdff01a34b0 draft
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 9981e25b00de29ed881b2229a173a8c812ded9bb
| author | bgruening | 
|---|---|
| date | Wed, 09 Aug 2023 12:37:34 +0000 | 
| parents | ed7d6d7c6e93 | 
| children | 
| rev | line source | 
|---|---|
| 
37
 
149949f04f9f
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ea12f973df4b97a2691d9e4ce6bf6fae59d57717"
 
bgruening 
parents:  
diff
changeset
 | 
1 import argparse | 
| 
 
149949f04f9f
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ea12f973df4b97a2691d9e4ce6bf6fae59d57717"
 
bgruening 
parents:  
diff
changeset
 | 
2 import json | 
| 
 
149949f04f9f
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ea12f973df4b97a2691d9e4ce6bf6fae59d57717"
 
bgruening 
parents:  
diff
changeset
 | 
3 import warnings | 
| 
 
149949f04f9f
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ea12f973df4b97a2691d9e4ce6bf6fae59d57717"
 
bgruening 
parents:  
diff
changeset
 | 
4 | 
| 
 
149949f04f9f
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ea12f973df4b97a2691d9e4ce6bf6fae59d57717"
 
bgruening 
parents:  
diff
changeset
 | 
5 import pandas as pd | 
| 
 
149949f04f9f
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ea12f973df4b97a2691d9e4ce6bf6fae59d57717"
 
bgruening 
parents:  
diff
changeset
 | 
6 from mlxtend.frequent_patterns import association_rules, fpgrowth | 
| 
 
149949f04f9f
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ea12f973df4b97a2691d9e4ce6bf6fae59d57717"
 
bgruening 
parents:  
diff
changeset
 | 
7 from mlxtend.preprocessing import TransactionEncoder | 
| 
 
149949f04f9f
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ea12f973df4b97a2691d9e4ce6bf6fae59d57717"
 
bgruening 
parents:  
diff
changeset
 | 
8 | 
| 
 
149949f04f9f
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ea12f973df4b97a2691d9e4ce6bf6fae59d57717"
 
bgruening 
parents:  
diff
changeset
 | 
9 | 
| 
41
 
ed7d6d7c6e93
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 9981e25b00de29ed881b2229a173a8c812ded9bb
 
bgruening 
parents: 
37 
diff
changeset
 | 
10 def main( | 
| 
 
ed7d6d7c6e93
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 9981e25b00de29ed881b2229a173a8c812ded9bb
 
bgruening 
parents: 
37 
diff
changeset
 | 
11 inputs, | 
| 
 
ed7d6d7c6e93
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 9981e25b00de29ed881b2229a173a8c812ded9bb
 
bgruening 
parents: 
37 
diff
changeset
 | 
12 infile, | 
| 
 
ed7d6d7c6e93
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 9981e25b00de29ed881b2229a173a8c812ded9bb
 
bgruening 
parents: 
37 
diff
changeset
 | 
13 outfile, | 
| 
 
ed7d6d7c6e93
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 9981e25b00de29ed881b2229a173a8c812ded9bb
 
bgruening 
parents: 
37 
diff
changeset
 | 
14 min_support=0.5, | 
| 
 
ed7d6d7c6e93
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 9981e25b00de29ed881b2229a173a8c812ded9bb
 
bgruening 
parents: 
37 
diff
changeset
 | 
15 min_confidence=0.5, | 
| 
 
ed7d6d7c6e93
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 9981e25b00de29ed881b2229a173a8c812ded9bb
 
bgruening 
parents: 
37 
diff
changeset
 | 
16 min_lift=1.0, | 
| 
 
ed7d6d7c6e93
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 9981e25b00de29ed881b2229a173a8c812ded9bb
 
bgruening 
parents: 
37 
diff
changeset
 | 
17 min_conviction=1.0, | 
| 
 
ed7d6d7c6e93
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 9981e25b00de29ed881b2229a173a8c812ded9bb
 
bgruening 
parents: 
37 
diff
changeset
 | 
18 max_length=None, | 
| 
 
ed7d6d7c6e93
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 9981e25b00de29ed881b2229a173a8c812ded9bb
 
bgruening 
parents: 
37 
diff
changeset
 | 
19 ): | 
| 
37
 
149949f04f9f
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ea12f973df4b97a2691d9e4ce6bf6fae59d57717"
 
bgruening 
parents:  
diff
changeset
 | 
20 """ | 
| 
 
149949f04f9f
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ea12f973df4b97a2691d9e4ce6bf6fae59d57717"
 
bgruening 
parents:  
diff
changeset
 | 
21 Parameter | 
| 
 
149949f04f9f
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ea12f973df4b97a2691d9e4ce6bf6fae59d57717"
 
bgruening 
parents:  
diff
changeset
 | 
22 --------- | 
| 
 
149949f04f9f
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ea12f973df4b97a2691d9e4ce6bf6fae59d57717"
 
bgruening 
parents:  
diff
changeset
 | 
23 input : str | 
| 
 
149949f04f9f
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ea12f973df4b97a2691d9e4ce6bf6fae59d57717"
 
bgruening 
parents:  
diff
changeset
 | 
24 File path to galaxy tool parameter | 
| 
 
149949f04f9f
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ea12f973df4b97a2691d9e4ce6bf6fae59d57717"
 
bgruening 
parents:  
diff
changeset
 | 
25 | 
| 
 
149949f04f9f
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ea12f973df4b97a2691d9e4ce6bf6fae59d57717"
 
bgruening 
parents:  
diff
changeset
 | 
26 infile : str | 
| 
 
149949f04f9f
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ea12f973df4b97a2691d9e4ce6bf6fae59d57717"
 
bgruening 
parents:  
diff
changeset
 | 
27 File paths of input vector | 
| 
 
149949f04f9f
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ea12f973df4b97a2691d9e4ce6bf6fae59d57717"
 
bgruening 
parents:  
diff
changeset
 | 
28 | 
| 
 
149949f04f9f
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ea12f973df4b97a2691d9e4ce6bf6fae59d57717"
 
bgruening 
parents:  
diff
changeset
 | 
29 outfile : str | 
| 
 
149949f04f9f
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ea12f973df4b97a2691d9e4ce6bf6fae59d57717"
 
bgruening 
parents:  
diff
changeset
 | 
30 File path to output matrix | 
| 
 
149949f04f9f
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ea12f973df4b97a2691d9e4ce6bf6fae59d57717"
 
bgruening 
parents:  
diff
changeset
 | 
31 | 
| 
 
149949f04f9f
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ea12f973df4b97a2691d9e4ce6bf6fae59d57717"
 
bgruening 
parents:  
diff
changeset
 | 
32 min_support: float | 
| 
 
149949f04f9f
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ea12f973df4b97a2691d9e4ce6bf6fae59d57717"
 
bgruening 
parents:  
diff
changeset
 | 
33 Minimum support | 
| 
 
149949f04f9f
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ea12f973df4b97a2691d9e4ce6bf6fae59d57717"
 
bgruening 
parents:  
diff
changeset
 | 
34 | 
| 
 
149949f04f9f
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ea12f973df4b97a2691d9e4ce6bf6fae59d57717"
 
bgruening 
parents:  
diff
changeset
 | 
35 min_confidence: float | 
| 
 
149949f04f9f
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ea12f973df4b97a2691d9e4ce6bf6fae59d57717"
 
bgruening 
parents:  
diff
changeset
 | 
36 Minimum confidence | 
| 
 
149949f04f9f
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ea12f973df4b97a2691d9e4ce6bf6fae59d57717"
 
bgruening 
parents:  
diff
changeset
 | 
37 | 
| 
 
149949f04f9f
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ea12f973df4b97a2691d9e4ce6bf6fae59d57717"
 
bgruening 
parents:  
diff
changeset
 | 
38 min_lift: float | 
| 
 
149949f04f9f
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ea12f973df4b97a2691d9e4ce6bf6fae59d57717"
 
bgruening 
parents:  
diff
changeset
 | 
39 Minimum lift | 
| 
 
149949f04f9f
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ea12f973df4b97a2691d9e4ce6bf6fae59d57717"
 
bgruening 
parents:  
diff
changeset
 | 
40 | 
| 
 
149949f04f9f
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ea12f973df4b97a2691d9e4ce6bf6fae59d57717"
 
bgruening 
parents:  
diff
changeset
 | 
41 min_conviction: float | 
| 
 
149949f04f9f
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ea12f973df4b97a2691d9e4ce6bf6fae59d57717"
 
bgruening 
parents:  
diff
changeset
 | 
42 Minimum conviction | 
| 
 
149949f04f9f
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ea12f973df4b97a2691d9e4ce6bf6fae59d57717"
 
bgruening 
parents:  
diff
changeset
 | 
43 | 
| 
 
149949f04f9f
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ea12f973df4b97a2691d9e4ce6bf6fae59d57717"
 
bgruening 
parents:  
diff
changeset
 | 
44 max_length: int | 
| 
 
149949f04f9f
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ea12f973df4b97a2691d9e4ce6bf6fae59d57717"
 
bgruening 
parents:  
diff
changeset
 | 
45 Maximum length | 
| 
 
149949f04f9f
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ea12f973df4b97a2691d9e4ce6bf6fae59d57717"
 
bgruening 
parents:  
diff
changeset
 | 
46 | 
| 
 
149949f04f9f
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ea12f973df4b97a2691d9e4ce6bf6fae59d57717"
 
bgruening 
parents:  
diff
changeset
 | 
47 """ | 
| 
41
 
ed7d6d7c6e93
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 9981e25b00de29ed881b2229a173a8c812ded9bb
 
bgruening 
parents: 
37 
diff
changeset
 | 
48 warnings.simplefilter("ignore") | 
| 
37
 
149949f04f9f
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ea12f973df4b97a2691d9e4ce6bf6fae59d57717"
 
bgruening 
parents:  
diff
changeset
 | 
49 | 
| 
41
 
ed7d6d7c6e93
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 9981e25b00de29ed881b2229a173a8c812ded9bb
 
bgruening 
parents: 
37 
diff
changeset
 | 
50 with open(inputs, "r") as param_handler: | 
| 
37
 
149949f04f9f
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ea12f973df4b97a2691d9e4ce6bf6fae59d57717"
 
bgruening 
parents:  
diff
changeset
 | 
51 params = json.load(param_handler) | 
| 
 
149949f04f9f
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ea12f973df4b97a2691d9e4ce6bf6fae59d57717"
 
bgruening 
parents:  
diff
changeset
 | 
52 | 
| 
41
 
ed7d6d7c6e93
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 9981e25b00de29ed881b2229a173a8c812ded9bb
 
bgruening 
parents: 
37 
diff
changeset
 | 
53 input_header = params["header0"] | 
| 
 
ed7d6d7c6e93
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 9981e25b00de29ed881b2229a173a8c812ded9bb
 
bgruening 
parents: 
37 
diff
changeset
 | 
54 header = "infer" if input_header else None | 
| 
37
 
149949f04f9f
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ea12f973df4b97a2691d9e4ce6bf6fae59d57717"
 
bgruening 
parents:  
diff
changeset
 | 
55 | 
| 
 
149949f04f9f
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ea12f973df4b97a2691d9e4ce6bf6fae59d57717"
 
bgruening 
parents:  
diff
changeset
 | 
56 with open(infile) as fp: | 
| 
 
149949f04f9f
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ea12f973df4b97a2691d9e4ce6bf6fae59d57717"
 
bgruening 
parents:  
diff
changeset
 | 
57 lines = fp.read().splitlines() | 
| 
 
149949f04f9f
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ea12f973df4b97a2691d9e4ce6bf6fae59d57717"
 
bgruening 
parents:  
diff
changeset
 | 
58 | 
| 
 
149949f04f9f
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ea12f973df4b97a2691d9e4ce6bf6fae59d57717"
 
bgruening 
parents:  
diff
changeset
 | 
59 if header is not None: | 
| 
 
149949f04f9f
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ea12f973df4b97a2691d9e4ce6bf6fae59d57717"
 
bgruening 
parents:  
diff
changeset
 | 
60 lines = lines[1:] | 
| 
 
149949f04f9f
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ea12f973df4b97a2691d9e4ce6bf6fae59d57717"
 
bgruening 
parents:  
diff
changeset
 | 
61 | 
| 
 
149949f04f9f
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ea12f973df4b97a2691d9e4ce6bf6fae59d57717"
 
bgruening 
parents:  
diff
changeset
 | 
62 dataset = [] | 
| 
 
149949f04f9f
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ea12f973df4b97a2691d9e4ce6bf6fae59d57717"
 
bgruening 
parents:  
diff
changeset
 | 
63 for line in lines: | 
| 
 
149949f04f9f
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ea12f973df4b97a2691d9e4ce6bf6fae59d57717"
 
bgruening 
parents:  
diff
changeset
 | 
64 line_items = line.split("\t") | 
| 
 
149949f04f9f
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ea12f973df4b97a2691d9e4ce6bf6fae59d57717"
 
bgruening 
parents:  
diff
changeset
 | 
65 dataset.append(line_items) | 
| 
 
149949f04f9f
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ea12f973df4b97a2691d9e4ce6bf6fae59d57717"
 
bgruening 
parents:  
diff
changeset
 | 
66 | 
| 
 
149949f04f9f
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ea12f973df4b97a2691d9e4ce6bf6fae59d57717"
 
bgruening 
parents:  
diff
changeset
 | 
67 # TransactionEncoder learns the unique labels in the dataset and transforms the | 
| 
 
149949f04f9f
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ea12f973df4b97a2691d9e4ce6bf6fae59d57717"
 
bgruening 
parents:  
diff
changeset
 | 
68 # input dataset (a Python list of lists) into a one-hot encoded NumPy boolean array | 
| 
 
149949f04f9f
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ea12f973df4b97a2691d9e4ce6bf6fae59d57717"
 
bgruening 
parents:  
diff
changeset
 | 
69 te = TransactionEncoder() | 
| 
 
149949f04f9f
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ea12f973df4b97a2691d9e4ce6bf6fae59d57717"
 
bgruening 
parents:  
diff
changeset
 | 
70 te_ary = te.fit_transform(dataset) | 
| 
 
149949f04f9f
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ea12f973df4b97a2691d9e4ce6bf6fae59d57717"
 
bgruening 
parents:  
diff
changeset
 | 
71 | 
| 
 
149949f04f9f
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ea12f973df4b97a2691d9e4ce6bf6fae59d57717"
 
bgruening 
parents:  
diff
changeset
 | 
72 # Turn the encoded NumPy array into a DataFrame | 
| 
 
149949f04f9f
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ea12f973df4b97a2691d9e4ce6bf6fae59d57717"
 
bgruening 
parents:  
diff
changeset
 | 
73 df = pd.DataFrame(te_ary, columns=te.columns_) | 
| 
 
149949f04f9f
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ea12f973df4b97a2691d9e4ce6bf6fae59d57717"
 
bgruening 
parents:  
diff
changeset
 | 
74 | 
| 
 
149949f04f9f
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ea12f973df4b97a2691d9e4ce6bf6fae59d57717"
 
bgruening 
parents:  
diff
changeset
 | 
75 # Extract frequent itemsets for association rule mining | 
| 
 
149949f04f9f
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ea12f973df4b97a2691d9e4ce6bf6fae59d57717"
 
bgruening 
parents:  
diff
changeset
 | 
76 # use_colnames: Use DataFrames' column names in the returned DataFrame instead of column indices | 
| 
41
 
ed7d6d7c6e93
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 9981e25b00de29ed881b2229a173a8c812ded9bb
 
bgruening 
parents: 
37 
diff
changeset
 | 
77 frequent_itemsets = fpgrowth( | 
| 
 
ed7d6d7c6e93
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 9981e25b00de29ed881b2229a173a8c812ded9bb
 
bgruening 
parents: 
37 
diff
changeset
 | 
78 df, min_support=min_support, use_colnames=True, max_len=max_length | 
| 
 
ed7d6d7c6e93
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 9981e25b00de29ed881b2229a173a8c812ded9bb
 
bgruening 
parents: 
37 
diff
changeset
 | 
79 ) | 
| 
37
 
149949f04f9f
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ea12f973df4b97a2691d9e4ce6bf6fae59d57717"
 
bgruening 
parents:  
diff
changeset
 | 
80 | 
| 
 
149949f04f9f
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ea12f973df4b97a2691d9e4ce6bf6fae59d57717"
 
bgruening 
parents:  
diff
changeset
 | 
81 # Get association rules, with confidence larger than min_confidence | 
| 
41
 
ed7d6d7c6e93
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 9981e25b00de29ed881b2229a173a8c812ded9bb
 
bgruening 
parents: 
37 
diff
changeset
 | 
82 rules = association_rules( | 
| 
 
ed7d6d7c6e93
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 9981e25b00de29ed881b2229a173a8c812ded9bb
 
bgruening 
parents: 
37 
diff
changeset
 | 
83 frequent_itemsets, metric="confidence", min_threshold=min_confidence | 
| 
 
ed7d6d7c6e93
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 9981e25b00de29ed881b2229a173a8c812ded9bb
 
bgruening 
parents: 
37 
diff
changeset
 | 
84 ) | 
| 
37
 
149949f04f9f
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ea12f973df4b97a2691d9e4ce6bf6fae59d57717"
 
bgruening 
parents:  
diff
changeset
 | 
85 | 
| 
 
149949f04f9f
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ea12f973df4b97a2691d9e4ce6bf6fae59d57717"
 
bgruening 
parents:  
diff
changeset
 | 
86 # Filter association rules, keeping rules with lift and conviction larger than min_liftand and min_conviction | 
| 
41
 
ed7d6d7c6e93
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 9981e25b00de29ed881b2229a173a8c812ded9bb
 
bgruening 
parents: 
37 
diff
changeset
 | 
87 rules = rules[(rules["lift"] >= min_lift) & (rules["conviction"] >= min_conviction)] | 
| 
37
 
149949f04f9f
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ea12f973df4b97a2691d9e4ce6bf6fae59d57717"
 
bgruening 
parents:  
diff
changeset
 | 
88 | 
| 
 
149949f04f9f
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ea12f973df4b97a2691d9e4ce6bf6fae59d57717"
 
bgruening 
parents:  
diff
changeset
 | 
89 # Convert columns from frozenset to list (more readable) | 
| 
41
 
ed7d6d7c6e93
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 9981e25b00de29ed881b2229a173a8c812ded9bb
 
bgruening 
parents: 
37 
diff
changeset
 | 
90 rules["antecedents"] = rules["antecedents"].apply(list) | 
| 
 
ed7d6d7c6e93
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 9981e25b00de29ed881b2229a173a8c812ded9bb
 
bgruening 
parents: 
37 
diff
changeset
 | 
91 rules["consequents"] = rules["consequents"].apply(list) | 
| 
37
 
149949f04f9f
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ea12f973df4b97a2691d9e4ce6bf6fae59d57717"
 
bgruening 
parents:  
diff
changeset
 | 
92 | 
| 
 
149949f04f9f
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ea12f973df4b97a2691d9e4ce6bf6fae59d57717"
 
bgruening 
parents:  
diff
changeset
 | 
93 # The next 3 steps are intended to fix the order of the association | 
| 
 
149949f04f9f
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ea12f973df4b97a2691d9e4ce6bf6fae59d57717"
 
bgruening 
parents:  
diff
changeset
 | 
94 # rules generated, so tests that rely on diff'ing a desired output | 
| 
 
149949f04f9f
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ea12f973df4b97a2691d9e4ce6bf6fae59d57717"
 
bgruening 
parents:  
diff
changeset
 | 
95 # with an expected output can pass | 
| 
 
149949f04f9f
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ea12f973df4b97a2691d9e4ce6bf6fae59d57717"
 
bgruening 
parents:  
diff
changeset
 | 
96 | 
| 
 
149949f04f9f
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ea12f973df4b97a2691d9e4ce6bf6fae59d57717"
 
bgruening 
parents:  
diff
changeset
 | 
97 # 1) Sort entry in every row/column for columns 'antecedents' and 'consequents' | 
| 
41
 
ed7d6d7c6e93
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 9981e25b00de29ed881b2229a173a8c812ded9bb
 
bgruening 
parents: 
37 
diff
changeset
 | 
98 rules["antecedents"] = rules["antecedents"].apply(lambda row: sorted(row)) | 
| 
 
ed7d6d7c6e93
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 9981e25b00de29ed881b2229a173a8c812ded9bb
 
bgruening 
parents: 
37 
diff
changeset
 | 
99 rules["consequents"] = rules["consequents"].apply(lambda row: sorted(row)) | 
| 
37
 
149949f04f9f
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ea12f973df4b97a2691d9e4ce6bf6fae59d57717"
 
bgruening 
parents:  
diff
changeset
 | 
100 | 
| 
 
149949f04f9f
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ea12f973df4b97a2691d9e4ce6bf6fae59d57717"
 
bgruening 
parents:  
diff
changeset
 | 
101 # 2) Create two temporary string columns to sort on | 
| 
41
 
ed7d6d7c6e93
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 9981e25b00de29ed881b2229a173a8c812ded9bb
 
bgruening 
parents: 
37 
diff
changeset
 | 
102 rules["ant_str"] = rules["antecedents"].apply(lambda row: " ".join(row)) | 
| 
 
ed7d6d7c6e93
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 9981e25b00de29ed881b2229a173a8c812ded9bb
 
bgruening 
parents: 
37 
diff
changeset
 | 
103 rules["con_str"] = rules["consequents"].apply(lambda row: " ".join(row)) | 
| 
37
 
149949f04f9f
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ea12f973df4b97a2691d9e4ce6bf6fae59d57717"
 
bgruening 
parents:  
diff
changeset
 | 
104 | 
| 
 
149949f04f9f
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ea12f973df4b97a2691d9e4ce6bf6fae59d57717"
 
bgruening 
parents:  
diff
changeset
 | 
105 # 3) Sort results so they are re-producable | 
| 
41
 
ed7d6d7c6e93
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 9981e25b00de29ed881b2229a173a8c812ded9bb
 
bgruening 
parents: 
37 
diff
changeset
 | 
106 rules.sort_values(by=["ant_str", "con_str"], inplace=True) | 
| 
 
ed7d6d7c6e93
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 9981e25b00de29ed881b2229a173a8c812ded9bb
 
bgruening 
parents: 
37 
diff
changeset
 | 
107 del rules["ant_str"] | 
| 
 
ed7d6d7c6e93
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 9981e25b00de29ed881b2229a173a8c812ded9bb
 
bgruening 
parents: 
37 
diff
changeset
 | 
108 del rules["con_str"] | 
| 
37
 
149949f04f9f
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ea12f973df4b97a2691d9e4ce6bf6fae59d57717"
 
bgruening 
parents:  
diff
changeset
 | 
109 rules.reset_index(drop=True, inplace=True) | 
| 
 
149949f04f9f
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ea12f973df4b97a2691d9e4ce6bf6fae59d57717"
 
bgruening 
parents:  
diff
changeset
 | 
110 | 
| 
 
149949f04f9f
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ea12f973df4b97a2691d9e4ce6bf6fae59d57717"
 
bgruening 
parents:  
diff
changeset
 | 
111 # Write association rules and metrics to file | 
| 
 
149949f04f9f
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ea12f973df4b97a2691d9e4ce6bf6fae59d57717"
 
bgruening 
parents:  
diff
changeset
 | 
112 rules.to_csv(outfile, sep="\t", index=False) | 
| 
 
149949f04f9f
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ea12f973df4b97a2691d9e4ce6bf6fae59d57717"
 
bgruening 
parents:  
diff
changeset
 | 
113 | 
| 
 
149949f04f9f
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ea12f973df4b97a2691d9e4ce6bf6fae59d57717"
 
bgruening 
parents:  
diff
changeset
 | 
114 | 
| 
41
 
ed7d6d7c6e93
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 9981e25b00de29ed881b2229a173a8c812ded9bb
 
bgruening 
parents: 
37 
diff
changeset
 | 
115 if __name__ == "__main__": | 
| 
37
 
149949f04f9f
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ea12f973df4b97a2691d9e4ce6bf6fae59d57717"
 
bgruening 
parents:  
diff
changeset
 | 
116 aparser = argparse.ArgumentParser() | 
| 
 
149949f04f9f
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ea12f973df4b97a2691d9e4ce6bf6fae59d57717"
 
bgruening 
parents:  
diff
changeset
 | 
117 aparser.add_argument("-i", "--inputs", dest="inputs", required=True) | 
| 
 
149949f04f9f
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ea12f973df4b97a2691d9e4ce6bf6fae59d57717"
 
bgruening 
parents:  
diff
changeset
 | 
118 aparser.add_argument("-y", "--infile", dest="infile", required=True) | 
| 
 
149949f04f9f
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ea12f973df4b97a2691d9e4ce6bf6fae59d57717"
 
bgruening 
parents:  
diff
changeset
 | 
119 aparser.add_argument("-o", "--outfile", dest="outfile", required=True) | 
| 
 
149949f04f9f
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ea12f973df4b97a2691d9e4ce6bf6fae59d57717"
 
bgruening 
parents:  
diff
changeset
 | 
120 aparser.add_argument("-s", "--support", dest="support", default=0.5) | 
| 
 
149949f04f9f
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ea12f973df4b97a2691d9e4ce6bf6fae59d57717"
 
bgruening 
parents:  
diff
changeset
 | 
121 aparser.add_argument("-c", "--confidence", dest="confidence", default=0.5) | 
| 
 
149949f04f9f
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ea12f973df4b97a2691d9e4ce6bf6fae59d57717"
 
bgruening 
parents:  
diff
changeset
 | 
122 aparser.add_argument("-l", "--lift", dest="lift", default=1.0) | 
| 
 
149949f04f9f
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ea12f973df4b97a2691d9e4ce6bf6fae59d57717"
 
bgruening 
parents:  
diff
changeset
 | 
123 aparser.add_argument("-v", "--conviction", dest="conviction", default=1.0) | 
| 
 
149949f04f9f
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ea12f973df4b97a2691d9e4ce6bf6fae59d57717"
 
bgruening 
parents:  
diff
changeset
 | 
124 aparser.add_argument("-t", "--length", dest="length", default=5) | 
| 
 
149949f04f9f
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ea12f973df4b97a2691d9e4ce6bf6fae59d57717"
 
bgruening 
parents:  
diff
changeset
 | 
125 args = aparser.parse_args() | 
| 
 
149949f04f9f
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ea12f973df4b97a2691d9e4ce6bf6fae59d57717"
 
bgruening 
parents:  
diff
changeset
 | 
126 | 
| 
41
 
ed7d6d7c6e93
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 9981e25b00de29ed881b2229a173a8c812ded9bb
 
bgruening 
parents: 
37 
diff
changeset
 | 
127 main( | 
| 
 
ed7d6d7c6e93
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 9981e25b00de29ed881b2229a173a8c812ded9bb
 
bgruening 
parents: 
37 
diff
changeset
 | 
128 args.inputs, | 
| 
 
ed7d6d7c6e93
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 9981e25b00de29ed881b2229a173a8c812ded9bb
 
bgruening 
parents: 
37 
diff
changeset
 | 
129 args.infile, | 
| 
 
ed7d6d7c6e93
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 9981e25b00de29ed881b2229a173a8c812ded9bb
 
bgruening 
parents: 
37 
diff
changeset
 | 
130 args.outfile, | 
| 
 
ed7d6d7c6e93
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 9981e25b00de29ed881b2229a173a8c812ded9bb
 
bgruening 
parents: 
37 
diff
changeset
 | 
131 min_support=float(args.support), | 
| 
 
ed7d6d7c6e93
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 9981e25b00de29ed881b2229a173a8c812ded9bb
 
bgruening 
parents: 
37 
diff
changeset
 | 
132 min_confidence=float(args.confidence), | 
| 
 
ed7d6d7c6e93
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 9981e25b00de29ed881b2229a173a8c812ded9bb
 
bgruening 
parents: 
37 
diff
changeset
 | 
133 min_lift=float(args.lift), | 
| 
 
ed7d6d7c6e93
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 9981e25b00de29ed881b2229a173a8c812ded9bb
 
bgruening 
parents: 
37 
diff
changeset
 | 
134 min_conviction=float(args.conviction), | 
| 
 
ed7d6d7c6e93
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 9981e25b00de29ed881b2229a173a8c812ded9bb
 
bgruening 
parents: 
37 
diff
changeset
 | 
135 max_length=int(args.length), | 
| 
 
ed7d6d7c6e93
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 9981e25b00de29ed881b2229a173a8c812ded9bb
 
bgruening 
parents: 
37 
diff
changeset
 | 
136 ) | 
