首页 > 解决方案 > 大边距分类器

问题描述

我正在构建一个分类器以最大化正标记点和负标记点之间的边距。我正在使用 sklearn.LinearSVC 来执行此操作。我必须找到权重(向量,theta)和截距(标量 theta_0)。我还需要计算最大边距。所以,我写了下面的代码。

import numpy as np
import sklearn 
from sklearn.svm import LinearSVC

# training data
X_train = np.array([[0,0],[2,0],[3,0],[0,2],[2,2],[5,1],[5,2],[2,4],[4,4],[5,5]])
y_train = [-1,-1,-1,-1,-1,1,1,1,1,1]


classifier = LinearSVC(random_state = 0, C=1.0, fit_intercept= True)
classifier.fit(X_train, y_train)

theta = classifier.coef_
theta_0.intercept_
norm = np.linalg.norm(theta)
margin = 2/norm

据我了解,LinearSVC 是正确的包;尽管我看到一些教程中人们使用 SVC,然后使用 kernel = 'linear'。我不确定是否应该将 fit_intercept 参数设置为 True。当我将其默认为 False 时,我得到了不同的 theta 和 theta_0 值。

有人可以指导我理解这个参数以及保证金计算是否正确吗?最后,LinearSVC 是否是正确的模型。谢谢。

标签: pythonsvm

解决方案


这种说法是错误的:

theta_0.intercept_

我认为它应该是:

theta_0 = classifier.intercept_

推荐阅读