人工智能/模式识别/机器学习精华专题大数据,机器学习,人工智能人工智能与机器学习

SVM: Support Vector Machine | 支持

2018-09-12  本文已影响4人  冰源
《hands on machine learning with scikit-learn and tensorflow》

支持向量机的作用

在两个类之间找到最宽的一条大道将其分开
SVM适用于复杂、中小规模的数据集上。

from sklearn.svm import LinearSVC,SVC #LinearSVC(C=1, loss="hinge") 比 SVC(kernel="linear", C=1)快

支持向量的含义

支持向量就是street(两条虚线之间,包含虚线)内的数据点,这些点决定了street的宽度。

数据的标准化与中心化

SVMs try to fit the largest possible “street” between the classes (see the first answer), so if the training set is not scaled, the SVM will tend to neglect small features
数据的标准化与中心化使得各特征重要性相同,不至于忽略某些特征

软间隔与硬间隔

硬间隔:street上不能有数据点(因此对异常值非常敏感)
软间隔:street上可以有些许数据点

sklearn的svm模型(from sklearn.svm import *)通过 超参 C 来调控对margin violations(street上的点)的严格程度,C越大则控制越严格,street宽度也会越小

Fewer margin violations versus large margin

支持向量回归

与分类相反,希望数据点都在street内部

背后逻辑(简化版)

增加特征,使得维度变高,从而可以找到满足线性可分的平面 f(x) = Wx +b
接下来关键就是求出W,b,并控制好street的宽度


提问:什么样的W,b是最好的?
回答:W越小,间隔越大(如下图解释,w越小黑线越长)

W与间隔大小的关系

我们定义负样本的标签 t=-1,正样本 t=1,那么硬间隔求解W的方法如下:

硬间隔

为什么是>=1?这个和损失函数hinge有关。

软间隔

软间隔加了一个宽松度ζ,用参数C进行了控制。

看起来好像不错,求出w,b就能完成分类了。但是映射可能有问题,譬如可能特征维度爆炸。RBF高斯径向基函数就能通过泰勒展开将 x 映射到(x, x^2, x^3 ... x^n)无穷维度上去。

f(Φ(x)) = WΦ(x)+b,Φ(x)要是无穷维的话 f(Φ(x)) 没法算,所以就算求出W,b了也不行,这就要想办法解决这个高维乃至无穷维的问题了。

上面求W,b本来要用一个二次规划就行,然后二次规划求解问题可以用一个对偶问题求出一样的解。至于这个对偶问题咋找到的,我就不知道了。如下图:

约束条件 约束条件下的W,b解

这样也能解出W,b,还有新出现的α,利用这些接触的信息就能跳过Φ(x)而直接利用x求解了。

利用核技巧求解

可以看到上图最后一行公式,只涉及α,t,b,K(x1,x2)。

据说(我没算过),只有support vector的α≠0
我看这个公式:不需要先根据训练集求W,b,而是找到support vector,然后计算上面的公式就能知道h(Φ(x))是正是负了。这还是挺神奇的,没有映射不升到高维也能分类。‘’

不升到高维却能得到跟高维一样的结果

分类用的实线(面)我们是找到了,但是间隔应该多宽我们还没调整好。

间隔应该多宽,考察的是我们对于那些边界点怎么算。
严格划分每个点?放过多少点可以留着间隔内?

这个就要用损失函数Hinge Loss来衡量了,max(0, 1-t*f(x))

Hinge Loss

提问:拐点必须是1?
回答:不是,可以其他。为啥?不清楚。
提问:Hinge Loss有啥好处
回答:容易求导

完整损失函数

如果将max(0, 1-t*f(x))记为ε,那么 在最小化的目标 下,下面第二个框中两者等价。

将max(0, 1-t*f(x))记为ε

于是乎,我就能理解上面的软间隔优化目标了。

软间隔

代码:SVM应用

# https://github.com/ageron/handson-ml/blob/master/05_support_vector_machines.ipynb
# 在加利福尼亚住宅(California housing)数据集上训练一个 SVM 回归模型
# Let's load the dataset using Scikit-Learn's fetch_california_housing() function:

from sklearn.datasets import fetch_california_housing
housing = fetch_california_housing()
X = housing["data"]
y = housing["target"]

# Split it into a training set and a test set:
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

# Don't forget to scale the data:
from sklearn.preprocessing import StandardScaler
scaler = StandardScaler()
X_train_scaled = scaler.fit_transform(X_train)
X_test_scaled = scaler.transform(X_test)

# Let's train a simple LinearSVR first:
from sklearn.svm import LinearSVR
lin_svr = LinearSVR(random_state=42)
lin_svr.fit(X_train_scaled, y_train)
# LinearSVR(C=1.0, dual=True, epsilon=0.0, fit_intercept=True,
#     intercept_scaling=1.0, loss='epsilon_insensitive', max_iter=1000,
#     random_state=42, tol=0.0001, verbose=0)

# Let's see how it performs on the training set:
from sklearn.metrics import mean_squared_error
y_pred = lin_svr.predict(X_train_scaled)
mse = mean_squared_error(y_train, y_pred)
# mse = 0.949968822217229

from sklearn.svm import SVR
from sklearn.model_selection import RandomizedSearchCV
from scipy.stats import reciprocal, uniform

param_distributions = {"gamma": reciprocal(0.001, 0.1), "C": uniform(1, 10)}
rnd_search_cv = RandomizedSearchCV(SVR(), param_distributions, n_iter=10, verbose=2, random_state=42)
rnd_search_cv.fit(X_train_scaled, y_train)

#>>> rnd_search_cv.best_estimator_
#>>> SVR(C=4.745401188473625, cache_size=200, coef0=0.0, degree=3, epsilon=0.1,
#  gamma=0.07969454818643928, kernel='rbf', max_iter=-1, shrinking=True,
#  tol=0.001, verbose=False)

y_pred = rnd_search_cv.best_estimator_.predict(X_train_scaled)
mse = mean_squared_error(y_train, y_pred)

y_pred = rnd_search_cv.best_estimator_.predict(X_test_scaled)
mse = mean_squared_error(y_test, y_pred)
上一篇下一篇

猜你喜欢

热点阅读