Plot hyperplane Linear SVM python

前端 未结 1 736
心在旅途
心在旅途 2021-02-10 05:06

I am trying to plot the hyperplane for the model I trained with LinearSVC and sklearn. Note that I am working with natural languages; before fitting the model I extracted featur

1条回答
  •  情话喂你
    2021-02-10 05:17

    What about leaving the support_ out, which is not defined for a LinearSVC?

    import numpy as np
    import matplotlib.pyplot as plt
    from sklearn import svm
    
    np.random.seed(0)
    X = np.r_[np.random.randn(20, 2) - [2, 2], np.random.randn(20, 2) + [2, 2]]
    Y = [0] * 20 + [1] * 20
    
    fig, ax = plt.subplots()
    clf2 = svm.LinearSVC(C=1).fit(X, Y)
    
    # get the separating hyperplane
    w = clf2.coef_[0]
    a = -w[0] / w[1]
    xx = np.linspace(-5, 5)
    yy = a * xx - (clf2.intercept_[0]) / w[1]
    
    # create a mesh to plot in
    x_min, x_max = X[:, 0].min() - 1, X[:, 0].max() + 1
    y_min, y_max = X[:, 1].min() - 1, X[:, 1].max() + 1
    xx2, yy2 = np.meshgrid(np.arange(x_min, x_max, .2),
                         np.arange(y_min, y_max, .2))
    Z = clf2.predict(np.c_[xx2.ravel(), yy2.ravel()])
    
    Z = Z.reshape(xx2.shape)
    ax.contourf(xx2, yy2, Z, cmap=plt.cm.coolwarm, alpha=0.3)
    ax.scatter(X[:, 0], X[:, 1], c=Y, cmap=plt.cm.coolwarm, s=25)
    ax.plot(xx,yy)
    
    ax.axis([x_min, x_max,y_min, y_max])
    plt.show()
    

    0 讨论(0)
提交回复
热议问题