1
$\begingroup$

I'm trying to plot decision boundary for Decision Tree classifier. Classifier is trained on training set, and decision boundary (contour) using random new observations and observations from test set is not same.

Here is the code:

def plot_decision_boundary(clf, X_train, y_train, X_test, y_test, contour=True):
    x1_min = np.round(X_train[:, 0], decimals=2).min()
    x1_max = np.round(X_train[:, 0], decimals=2).max()
    x2_min = np.round(X_train[:, 1], decimals=2).min()
    x2_max = np.round(X_train[:, 1], decimals=2).max()
    
    # Test set
    x1 = X_test[:100, 0]
    x2 = X_test[:100, 1]
    
    xx, yy = np.meshgrid(x1, x2)
    X_new = np.c_[xx.ravel(), yy.ravel()]
    y_pred = clf.predict(X_new)
    zz = y_pred.reshape(xx.shape)
    
    print(x1.shape)
    print(x2.shape)
    print(xx.shape)
    print(yy.shape)
    print(X_new.shape)
    print(zz.shape)
    print()
    
    # Random points
    x1_r = np.linspace(x1_min, x1_max, 100)
    x2_r = np.linspace(x2_min, x2_max, 100)
    xx_r, yy_r = np.meshgrid(x1_r, x2_r)
    X_new_r = np.c_[xx_r.ravel(), yy_r.ravel()]
    y_pred_r = clf.predict(X_new_r)
    zz_r = y_pred_r.reshape(xx_r.shape)
    
    print(x1_r.shape)
    print(x2_r.shape)
    print(xx_r.shape)
    print(yy_r.shape)
    print(X_new_r.shape)
    print(zz_r.shape)

    custom_cmap = ListedColormap(['#fafab0','#9898ff','#a0faa0'])
    # Plotting training points
    plt.plot(X_train[y_train==0, 0], X_train[y_train==0, 1], 'ro')
    plt.plot(X_train[y_train==1, 0], X_train[y_train==1, 1], 'bs')

    # Plotting decision boundary
    plt.contour(xx, yy, zz, alpha=0.8)

    plt.axis([x1_min, x1_max, x2_min, x2_max])
    plt.xlabel("$x_1$", fontsize=16)
    plt.ylabel("$x_2$", fontsize=16)
    plt.title("Decision Tree")

    plt.show()

Output: (not including contour)

(100,)
(100,)
(100, 100)
(100, 100)
(10000, 2)
(100, 100)

(100,)
(100,)
(100, 100)
(100, 100)
(10000, 2)
(100, 100)

Shapes of data (both random and test sets) used for predicting and plotting is same. Contour for random set is clean and as expected, but for test set, its zigzag. Is there any issue with plotting test set in this code?

$\endgroup$
1
  • $\begingroup$ Do you mean the Decision path Or Decision boundary and why random new records should match the test data decision path $\endgroup$
    – 10xAI
    Jul 6 '20 at 11:36

Your Answer

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

Browse other questions tagged or ask your own question.