sklearn 下的 SVM 及其参数

1. 作为对比基线的 SVM

工业界的 SVM,以及 Logistic Regression,因其相较深度神经网络更为轻量级,且性能也不会差别很大的缘故,一直是模型选择的首选。而在学术界,深度学习方兴未艾的今天,SVM 只能作为一个基准模型使用,作为其他模型的陪衬。

我们就以一个手写字符识别(mnist)的例子说明,如何使用 SVM :

import pickle, gzip
from sklearn import svm 

def load_data():
	with gzip.open('./mnist.pkl.gz') as fp:
		training_data, valid_data, test_data = pickle.load(fp)
	return training_data, valid_data, test_data

def svm_baseline():
	training_data, valid_data, test_data = load_data()
	clf = svm.SVC()
	clf.fit(training_data[0], training_data[1])
	predications = clf.predict(test_data[0])
	num_correct = sum(np.where(predications == test_data[1], 1, 0))
	print 'Baseline classifier using svm.'
	print '%s of %s correct' % (num_correct, len(test_data[0]))

if __name__ == '__main__':
	svm_baseline()

2. sklearn 下 SVM 的参数

http://sofasofa.io/forum_main_post.php?postid=1001951

  • class sklearn.svm.SVC(C=1.0, kernel=’rbf’, degree=3, gamma=’auto’)

    gamma 与 kernel 的 rbf(径向基函数)相对应,径向基函数:

    K ( x − x ′ ) = exp ⁡ ( − ∥ x − x ′ ∥ 2 2 σ 2 ) K(\mathbf x-\mathbf x')=\exp\left(-\frac{\|\mathbf x-\mathbf x'\|^2}{2\sigma^2}\right) K(xx)=exp(2σ2xx2)

    而构造函数参数中的 gamma其实就是:

    gamma = 1 2 σ 2 \text{gamma}=\frac{1}{2\sigma^2} gamma=2σ21

    也即:
    K ( x − x ′ ) = exp ⁡ ( − γ ∥ x − x ′ ∥ 2 ) K(\mathbf x-\mathbf x')=\exp\left(-\gamma{\|\mathbf x-\mathbf x'\|^2}\right) K(xx)=exp(γxx2)

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

五道口纳什

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值