# scikit learn target variable reversed (DecisionTreeClassifier)

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.

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

It might be because of the conflict of order between model.classes_ and series.unique()
model.classes_ = array([0, 1])
series.unique() = array([1, 0])
• Thanks, this solved my issue. Appears, that it all came down to series.unqiue() outputting the label values in the incorrect order. May 4 at 17:49