python svm

来源:互联网 发布:php httpclient 编辑:程序博客网 时间:2024/06/11 13:18

原文:http://www.cnblogs.com/harvey888/p/5852687.html

一:我对SVM的理解

先介绍一些简单的基本概念

分隔超平面:将数据集分割开来的直线叫做分隔超平面。

超平面:如果数据集是N维的,那么就需要N-1维的某对象来对数据进行分割。该对象叫做超平面,也就是分类的决策边界。

间隔

一个点到分割面的距离,称为点相对于分割面的距离。

数据集中所有的点到分割面的最小间隔的2倍,称为分类器或数据集的间隔。

最大间隔:SVM分类器是要找最大的数据集间隔。

支持向量:坐落在数据边际的两边超平面上的点被称为支持向量



'''SVC参数解释(1)C: 目标函数的惩罚系数C,用来平衡分类间隔margin和错分样本的,default C = 1.0;(2)kernel:参数选择有RBF, Linear, Poly, Sigmoid, 默认的是"RBF";(3)degree:if you choose 'Poly' in param 2, this is effective, degree决定了多项式的最高次幂;(4)gamma:核函数的系数('Poly', 'RBF' and 'Sigmoid'), 默认是gamma = 1 / n_features;(5)coef0:核函数中的独立项,'RBF' and 'Poly'有效;(6)probablity: 可能性估计是否使用(true or false);(7)shrinking:是否进行启发式;(8)tol(default = 1e - 3): svm结束标准的精度;(9)cache_size: 制定训练所需要的内存(以MB为单位);(10)class_weight: 每个类所占据的权重,不同的类设置不同的惩罚参数C, 缺省的话自适应;(11)verbose: 跟多线程有关,不大明白啥意思具体;(12)max_iter: 最大迭代次数,default = 1, if max_iter = -1, no limited;(13)decision_function_shape : ‘ovo’ 一对一, ‘ovr’ 多对多  or None 无, default=None(14)random_state :用于概率估计的数据重排时的伪随机数生成器的种子。 ps:7,8,9一般不考虑。'''#分类:from sklearn.svm import SVCimport numpy as npX = np.array([[-1, -1], [-2, -1], [1, 1], [2, 1]])y = np.array([1, 1, 2, 2])clf = SVC()clf.fit(X, y)print(clf.fit(X, y))print(clf.predict([[-0.8, -1]]))#回归from sklearn import svmX = [[0, 0], [2, 2]]y = [0.5, 2.5]clf = svm.SVR()clf.fit(X, y)svm.SVR(C=1.0, cache_size=200, coef0=0.0, degree=3, epsilon=0.1, gamma='auto',    kernel='rbf', max_iter=-1, shrinking=True, tol=0.001, verbose=False)print(clf.predict([[1, 1]]))# -*-coding:utf-8-*-import numpy as npfrom sklearn.svm import SVRimport matplotlib.pyplot as plt################################################################################ Generate sample dataX = np.sort(5 * np.random.rand(40, 1), axis=0)  # 产生40组数据,每组一个数据,axis=0决定按列排列,=1表示行排列y = np.sin(X).ravel()  # np.sin()输出的是列,和X对应,ravel表示转换成行################################################################################ Add noise to targetsy[::5] += 3 * (0.5 - np.random.rand(8))################################################################################ Fit regression modelsvr_rbf = SVR(kernel='rbf', C=1e3, gamma=0.1)svr_lin = SVR(kernel='linear', C=1e3)svr_poly = SVR(kernel='poly', C=1e3, degree=2)y_rbf = svr_rbf.fit(X, y).predict(X)y_lin = svr_lin.fit(X, y).predict(X)y_poly = svr_poly.fit(X, y).predict(X)################################################################################ look at the resultslw = 2plt.scatter(X, y, color='darkorange', label='data')plt.hold('on')plt.plot(X, y_rbf, color='navy', lw=lw, label='RBF model')plt.plot(X, y_lin, color='c', lw=lw, label='Linear model')plt.plot(X, y_poly, color='cornflowerblue', lw=lw, label='Polynomial model')plt.xlabel('data')plt.ylabel('target')plt.title('Support Vector Regression')plt.legend()plt.show()

原创粉丝点击