python - 大边距分类器
问题描述
我正在构建一个分类器以最大化正标记点和负标记点之间的边距。我正在使用 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 是否是正确的模型。谢谢。
解决方案
这种说法是错误的:
theta_0.intercept_
我认为它应该是:
theta_0 = classifier.intercept_
推荐阅读
- animation - Flutter 通过向下滑动缩小卡片堆栈的效果
- php - 如何使用 codeigniter 发送重置密码链接?
- python - Python - 从 json 解码图像
- php - 从变量开始反向递增列
- ios - 奇怪的消息:xcodebuild[14933:1513031] DVTPortal: Service - on tns run ios
- java - 无法访问 API-29 中的外部存储
- c# - Razor 助手不想在助手中使用 int 数组
- node.js - 如何在 express-handlebars 节点 js 中引用空格和特殊字符
- sql - 转换为双引号类型的不一致
- node.js - Node.js Express 不会删除安全 cookie