Sklearn notes

From raju

Decision Trees


The sklearn library provides two ways to do decision trees - classification based, regression based.

Pros: well written, easy to follow, contains sample code.
Issues: Does not show how to do it with pandas dataframes, does not show how to handle categorical variables. Only shows how to export the decision tree to an image and does not show how to print it in text format.

Pros: well written, easy to follow. It helped me to get decision trees up and running in no time.

Cons: In the preprocessing step, the code uses an encode_target() function to handle categorical columns. But this approach has multiple problems.

  1. The function "encodes" different "states" in the "target" column with integers. But those different "states" need not have a monotonic relationship. For example, if the "target" column consists of RED, BLUE, GREEN states and if we assign 1, 2,3 to them then there could be some artifacts since the mapping inherently assumes that RED < BLUE < GREEN and BLUE = AVERAGE(RED, GREEN) etc.,
  2. The function cannot handle multiple target variables.

A better way of handling categorical columns is to use pd.get_dummies( df[features] ) and create a dummy variable for each "state" in each "target" column.

API Documentation:

Related Links:

handling categorical variables

  • To handle categorical variables, create dummy variables for each state of a categorical variable by using pd.get_dummies( df[features] )

internal structure

The inspect module can be used to get the elements of a tree object

    from inspect import getmembers
    print( getmembers( clf.tree_ ) )

traverse a decision tree

external links