首页 文章

使用scikit-learn线性SVM提取决策边界

提问于
浏览
3

我有一个非常简单的1D分类问题:值列表[0,0.5,2]及其关联的类[0,1,2] . 我想获得这些类之间的分类界限 .

调整iris example(用于可视化目的),摆脱非线性模型:

X = np.array([[x, 1] for x in [0, 0.5, 2]]) 
Y = np.array([1, 0, 2])

C = 1.0  # SVM regularization parameter
svc = svm.SVC(kernel='linear', C=C).fit(X, Y)
lin_svc = svm.LinearSVC(C=C).fit(X, Y)

给出以下结果:
enter image description here

LinearSVC正在返回垃圾(为什么?),但带有线性内核的SVC工作正常 . 所以我想得到边界值,你可以用图形猜测:~0.25和~1.25 .

那个's where I' m输了: svc.coef_ 返回

array([[ 0.5       ,  0.        ],
       [-1.33333333,  0.        ],
       [-1.        ,  0.        ]])

svc.intercept_ 返回 array([-0.125 , 1.66666667, 1. ]) . 这不明确 .

我一定是在傻傻丢失,如何获得这些 Value 观?它们似乎很容易计算,迭代x轴找到边界会很荒谬......

3 回答

  • 0

    我有同样的问题,最终在sklearn documentation找到了解决方案 .

    给定权重 W=svc.coef_[0] 和截距 I=svc.intercept_ ,决策边界就是线

    y = a*x - b
    

    a = -W[0]/W[1]
    b = I[0]/W[1]
    
  • 2

    Exact boundary calculated from coef_ and intercept_


    我认为这是一个很好的问题,并且无法在文档中的任何位置找到它的一般答案 . 这个网站真的需要Latex,但无论如何,我会尽力而为......

    通常,超平面由其单位法线和与原点的偏移来定义 . 所以我们希望找到一些形式的决策函数: x dot n + d > 0 (其中 > 当然可以用 >= 替换) .

    SVM Margins Example的情况下,我们可以操纵他们开始的等式来阐明其概念意义 . 首先,让我们 Build 编写 coef 以表示 coef_[0]intercept 来表示 intercept_[0] 的符号方便性,因为这些数组只有1个值 . 然后一些简单的替换产生等式:

    y + coef[0]*x/coef[1] + intercept/coef[1] = 0
    

    乘以 coef[1] ,我们得到

    coef[1]*y + coef[0]*x + intercept = 0
    

    所以我们看到系数和截距函数大致就像它们的名字所暗示的那样 . 应用一种快速的符号概括应该明确答案 - 我们将用单个向量 x 替换 xy .

    coef[0]*x[0] + coef[1]*x[1] + intercept = 0
    

    通常,svm分类器的coef_和intercept_成员将具有与其训练的数据集匹配的维度,因此我们可以将该等式推断为任意维度的数据 . 为了避免让任何人误入歧途,这里是使用svm中原始变量名的最终 generalized decision boundary

    coef_[0][0]*x[0] + coef_[0][1]*x[1] + coef_[0][2]*x[2] + ... + coef_[0][n-1]*x[n-1] + intercept_[0] = 0
    

    其中数据的维度是 n .

    或者更简洁:

    sum(coef_[0][i]*x[i]) + intercept_[0] = 0
    

    其中 i 在输入数据的维度范围内求和 .

  • 6

    从SVM获取决策线,演示1

    import numpy as np
    import matplotlib.pyplot as plt
    from sklearn import svm
    from sklearn.datasets import make_blobs
    # we create 40 separable points
    X, y = make_blobs(n_samples=40, centers=2, random_state=6)
    # fit the model, don't regularize for illustration purposes
    clf = svm.SVC(kernel='linear', C=1000)
    clf.fit(X, y)
    plt.scatter(X[:, 0], X[:, 1], c=y, s=30, cmap=plt.cm.Paired)
    # plot the decision function
    ax = plt.gca()
    xlim = ax.get_xlim()
    ylim = ax.get_ylim()
    # create grid to evaluate model
    xx = np.linspace(xlim[0], xlim[1], 30)
    yy = np.linspace(ylim[0], ylim[1], 30)
    YY, XX = np.meshgrid(yy, xx)
    xy = np.vstack([XX.ravel(), YY.ravel()]).T
    Z = clf.decision_function(xy).reshape(XX.shape)
    # plot decision boundary and margins
    ax.contour(XX, YY, Z, colors='k', levels=[-1, 0, 1], alpha=0.5,
               linestyles=['--', '-', '--'])
    # plot support vectors
    ax.scatter(clf.support_vectors_[:, 0], clf.support_vectors_[:, 1], s=100,
               linewidth=1, facecolors='none')
    plt.show()
    

    Prints:

    enter image description here

    近似SVM的分离n-1维超平面,演示2

    import numpy as np
    import mlpy
    from sklearn import svm
    from sklearn.svm import SVC
    import matplotlib.pyplot as plt
    np.random.seed(0)
    mean1, cov1, n1 = [1, 5], [[1,1],[1,2]], 200  # 200 samples of class 1
    x1 = np.random.multivariate_normal(mean1, cov1, n1)
    y1 = np.ones(n1, dtype=np.int)
    
    mean2, cov2, n2 = [2.5, 2.5], [[1,0],[0,1]], 300 # 300 samples of class -1
    x2 = np.random.multivariate_normal(mean2, cov2, n2)
    y2 = 0 * np.ones(n2, dtype=np.int)
    X = np.concatenate((x1, x2), axis=0) # concatenate the 1 and -1 samples
    y = np.concatenate((y1, y2))
    clf = svm.SVC()
    #fit the hyperplane between the clouds of data, should be fast as hell
    clf.fit(X, y)
    SVC(C=1.0, cache_size=200, class_weight=None, coef0=0.0, 
        decision_function_shape='ovr', degree=3, gamma='auto', kernel='rbf',
        max_iter=-1, probability=False, random_state=None, shrinking=True,
        tol=0.001, verbose=False)
    
    production_point = [1., 2.5]
    
    answer = clf.predict([production_point])
    print("Answer: " + str(answer))
    plt.plot(x1[:,0], x1[:,1], 'ob', x2[:,0], x2[:,1], 'or', markersize = 5)
    colormap = ['r', 'b']
    color = colormap[answer[0]]
    plt.plot(production_point[0], production_point[1], 'o' + str(color), markersize=20)
    
    #I want to draw the decision lines
    ax = plt.gca()
    xlim = ax.get_xlim()
    ylim = ax.get_ylim()
    xx = np.linspace(xlim[0], xlim[1], 30)
    yy = np.linspace(ylim[0], ylim[1], 30)
    YY, XX = np.meshgrid(yy, xx)
    xy = np.vstack([XX.ravel(), YY.ravel()]).T
    Z = clf.decision_function(xy).reshape(XX.shape)
    ax.contour(XX, YY, Z, colors='k', levels=[-1, 0, 1], alpha=0.5,
               linestyles=['--', '-', '--'])
    plt.show()
    

    Prints:

    enter image description here

    这些超平面都像箭一样直,它们只能被限制在三维空间中的凡人所理解 . 这些超平面通过创造性的内核功能投射到更高的尺寸,而不是平坦回到可见尺寸以便您的观看乐趣 . 这是一个视频试图传达一些关于演示2中发生的事情的直觉:https://www.youtube.com/watch?v=3liCbRZPrZA

相关问题