Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

prune.html 18 KB

You have to be logged in to leave a comment. Sign In
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1, minimum-scale=1" />
  6. <meta name="generator" content="pdoc 0.10.0" />
  7. <link rel="preload stylesheet" as="style" href="https://cdnjs.cloudflare.com/ajax/libs/10up-sanitize.css/11.0.1/sanitize.min.css" integrity="sha256-PK9q560IAAa6WVRRh76LtCaI8pjTJ2z11v0miyNNjrs=" crossorigin>
  8. <link rel="preload stylesheet" as="style" href="https://cdnjs.cloudflare.com/ajax/libs/10up-sanitize.css/11.0.1/typography.min.css" integrity="sha256-7l/o7C8jubJiy74VsKTidCy1yBkRtiUGbVkYBylBqUg=" crossorigin>
  9. <link rel="stylesheet preload" as="style" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/10.1.1/styles/github.min.css" crossorigin>
  10. <style>:root{--highlight-color:#fe9}.flex{display:flex !important}body{line-height:1.5em}#content{padding:20px}#sidebar{padding:30px;overflow:hidden}#sidebar > *:last-child{margin-bottom:2cm}.http-server-breadcrumbs{font-size:130%;margin:0 0 15px 0}#footer{font-size:.75em;padding:5px 30px;border-top:1px solid #ddd;text-align:right}#footer p{margin:0 0 0 1em;display:inline-block}#footer p:last-child{margin-right:30px}h1,h2,h3,h4,h5{font-weight:300}h1{font-size:2.5em;line-height:1.1em}h2{font-size:1.75em;margin:1em 0 .50em 0}h3{font-size:1.4em;margin:25px 0 10px 0}h4{margin:0;font-size:105%}h1:target,h2:target,h3:target,h4:target,h5:target,h6:target{background:var(--highlight-color);padding:.2em 0}a{color:#058;text-decoration:none;transition:color .3s ease-in-out}a:hover{color:#e82}.title code{font-weight:bold}h2[id^="header-"]{margin-top:2em}.ident{color:#900}pre code{background:#f8f8f8;font-size:.8em;line-height:1.4em}code{background:#f2f2f1;padding:1px 4px;overflow-wrap:break-word}h1 code{background:transparent}pre{background:#f8f8f8;border:0;border-top:1px solid #ccc;border-bottom:1px solid #ccc;margin:1em 0;padding:1ex}#http-server-module-list{display:flex;flex-flow:column}#http-server-module-list div{display:flex}#http-server-module-list dt{min-width:10%}#http-server-module-list p{margin-top:0}.toc ul,#index{list-style-type:none;margin:0;padding:0}#index code{background:transparent}#index h3{border-bottom:1px solid #ddd}#index ul{padding:0}#index h4{margin-top:.6em;font-weight:bold}@media (min-width:200ex){#index .two-column{column-count:2}}@media (min-width:300ex){#index .two-column{column-count:3}}dl{margin-bottom:2em}dl dl:last-child{margin-bottom:4em}dd{margin:0 0 1em 3em}#header-classes + dl > dd{margin-bottom:3em}dd dd{margin-left:2em}dd p{margin:10px 0}.name{background:#eee;font-weight:bold;font-size:.85em;padding:5px 10px;display:inline-block;min-width:40%}.name:hover{background:#e0e0e0}dt:target .name{background:var(--highlight-color)}.name > span:first-child{white-space:nowrap}.name.class > span:nth-child(2){margin-left:.4em}.inherited{color:#999;border-left:5px solid #eee;padding-left:1em}.inheritance em{font-style:normal;font-weight:bold}.desc h2{font-weight:400;font-size:1.25em}.desc h3{font-size:1em}.desc dt code{background:inherit}.source summary,.git-link-div{color:#666;text-align:right;font-weight:400;font-size:.8em;text-transform:uppercase}.source summary > *{white-space:nowrap;cursor:pointer}.git-link{color:inherit;margin-left:1em}.source pre{max-height:500px;overflow:auto;margin:0}.source pre code{font-size:12px;overflow:visible}.hlist{list-style:none}.hlist li{display:inline}.hlist li:after{content:',\2002'}.hlist li:last-child:after{content:none}.hlist .hlist{display:inline;padding-left:1em}img{max-width:100%}td{padding:0 .5em}.admonition{padding:.1em .5em;margin-bottom:1em}.admonition-title{font-weight:bold}.admonition.note,.admonition.info,.admonition.important{background:#aef}.admonition.todo,.admonition.versionadded,.admonition.tip,.admonition.hint{background:#dfd}.admonition.warning,.admonition.versionchanged,.admonition.deprecated{background:#fd4}.admonition.error,.admonition.danger,.admonition.caution{background:lightpink}</style>
  11. <style media="screen and (min-width: 700px)">@media screen and (min-width:700px){#sidebar{width:30%;height:100vh;overflow:auto;position:sticky;top:0}#content{width:70%;max-width:100ch;padding:3em 4em;border-left:1px solid #ddd}pre code{font-size:1em}.item .name{font-size:1em}main{display:flex;flex-direction:row-reverse;justify-content:flex-end}.toc ul ul,#index ul{padding-left:1.5em}.toc > ul > li{margin-top:.5em}}</style>
  12. <style media="print">@media print{#sidebar h1{page-break-before:always}.source{display:none}}@media print{*{background:transparent !important;color:#000 !important;box-shadow:none !important;text-shadow:none !important}a[href]:after{content:" (" attr(href) ")";font-size:90%}a[href][title]:after{content:none}abbr[title]:after{content:" (" attr(title) ")"}.ir a:after,a[href^="javascript:"]:after,a[href^="#"]:after{content:""}pre,blockquote{border:1px solid #999;page-break-inside:avoid}thead{display:table-header-group}tr,img{page-break-inside:avoid}img{max-width:100% !important}@page{margin:0.5cm}p,h2,h3{orphans:3;widows:3}h1,h2,h3,h4,h5,h6{page-break-after:avoid}}</style>
  13. <script defer src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/10.1.1/highlight.min.js" integrity="sha256-Uv3H6lx7dJmRfRvH8TH6kJD1TSK1aFcwgx+mdg3epi8=" crossorigin></script>
  14. <script>window.addEventListener('DOMContentLoaded', () => hljs.initHighlighting())</script>
  15. </head>
  16. <body>
  17. <main>
  18. <article id="content">
  19. <section id="section-intro">
  20. <details class="source">
  21. <summary>
  22. <span>Expand source code</span>
  23. </summary>
  24. <pre><code class="python">from typing import List
  25. from collections import Counter
  26. from imodels.util.rule import Rule
  27. def prune_mins(rules: List[Rule], precision_min: float, recall_min: float) -&gt; List[Rule]:
  28. # Factorize rules before semantic tree filtering
  29. rules_ = [tuple(rule) for rule in rules]
  30. rules_dict = {}
  31. # keep only rules verifying precision_min and recall_min:
  32. for rule, score in rules_:
  33. if score[0] &gt;= precision_min and score[1] &gt;= recall_min:
  34. if rule in rules_dict:
  35. # update the score to the new mean
  36. c = rules_dict[rule][2] + 1
  37. b = rules_dict[rule][1] + 1. / c * (
  38. score[1] - rules_dict[rule][1])
  39. a = rules_dict[rule][0] + 1. / c * (
  40. score[0] - rules_dict[rule][0])
  41. rules_dict[rule] = (a, b, c)
  42. else:
  43. rules_dict[rule] = (score[0], score[1], 1)
  44. rule_tuple_list = sorted(rules_dict.items(), key=lambda x: (x[1][0], x[1][1]), reverse=True)
  45. return [Rule(rule, args=scores) for rule, scores in rule_tuple_list]
  46. def deduplicate(rules: List[Rule], max_depth_dup: int) -&gt; List[Rule]:
  47. if max_depth_dup is not None:
  48. rules = [max(rules_set, key=f1_score) for rules_set in find_similar_rulesets(rules, max_depth_dup)]
  49. return sorted(rules, key=lambda x: - f1_score(x))
  50. def f1_score(rule: Rule) -&gt; float:
  51. return 2 * rule.args[0] * rule.args[1] / \
  52. (rule.args[0] + rule.args[1]) if (rule.args[0] + rule.args[1]) &gt; 0 else 0
  53. def find_similar_rulesets(rules: List[Rule], max_depth_duplication: int = None) -&gt; List[List[Rule]]:
  54. &#34;&#34;&#34;Create clusters of rules using a decision tree based
  55. on the terms of the rules
  56. Parameters
  57. ----------
  58. rules : List, List of rules
  59. The rules that should be splitted in subsets of similar rules
  60. Returns
  61. -------
  62. rules : List of list of rules
  63. The different set of rules. Each set should be homogeneous
  64. &#34;&#34;&#34;
  65. def split_with_best_feature(rules, depth, exceptions=[]):
  66. &#34;&#34;&#34;
  67. Method to find a split of rules given most represented feature
  68. &#34;&#34;&#34;
  69. if depth == 0:
  70. return rules
  71. rulelist = [rule.split(&#39; and &#39;) for rule, score in rules]
  72. terms = [t.split(&#39; &#39;)[0] for term in rulelist for t in term]
  73. counter = Counter(terms)
  74. # Drop exception list
  75. for exception in exceptions:
  76. del counter[exception]
  77. if len(counter) == 0:
  78. return rules
  79. most_represented_term = counter.most_common()[0][0]
  80. # Proceed to split
  81. rules_splitted = [[], [], []]
  82. for rule in rules:
  83. if (most_represented_term + &#39; &lt;=&#39;) in rule.rule:
  84. rules_splitted[0].append(rule)
  85. elif (most_represented_term + &#39; &gt;&#39;) in rule.rule:
  86. rules_splitted[1].append(rule)
  87. else:
  88. rules_splitted[2].append(rule)
  89. new_exceptions = exceptions + [most_represented_term]
  90. # Choose best term
  91. return [split_with_best_feature(ruleset,
  92. depth - 1,
  93. exceptions=new_exceptions)
  94. for ruleset in rules_splitted]
  95. def breadth_first_search(rules, leaves=None):
  96. if len(rules) == 0 or not isinstance(rules[0], list):
  97. if len(rules) &gt; 0:
  98. return leaves.append(rules)
  99. else:
  100. for rules_child in rules:
  101. breadth_first_search(rules_child, leaves=leaves)
  102. return leaves
  103. leaves = []
  104. res = split_with_best_feature(rules, max_depth_duplication)
  105. breadth_first_search(res, leaves=leaves)
  106. return leaves</code></pre>
  107. </details>
  108. </section>
  109. <section>
  110. </section>
  111. <section>
  112. </section>
  113. <section>
  114. <h2 class="section-title" id="header-functions">Functions</h2>
  115. <dl>
  116. <dt id="imodels.util.prune.deduplicate"><code class="name flex">
  117. <span>def <span class="ident">deduplicate</span></span>(<span>rules: List[<a title="imodels.util.rule.Rule" href="rule.html#imodels.util.rule.Rule">Rule</a>], max_depth_dup: int) ‑> List[<a title="imodels.util.rule.Rule" href="rule.html#imodels.util.rule.Rule">Rule</a>]</span>
  118. </code></dt>
  119. <dd>
  120. <div class="desc"></div>
  121. <details class="source">
  122. <summary>
  123. <span>Expand source code</span>
  124. </summary>
  125. <pre><code class="python">def deduplicate(rules: List[Rule], max_depth_dup: int) -&gt; List[Rule]:
  126. if max_depth_dup is not None:
  127. rules = [max(rules_set, key=f1_score) for rules_set in find_similar_rulesets(rules, max_depth_dup)]
  128. return sorted(rules, key=lambda x: - f1_score(x))</code></pre>
  129. </details>
  130. </dd>
  131. <dt id="imodels.util.prune.f1_score"><code class="name flex">
  132. <span>def <span class="ident">f1_score</span></span>(<span>rule: <a title="imodels.util.rule.Rule" href="rule.html#imodels.util.rule.Rule">Rule</a>) ‑> float</span>
  133. </code></dt>
  134. <dd>
  135. <div class="desc"></div>
  136. <details class="source">
  137. <summary>
  138. <span>Expand source code</span>
  139. </summary>
  140. <pre><code class="python">def f1_score(rule: Rule) -&gt; float:
  141. return 2 * rule.args[0] * rule.args[1] / \
  142. (rule.args[0] + rule.args[1]) if (rule.args[0] + rule.args[1]) &gt; 0 else 0</code></pre>
  143. </details>
  144. </dd>
  145. <dt id="imodels.util.prune.find_similar_rulesets"><code class="name flex">
  146. <span>def <span class="ident">find_similar_rulesets</span></span>(<span>rules: List[<a title="imodels.util.rule.Rule" href="rule.html#imodels.util.rule.Rule">Rule</a>], max_depth_duplication: int = None) ‑> List[List[<a title="imodels.util.rule.Rule" href="rule.html#imodels.util.rule.Rule">Rule</a>]]</span>
  147. </code></dt>
  148. <dd>
  149. <div class="desc"><p>Create clusters of rules using a decision tree based
  150. on the terms of the rules</p>
  151. <h2 id="parameters">Parameters</h2>
  152. <dl>
  153. <dt><strong><code>rules</code></strong> :&ensp;<code>List, List</code> of <code>rules</code></dt>
  154. <dd>The rules that should be splitted in subsets of similar rules</dd>
  155. </dl>
  156. <h2 id="returns">Returns</h2>
  157. <dl>
  158. <dt><strong><code>rules</code></strong> :&ensp;<code>List</code> of <code>list</code> of <code>rules</code></dt>
  159. <dd>The different set of rules. Each set should be homogeneous</dd>
  160. </dl></div>
  161. <details class="source">
  162. <summary>
  163. <span>Expand source code</span>
  164. </summary>
  165. <pre><code class="python">def find_similar_rulesets(rules: List[Rule], max_depth_duplication: int = None) -&gt; List[List[Rule]]:
  166. &#34;&#34;&#34;Create clusters of rules using a decision tree based
  167. on the terms of the rules
  168. Parameters
  169. ----------
  170. rules : List, List of rules
  171. The rules that should be splitted in subsets of similar rules
  172. Returns
  173. -------
  174. rules : List of list of rules
  175. The different set of rules. Each set should be homogeneous
  176. &#34;&#34;&#34;
  177. def split_with_best_feature(rules, depth, exceptions=[]):
  178. &#34;&#34;&#34;
  179. Method to find a split of rules given most represented feature
  180. &#34;&#34;&#34;
  181. if depth == 0:
  182. return rules
  183. rulelist = [rule.split(&#39; and &#39;) for rule, score in rules]
  184. terms = [t.split(&#39; &#39;)[0] for term in rulelist for t in term]
  185. counter = Counter(terms)
  186. # Drop exception list
  187. for exception in exceptions:
  188. del counter[exception]
  189. if len(counter) == 0:
  190. return rules
  191. most_represented_term = counter.most_common()[0][0]
  192. # Proceed to split
  193. rules_splitted = [[], [], []]
  194. for rule in rules:
  195. if (most_represented_term + &#39; &lt;=&#39;) in rule.rule:
  196. rules_splitted[0].append(rule)
  197. elif (most_represented_term + &#39; &gt;&#39;) in rule.rule:
  198. rules_splitted[1].append(rule)
  199. else:
  200. rules_splitted[2].append(rule)
  201. new_exceptions = exceptions + [most_represented_term]
  202. # Choose best term
  203. return [split_with_best_feature(ruleset,
  204. depth - 1,
  205. exceptions=new_exceptions)
  206. for ruleset in rules_splitted]
  207. def breadth_first_search(rules, leaves=None):
  208. if len(rules) == 0 or not isinstance(rules[0], list):
  209. if len(rules) &gt; 0:
  210. return leaves.append(rules)
  211. else:
  212. for rules_child in rules:
  213. breadth_first_search(rules_child, leaves=leaves)
  214. return leaves
  215. leaves = []
  216. res = split_with_best_feature(rules, max_depth_duplication)
  217. breadth_first_search(res, leaves=leaves)
  218. return leaves</code></pre>
  219. </details>
  220. </dd>
  221. <dt id="imodels.util.prune.prune_mins"><code class="name flex">
  222. <span>def <span class="ident">prune_mins</span></span>(<span>rules: List[<a title="imodels.util.rule.Rule" href="rule.html#imodels.util.rule.Rule">Rule</a>], precision_min: float, recall_min: float) ‑> List[<a title="imodels.util.rule.Rule" href="rule.html#imodels.util.rule.Rule">Rule</a>]</span>
  223. </code></dt>
  224. <dd>
  225. <div class="desc"></div>
  226. <details class="source">
  227. <summary>
  228. <span>Expand source code</span>
  229. </summary>
  230. <pre><code class="python">def prune_mins(rules: List[Rule], precision_min: float, recall_min: float) -&gt; List[Rule]:
  231. # Factorize rules before semantic tree filtering
  232. rules_ = [tuple(rule) for rule in rules]
  233. rules_dict = {}
  234. # keep only rules verifying precision_min and recall_min:
  235. for rule, score in rules_:
  236. if score[0] &gt;= precision_min and score[1] &gt;= recall_min:
  237. if rule in rules_dict:
  238. # update the score to the new mean
  239. c = rules_dict[rule][2] + 1
  240. b = rules_dict[rule][1] + 1. / c * (
  241. score[1] - rules_dict[rule][1])
  242. a = rules_dict[rule][0] + 1. / c * (
  243. score[0] - rules_dict[rule][0])
  244. rules_dict[rule] = (a, b, c)
  245. else:
  246. rules_dict[rule] = (score[0], score[1], 1)
  247. rule_tuple_list = sorted(rules_dict.items(), key=lambda x: (x[1][0], x[1][1]), reverse=True)
  248. return [Rule(rule, args=scores) for rule, scores in rule_tuple_list]</code></pre>
  249. </details>
  250. </dd>
  251. </dl>
  252. </section>
  253. <section>
  254. </section>
  255. </article>
  256. <nav id="sidebar">
  257. <h1>Index 🔍</h1>
  258. <div class="toc">
  259. <ul></ul>
  260. </div>
  261. <ul id="index">
  262. <li><h3>Super-module</h3>
  263. <ul>
  264. <li><code><a title="imodels.util" href="index.html">imodels.util</a></code></li>
  265. </ul>
  266. </li>
  267. <li><h3><a href="#header-functions">Functions</a></h3>
  268. <ul class="">
  269. <li><code><a title="imodels.util.prune.deduplicate" href="#imodels.util.prune.deduplicate">deduplicate</a></code></li>
  270. <li><code><a title="imodels.util.prune.f1_score" href="#imodels.util.prune.f1_score">f1_score</a></code></li>
  271. <li><code><a title="imodels.util.prune.find_similar_rulesets" href="#imodels.util.prune.find_similar_rulesets">find_similar_rulesets</a></code></li>
  272. <li><code><a title="imodels.util.prune.prune_mins" href="#imodels.util.prune.prune_mins">prune_mins</a></code></li>
  273. </ul>
  274. </li>
  275. </ul>
  276. <p><img align="center" width=100% src="https://csinva.io/imodels/img/anim.gif"> </img></p>
  277. <!-- add wave animation -->
  278. </nav>
  279. </main>
  280. <footer id="footer">
  281. </footer>
  282. </body>
  283. </html>
  284. <!-- add github corner -->
  285. <a href="https://github.com/csinva/imodels" class="github-corner" aria-label="View source on GitHub"><svg width="120" height="120" viewBox="0 0 250 250" style="fill:#70B7FD; color:#fff; position: absolute; top: 0; border: 0; right: 0;" aria-hidden="true"><path d="M0,0 L115,115 L130,115 L142,142 L250,250 L250,0 Z"></path><path d="m128.3,109.0 c113.8,99.7 119.0,89.6 119.0,89.6 c122.0,82.7 120.5,78.6 120.5,78.6 c119.2,72.0 123.4,76.3 123.4,76.3 c127.3,80.9 125.5,87.3 125.5,87.3 c122.9,97.6 130.6,101.9 134.4,103.2" fill="currentcolor" style="transform-origin: 130px 106px;" class="octo-arm"></path><path d="M115.0,115.0 C114.9,115.1 118.7,116.5 119.8,115.4 L133.7,101.6 C136.9,99.2 139.9,98.4 142.2,98.6 C133.8,88.0 127.5,74.4 143.8,58.0 C148.5,53.4 154.0,51.2 159.7,51.0 C160.3,49.4 163.2,43.6 171.4,40.1 C171.4,40.1 176.1,42.5 178.8,56.2 C183.1,58.6 187.2,61.8 190.9,65.4 C194.5,69.0 197.7,73.2 200.1,77.6 C213.8,80.2 216.3,84.9 216.3,84.9 C212.7,93.1 206.9,96.0 205.4,96.6 C205.1,102.4 203.0,107.8 198.3,112.5 C181.9,128.9 168.3,122.5 157.7,114.1 C157.9,116.9 156.7,120.9 152.7,124.9 L141.0,136.5 C139.8,137.7 141.6,141.9 141.8,141.8 Z" fill="currentColor" class="octo-body"></path></svg></a><style>.github-corner:hover .octo-arm{animation:octocat-wave 560ms ease-in-out}@keyframes octocat-wave{0%,100%{transform:rotate(0)}20%,60%{transform:rotate(-25deg)}40%,80%{transform:rotate(10deg)}}@media (max-width:500px){.github-corner:hover .octo-arm{animation:none}.github-corner .octo-arm{animation:octocat-wave 560ms ease-in-out}}</style>
  286. <!-- add wave animation stylesheet -->
  287. <link rel="stylesheet" href="github.css">
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...