in Education by
How can I extract the decision path as a textual list from a trained tree in a decision tree ? Something similar to this: if P>0.4 then if Q<0.2 then if R>0.8 then class='A' Select the correct answer from above options

1 Answer

0 votes
by
 
Best answer
To extract the decision rules from scikit-learn decision-tree try this code below: from sklearn.tree import _tree def tree_to_code(tree,fnames): tree_ = tree.tree_ fnames = [ fnames[n] if n != _tree.TREE_UNDEFINED else "undefined!" for n in tree_.feature ] print "def tree({}):".format(", ".join(fnames)) def recurse(node, depth): ind = " " * depth if tree_.feature[node] != _tree.TREE_UNDEFINED: name = fnames[node] th = tree_.threshold[node] print "{}if {} <= {}:".format(ind, name, th) recurse(tree_.children_left[node], depth + 1) print "{}else: # if {} > {}".format(ind, name, th) recurse(tree_.children_right[node], depth + 1) else: print "{}return {}".format(ind, tree_.value[node]) recurse(0, 1) The above code prints a valid Python function. Example: output of a tree which is trying to return a number between 0 to 10 def tree(m0): if m0 <= 6.0: if m0 <= 1.5: return [[ 0.]] else: if m0 <= 4.5: if m0 <= 3.5: return [[ 3.]] else: return [[ 4.]] else: return [[ 5.]] else: if m0 <= 8.5: if m0 <= 7.5: return [[ 7.]] else: return [[ 8.]] else: return [[ 9.]]

Related questions

0 votes
    The classifiers in machine learning packages like liblinear and nltk offer a method show_most_informative_features(), which ... lot! Select the correct answer from above options...
asked Feb 4, 2022 in Education by JackTerrance
0 votes
    Today I'm trying to learn something about K-means. I Have understood the algorithm and I know how it works. Now I ... a lot of time? Select the correct answer from above options...
asked Jan 29, 2022 in Education by JackTerrance
0 votes
    How can I save a trained Naive Bayes classifier to a disk and use it for predicting data? Select the correct answer from above options...
asked Jan 22, 2022 in Education by JackTerrance
0 votes
    Can anyone tell me why scikit learn is used? Select the correct answer from above options...
asked Jan 10, 2022 in Education by JackTerrance
0 votes
    Can anyone tell me what is test size in Scikit learn? Select the correct answer from above options...
asked Jan 10, 2022 in Education by JackTerrance
0 votes
    Can I specify my own distance function using scikit-learn K-Means Clustering? Select the correct answer from above options...
asked Jan 22, 2022 in Education by JackTerrance
0 votes
    I'm starting with input data like this df1 = pandas.DataFrame( { "Name" : ["Alice", "Bob", "Mallory", ... Any hints would be welcome. Select the correct answer from above options...
asked Jan 28, 2022 in Education by JackTerrance
0 votes
    While training a tensorflow seq2seq model I see the following messages : W tensorflow/core/common_runtime/gpu/pool_allocator ... GB GPU Select the correct answer from above options...
asked Feb 8, 2022 in Education by JackTerrance
0 votes
    While training a tensorflow seq2seq model I see the following messages : W tensorflow/core/common_runtime/gpu/pool_allocator ... GB GPU Select the correct answer from above options...
asked Feb 5, 2022 in Education by JackTerrance
0 votes
    Classification problems, such as logistic regression or multinomial logistic regression, optimize a cross-entropy loss. ... jungle. Select the correct answer from above options...
asked Feb 4, 2022 in Education by JackTerrance
0 votes
    I am trying to groupby a column and compute value counts on another column. import pandas as pd dftest = pd. ... Amt, already exists Select the correct answer from above options...
asked Feb 1, 2022 in Education by JackTerrance
0 votes
    I have just built my first model using Keras and this is the output. It looks like the standard output you get ... - loss: 0.1928 Select the correct answer from above options...
asked Feb 1, 2022 in Education by JackTerrance
0 votes
    I can't decipher however the sklearn.pipeline.Pipeline works precisely. Some explanation in this documentation. What ... estimator)]) Select the correct answer from above options...
asked Jan 22, 2022 in Education by JackTerrance
0 votes
    Can anyone tell me what should I learn Scikit learn or TensorFlow? Select the correct answer from above options...
asked Jan 10, 2022 in Education by JackTerrance
0 votes
    What is the difference between the two? It seems that both create new columns, in which their number is equal to ... they are in. Select the correct answer from above options...
asked Feb 1, 2022 in Education by JackTerrance
...