Quantcast
Channel: How to extract sklearn decision tree rules to pandas boolean conditions? - Stack Overflow
Viewing all articles
Browse latest Browse all 4

Answer by Thomas Funk for How to extract sklearn decision tree rules to pandas boolean conditions?

$
0
0

I figured out a further solution to this problem (a second part to the one posted by vlemaistre) which allows the user to run through any node and subset the data based on the pandas boolean condition.

node_id = 3def datatree_path_summarystats(node_id):    for k, v in paths.items():        if node_id in v:            d = k,v    ruleskey = d[0]    numberofsteps = sum(map(lambda x : x<node_id, d[1]))    for k, v in rules.items():        if k == ruleskey:            b = k,v    stringsubset = b[1]    datasubset = "&".join(stringsubset.split('&')[:numberofsteps])    return datasubsetdatasubset = datatree_path_summarystats(node_id)df[eval(datasubset)]

This function runs through the paths that contain the node id you are looking for. It will then split the rule based on that number of nodes creating the logic to subset the dataframe based on that one specific node.


Viewing all articles
Browse latest Browse all 4