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