Question: We can visualize decision tree with training set distribution, for example from matplotlib import pyplot as plt from sklearn import datasets from sklearn.tree import DecisionTreeClassifier from sklearn import tree # Prepare the data data, can do row sample and column sample here iris = datasets.load_iris() X = iris.data y = iris.target # Fit the classifier with default hyper-parameters clf = DecisionTreeClassifier(random_state=1234) clf.fit(X, y) fig = plt.figure(figsize=(25,20)) _ = tree.plot_tree(clf, feature_names=iris.feature_names, class_names=iris.target_names, filled=True)  gives us  with distribution of training set, for example value = [50, 50, 50] in the root node. However, I am not able to give it a test set, and get the distribution of test set in the visualized tree.

Answer: I don't think there is an sklearn method to do this (yet). Option 1: Changing the annotation plot of the tree by adding X_test information You can use the custom function below: def plot_tree_test(clf, tree_plot, X_test, y_test): n = len(tree_plot) cat = clf.n_classes_ # Getting the path for each item in X_test path = clf.decision_path(X_test).toarray().transpose() # Looping through each node/leaf in the tree and adding information from X_test path for i in range(n): value = [] for j in range(cat): value += [sum(y_test[path[i]==1]==j)] tree_plot[i].set_text(tree_plot[i].get_text()+f'\ntest samples = {path[i].sum()}\ntest value = {value}') return tree_plot  Then changing slightly the script: from matplotlib import pyplot as plt from sklearn import datasets from sklearn.tree import DecisionTreeClassifier from sklearn import tree from sklearn.model_selection import train_test_split # Prepare the data data, can do row sample and column sample here iris = datasets.load_iris() X = iris.data y = iris.target # Creating a train and test set X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, shuffle=True, random_state=1234) # Fit the classifier with default hyper-parameters clf = DecisionTreeClassifier(random_state=1234) clf.fit(X_train, y_train) fig = plt.figure(figsize=(25,20)) tree_plot = tree.plot_tree(clf, feature_names=iris.feature_names, class_names=iris.target_names, filled=True) tree_plot = plot_tree_test(clf, tree_plot, X_test, y_test) plt.show()  Output:  Option 2: Changing the classifier itself with X_test information You can use the custom function below: def tree_test(clf, X_test, y_test): state = clf.tree_.__getstate__() n = len(state['values']) cat = clf.n_classes_ # Getting the path for each item in X_test path = clf.decision_path(X_test).toarray().transpose() # Looping through each node/leaf in the tree and adding information from X_test path values = [] for i in range(n): value = [] for j in range(cat): value += [float(sum(y_test[path[i]==1]==j))] values += [[value]] state['nodes'][i][5] = path[i].sum() state['nodes'][i][6] = max(path[i].sum(), 0.1) # 0 returns error values = np.array(values) state['values'] = values clf.tree_.__setstate__(state) return clf  Then changing slightly the script: from matplotlib import pyplot as plt from sklearn import datasets from sklearn.tree import DecisionTreeClassifier from sklearn import tree from sklearn.model_selection import train_test_split import numpy as np # Prepare the data data, can do row sample and column sample here iris = datasets.load_iris() X = iris.data y = iris.target # Creating a train and test set X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, shuffle=True, random_state=1234) # Fit the classifier with default hyper-parameters clf = DecisionTreeClassifier(random_state=1234) clf.fit(X_train, y_train) clf = tree_test(clf, X_test, y_test) fig = plt.figure(figsize=(25,20)) tree_plot = tree.plot_tree(clf, feature_names=iris.feature_names, class_names=iris.target_names, filled=True) plt.show()  Output: