0
$\begingroup$

I created a Decision Tree Classifier using sklearn, defined the target variable:

#extract features and target variables
x = df.drop(columns="target_column",)
y = df["target_column"]
#save the feature name and target variables
feature_names = x.columns
labels = y.unique()
#split the dataset
from sklearn.model_selection import train_test_split
x_train, x_test, y_train, y_test = train_test_split(x,y, test_size = 0.3, random_state = 42)

Additionally I checked the count of each of the two classes (Success, Failure) within y which confirmed to me that each has the correct count.

Then I fitted my DTClassifier:

clf = DecisionTreeClassifier(
    criterion='gini',
    splitter='best',
    max_depth=None,
    min_samples_split=2,
    min_samples_leaf=1,
    min_weight_fraction_leaf=0.0,
    max_features=None,
    random_state=42,
    max_leaf_nodes=None,
    min_impurity_decrease=0.0,
    min_impurity_split=None,
    #class_weight="balanced",
    presort='deprecated',
    ccp_alpha=0.0,
)
clf.fit(x_train, y_train)

The problem becomes apparent at the visualization step when I plotted the tree, each node shows me class = Failure when Failure is the minority and vice versa. Further down the line plotting the confusion matrix and calculating all the performance metrics it also becomes apparent, that the labels were reversed and I cannot figure out as to why.

Any ideas where I might need to look for the answer? If more code is necessary to give a feedback I can provide.

Much appreciated.

$\endgroup$
2
  • $\begingroup$ Does the classification produce correct results, apart from the internal details you observe? $\endgroup$
    – Nikos M.
    May 4 at 15:28
  • $\begingroup$ Yes, the splits are correct, the values for those resulting nodes in each respective class are also correct. $\endgroup$
    – Godgory
    May 4 at 17:21
2
$\begingroup$

It might be because of the conflict of order between model.classes_ and series.unique()

For a binary labels,
model.classes_ = array([0, 1])
series.unique() = array([1, 0])

Try creating a constant value i.e. np.array[0,1] for labels and see.

$\endgroup$
1
  • 1
    $\begingroup$ Thanks, this solved my issue. Appears, that it all came down to series.unqiue() outputting the label values in the incorrect order. $\endgroup$
    – Godgory
    May 4 at 17:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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