Skip to content

Commit a525f09

Browse files
authored
Merge pull request #225 from awhug/master
Revising printing of GreedyRuleListClassifier
2 parents b87971c + f1fdd6e commit a525f09

1 file changed

Lines changed: 13 additions & 33 deletions

File tree

imodels/rule_list/greedy_rule_list.py

Lines changed: 13 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ def fit_node_recursive(self, X, y, depth: int, verbose):
9696

9797
# save info
9898
par_node = [{
99+
'depth': depth,
99100
'col': self.feature_names_[col],
100101
'index_col': col,
101102
'cutoff': cutoff,
@@ -140,46 +141,25 @@ def predict(self, X):
140141
X = check_array(X)
141142
return np.argmax(self.predict_proba(X), axis=1)
142143

143-
"""
144-
def __str__(self):
145-
# s = ''
146-
# for rule in self.rules_:
147-
# s += f"mean {rule['val'].round(3)} ({rule['num_pts']} pts)\n"
148-
# if 'col' in rule:
149-
# s += f"if {rule['col']} >= {rule['cutoff']} then {rule['val_right'].round(3)} ({rule['num_pts_right']} pts)\n"
150-
# return s
151-
"""
152144

153145
def __str__(self):
154146
'''Print out the list in a nice way
155147
'''
156-
s = '> ------------------------------\n> Greedy Rule List\n> ------------------------------\n'
157-
158-
def red(s):
159-
# return f"\033[91m{s}\033[00m"
160-
return s
161-
162-
def cyan(s):
163-
# return f"\033[96m{s}\033[00m"
164-
return s
165148

166-
def rule_name(rule):
167-
if rule['flip']:
168-
return '~' + rule['col']
169-
return rule['col']
170-
171-
# rule = self.rules_[0]
172-
# s += f"{red((100 * rule['val']).round(3))}% IwI ({rule['num_pts']} pts)\n"
149+
s = '> ------------------------------\n> Greedy Rule List\n> ------------------------------\n'
150+
precision = 2
173151
for rule in self.rules_:
174-
s += u'\u2193\n' + f"{cyan((100 * rule['val']).round(2))}% risk ({rule['num_pts']} pts)\n"
175-
# s += f"\t{'Else':>45} => {cyan((100 * rule['val']).round(2)):>6}% IwI ({rule['val'] * rule['num_pts']:.0f}/{rule['num_pts']} pts)\n"
176152
if 'col' in rule:
177-
# prefix = f"if {rule['col']} >= {rule['cutoff']}"
178-
prefix = f"if {rule_name(rule)}"
179-
val = f"{100 * rule['val_right'].round(3)}"
180-
s += f"\t{prefix} ==> {red(val)}% risk ({rule['num_pts_right']} pts)\n"
181-
# rule = self.rules_[-1]
182-
# s += f"{red((100 * rule['val']).round(3))}% IwI ({rule['num_pts']} pts)\n"
153+
prefix = "if" if rule['depth'] == 0 else "else if"
154+
sign = '<=' if rule['flip'] else '>'
155+
threshold = rule['cutoff'].round(precision)
156+
condition = f"{prefix} {rule['col']} {sign} {threshold}"
157+
pred_prob = (100 * rule['val_right']).round(precision)
158+
num_pts = rule['num_pts_right']
159+
s += f"> {condition} | {pred_prob}% pred prob ({num_pts} obs)\n"
160+
else:
161+
s += f"> else | {(100 * rule['val']).round(precision)}% pred prob ({rule['num_pts']} obs)\n"
162+
s += '> ------------------------------\n'
183163
return s
184164

185165
######## HERE ONWARDS CUSTOM SPLITTING (DEPRECATED IN FAVOR OF SKLEARN STUMP) ########

0 commit comments

Comments
 (0)