2
$\begingroup$

I have some dataset with procurements of organization where actually i'm working. The aim is to find most important features that describe why some processes of purchases is succesful, and why not succesful.

To solve this, I'm using Decision Tree Classifier.

First, I convert boolen columns to int

for col in ['flag_prequalification', 'flag_procurement_category_strategy']:
data.loc[:, col] = data.loc[:, col].astype(int)

Then, convert object type to string. This for label encoding

for cat_feature in categorical_features:
    data.loc[:, cat_feature] = data.loc[:, cat_feature].fillna('unknown').astype(str)
%%time
encoder = defaultdict(LabelEncoder)
data.loc[:, categorical_features] = (
    data
    .loc[:, categorical_features]
    .apply(lambda x: encoder[x.name].fit_transform(x), axis=0)
)

After, let's use log(sum) instead of sum

for col in ['sum_tru_no_nds (lot)', 'price (lot)', 'count (lot)']:
data[col] = (data[col] + 1).apply(np.log)

And, in the end plot the tree:

from sklearn.externals.six import StringIO  
from IPython.display import Image  
from sklearn.tree import export_graphviz
import pydotplus

import graphviz
# DOT data
dot_data = tree.export_graphviz(clf, out_file=None, 
                                feature_names=None,  
                                class_names=target_col,
                                filled=True)

# Draw graph
graph = graphviz.Source(dot_data, format="png") 
graph

enter image description here

The question is, how can I explain this tree to others, more precisely how can I decode this labels in tree?

$\endgroup$

2 Answers 2

0
$\begingroup$

First, setting feature_names will replace all the X[2] and such. Using feature_names=data.columns is sensible, provided your feature names aren't too long for easy display.

Now, you'll still be comparing against the label encodings. By saving the label encoder objects (your encoder dict), you can retrieve which levels correspond to the integer labels (with the classes_ attribute, or possibly the inverse_transform method). If your features have many levels, then you probably don't want to display all the levels in each split, and just providing them off to the side somewhere might be best. If your features have few levels, maybe hacking the resulting DOT code will work; see this related answer of mine, which concerns one-hot encoded variables.

$\endgroup$
0
$\begingroup$

Since you are already using scikit-learn to fit the decision tree, you should scikit-learn to encode the features. For example, scikit-learn's OneHotEncoder has the get_feature_names attribute which return feature names for output features.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.