Python - how to visualise a decision boundary for a LogisticRegression?

Viewed 261

I'm trying to visualise a decision boundary for a LogisticRegression() classifier.

But getting a ValueError: X has 2 features per sample; expecting 24 when calling plot_boundary(LogisticRegression(), X, y, "Log Reg")

I've checked the X.shape and it is (14635, 24) What could be wrong with my function?

def plot_boundary(clf, X, y, plot_title): 
    xx, yy = np.meshgrid(np.linspace(-3, 3, 50), np.linspace(-3, 3, 50)) 
    clf.fit(X, y) 
    # plot the decision function for each datapoint on the grid 
    Z = clf.predict_proba(np.vstack((xx.ravel(), yy.ravel())).T)[:, 1] 
    Z = Z.reshape(xx.shape) 

    image = plt.imshow(Z, interpolation='nearest', extent=(xx.min(), xx.max(),
                       yy.min(), yy.max()), aspect='auto', origin='lower', 
                       cmap=plt.cm.PuOr_r) 
    contours = plt.contour(xx, yy, Z, levels=[0], linewidths=2, linetypes='--') 
    plt.scatter(X[:, 0], X[:, 1], s=30, c=y, cmap=plt.cm.Paired) 
    plt.xticks(()) 
    plt.yticks(()) 
    plt.xlabel(r'$x_1$') 
    plt.ylabel(r'$x_2$') 
    plt.axis([-3, 3, -3, 3]) 
    plt.colorbar(image) 
    plt.title(plot_title, fontsize=12)
0 Answers
Related