Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for multi-class random forest. #5

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 23 additions & 11 deletions conifer/converters/sklearn.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,29 @@ def convert_random_forest(bdt):
'n_classes' : bdt.n_classes_, 'trees' : [],
'init_predict' : [0] * bdt.n_classes_,
'norm' : 1}
for tree in bdt.estimators_:
treesl = []
tree = treeToDict(bdt, tree.tree_)
tree = padTree(ensembleDict, tree)
# Random forest takes the mean prediction, do that here
# Also need to scale the values by their sum
v = np.array(tree['value'])
tree['value'] = (v / v.sum(axis=2)[:, np.newaxis] / bdt.n_estimators)[:,0,0].tolist()
treesl.append(tree)
ensembleDict['trees'].append(treesl)

if bdt.n_classes_ == 2:
for tree in bdt.estimators_:
treesl = []
tree = treeToDict(bdt, tree.tree_)
tree = padTree(ensembleDict, tree)
# Random forest takes the mean prediction, do that here
# Also need to scale the values by their sum
v = np.array(tree['value'])
tree['value'] = (v / v.sum(axis=2)[:, np.newaxis] / bdt.n_estimators)[:,0,0].tolist()
treesl.append(tree)
ensembleDict['trees'].append(treesl)
else:
for tree in bdt.estimators_:
trees_list = []
for c in range(bdt.n_classes_):
tree_dict = treeToDict(bdt, tree.tree_)
padded_tree = padTree(ensembleDict, tree_dict)
# Random forest takes the mean prediction, do that here
# Also need to scale the values by their sum
v = np.array(padded_tree['value'])
padded_tree['value'] = (v / v.sum(axis=2)[:, np.newaxis] / bdt.n_estimators)[:, 0, c].tolist()
trees_list.append(padded_tree)
ensembleDict['trees'].append(trees_list)
return ensembleDict

def convert(bdt):
Expand Down