前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >【ML】支持向量机是什么?我为什么要使用它?

【ML】支持向量机是什么?我为什么要使用它?

作者头像
陆勤_数据人网
发布2018-12-27 15:24:47
1.9K0
发布2018-12-27 15:24:47
举报

笔者邀请您,先思考:

1 支持向量机是什么?如何理解?

支持向量机已经成为一种非常流行的算法。在本文中,我试图对其工作原理给出一个简单的解释,并给出几个使用Python scikit库的示例。

支持向量机是什么?

支持向量机是一种有监督的机器学习算法,可以用于分类或回归问题。它使用一种称为核技巧的技术来转换数据,然后根据这些转换在可能的输出之间找到一个最佳边界。简单地说,它做一些非常复杂的数据转换,然后根据定义的标签或输出来划分数据

那么是什么让它如此伟大呢?

支持向量机既能进行分类又能进行回归。在本文中,我将重点介绍如何使用SVM进行分类。我将特别关注非线性支持向量机,或者说是使用非线性核的支持向量机。非线性支持向量机意味着算法计算的边界不一定是直线。好处是您可以捕获数据点之间更复杂的关系,而不必自己做困难的转换。缺点是训练时间更长,因为它需要更多的计算。

那么核技巧是什么?

核技巧对你获得的数据进行转换。有一些很好的特性,你认为可以用来做一个很好的分类器,然后出来一些你不再认识的数据。这有点像解开一条DNA链。你从这个看起来很难看的数据向量开始,在通过核技巧之后,它会被解开并自我复合,直到它现在是一个更大的数据集,通过查看电子表格无法理解。 但是这里有魔力,在扩展数据集时,你的类之间现在有更明显的界限,SVM算法能够计算出更加优化的超平面。

接下来,假设你是一个农民,你有一个问题 - 你需要设置一个围栏,以保护你的奶牛免受狼的攻击。 但是你在哪里建造篱笆? 好吧,如果你是一个真正的数据驱动农民,你可以做的一件事就是建立一个基于你牧场中奶牛和狼的位置的分类器。 通过几种不同类型的分类器,我们看到SVM在从狼群中分离你的奶牛方面做得很好。 我认为这些图也很好地说明了使用非线性分类器的好处。 您可以看到逻辑和决策树模型都只使用直线。

想要重新创建分析

想为自己创造这些图吗?您可以在您的终端或您选择的IDE中运行代码,但是,令人惊讶的是,我推荐Rodeo。它有一个非常棒的弹出式图特性,对于这种类型的分析非常有用。它还附带了已经包含在Windows机器中的Python。

下载Rodeo之后,需要保存来自我的github的原始cows_and_wolves.txt文件。确保将工作目录设置为保存文件的位置。

好的,现在只需将下面的代码复制粘贴到Rodeo中,并按行或整个脚本运行它。不要忘记,您可以弹出您的plot选项卡,在窗口中移动,或者调整它们的大小。

代码语言:javascript
复制
 1# Data driven farmer goes to the Rodeo
 2
 3import numpy as np
 4import pylab as pl
 5from sklearn import svm
 6from sklearn import linear_model
 7from sklearn import tree
 8import pandas as pd
 9
10
11def plot_results_with_hyperplane(clf, clf_name, df, plt_nmbr):
12    x_min, x_max = df.x.min() - .5, df.x.max() + .5
13    y_min, y_max = df.y.min() - .5, df.y.max() + .5
14
15    # step between points. i.e. [0, 0.02, 0.04, ...]
16    step = .02
17    # to plot the boundary, we're going to create a matrix of every possible point
18    # then label each point as a wolf or cow using our classifier
19    xx, yy = np.meshgrid(np.arange(x_min, x_max, step), np.arange(y_min, y_max, step))
20    Z = clf.predict(np.c_[xx.ravel(), yy.ravel()])
21    # this gets our predictions back into a matrix
22    Z = Z.reshape(xx.shape)
23
24    # create a subplot (we're going to have more than 1 plot on a given image)
25    pl.subplot(2, 2, plt_nmbr)
26    # plot the boundaries
27    pl.pcolormesh(xx, yy, Z, cmap=pl.cm.Paired)
28
29    # plot the wolves and cows
30    for animal in df.animal.unique():
31        pl.scatter(df[df.animal==animal].x,
32                   df[df.animal==animal].y,
33                   marker=animal,
34                   label="cows" if animal=="x" else "wolves",
35                   color='black')
36    pl.title(clf_name)
37    pl.legend(loc="best")
38
39
40data = open("cows_and_wolves.txt").read()
41data = [row.split('\t') for row in data.strip().split('\n')]
42
43animals = []
44for y, row in enumerate(data):
45    for x, item in enumerate(row):
46        # x's are cows, o's are wolves
47        if item in ['o', 'x']:
48            animals.append([x, y, item])
49
50df = pd.DataFrame(animals, columns=["x", "y", "animal"])
51df['animal_type'] = df.animal.apply(lambda x: 0 if x=="x" else 1)
52
53# train using the x and y position coordiantes
54train_cols = ["x", "y"]
55
56clfs = {
57    "SVM": svm.SVC(),
58    "Logistic" : linear_model.LogisticRegression(),
59    "Decision Tree": tree.DecisionTreeClassifier(),
60}
61
62plt_nmbr = 1
63for clf_name, clf in clfs.iteritems():
64    clf.fit(df[train_cols], df.animal_type)
65    plot_results_with_hyperplane(clf, clf_name, df, plt_nmbr)
66    plt_nmbr += 1
67pl.show()

让支持向量机做这个棘手的工作

如果因变量和自变量之间的关系是非线性的,它就不会像SVM那样准确。 在变量(log(x),(x ^ 2))之间进行转换变得不那么重要了,因为它将在算法中加以考虑。 如果你仍然遇到麻烦,看看你是否可以按照这个例子。

假设我们有一个由绿点和红点组成的数据集。 当用它们的坐标绘制时,这些点形成一个带有绿色轮廓的红色圆圈(看起来很像孟加拉国的旗帜)。

如果以某种方式我们丢失了1/3的数据会发生什么。 如果我们无法恢复它并且我们想找到一种方法来估计缺少的1/3看起来是什么样的。

那么我们如何找出丢失的1/3看起来像什么? 一种方法可能是使用我们拥有的80%数据作为训练集来构建模型。 但是我们使用什么类型的模型? 我们试试以下内容:

  • 逻辑模型
  • 决策树
  • SVM

我对每个模型进行了训练,然后使用每个模型对缺失的1/3数据进行预测。 让我们来看看我们预测的形状是什么样的……

这是用于比较逻辑模型,决策树和SVM的代码。

代码语言:javascript
复制
 1import numpy as np
 2import pylab as pl
 3import pandas as pd
 4
 5from sklearn import svm
 6from sklearn import linear_model
 7from sklearn import tree
 8
 9from sklearn.metrics import confusion_matrix
10
11x_min, x_max = 0, 15
12y_min, y_max = 0, 10
13step = .1
14# to plot the boundary, we're going to create a matrix of every possible point
15# then label each point as a wolf or cow using our classifier
16xx, yy = np.meshgrid(np.arange(x_min, x_max, step), np.arange(y_min, y_max, step))
17
18df = pd.DataFrame(data={'x': xx.ravel(), 'y': yy.ravel()})
19
20df['color_gauge'] = (df.x-7.5)**2 + (df.y-5)**2
21df['color'] = df.color_gauge.apply(lambda x: "red" if x <= 15 else "green")
22df['color_as_int'] = df.color.apply(lambda x: 0 if x=="red" else 1)
23
24print "Points on flag:"
25print df.groupby('color').size()
26print
27
28figure = 1
29
30# plot a figure for the entire dataset
31for color in df.color.unique():
32    idx = df.color==color
33    pl.subplot(2, 2, figure)
34    pl.scatter(df[idx].x, df[idx].y, color=color)
35    pl.title('Actual')
36
37
38train_idx = df.x < 10
39
40train = df[train_idx]
41test = df[-train_idx]
42
43
44print "Training Set Size: %d" % len(train)
45print "Test Set Size: %d" % len(test)
46
47# train using the x and y position coordiantes
48cols = ["x", "y"]
49
50clfs = {
51    "SVM": svm.SVC(degree=0.5),
52    "Logistic" : linear_model.LogisticRegression(),
53    "Decision Tree": tree.DecisionTreeClassifier()
54}
55
56
57# racehorse different classifiers and plot the results
58for clf_name, clf in clfs.iteritems():
59    figure += 1
60
61    # train the classifier
62    clf.fit(train[cols], train.color_as_int)
63
64    # get the predicted values from the test set
65    test['predicted_color_as_int'] = clf.predict(test[cols])
66    test['pred_color'] = test.predicted_color_as_int.apply(lambda x: "red" if x==0 else "green")
67
68    # create a new subplot on the plot
69    pl.subplot(2, 2, figure)
70    # plot each predicted color
71    for color in test.pred_color.unique():
72        # plot only rows where pred_color is equal to color
73        idx = test.pred_color==color
74        pl.scatter(test[idx].x, test[idx].y, color=color)
75
76    # plot the training set as well
77    for color in train.color.unique():
78        idx = train.color==color
79        pl.scatter(train[idx].x, train[idx].y, color=color)
80
81    # add a dotted line to show the boundary between the training and test set
82    # (everything to the right of the line is in the test set)
83    #this plots a vertical line
84    train_line_y = np.linspace(y_min, y_max) #evenly spaced array from 0 to 10
85    train_line_x = np.repeat(10, len(train_line_y)) #repeat 10 (threshold for traininset) n times
86    # add a black, dotted line to the subplot
87    pl.plot(train_line_x, train_line_y, 'k--', color="black")
88
89    pl.title(clf_name)
90
91    print "Confusion Matrix for %s:" % clf_name
92    print confusion_matrix(test.color, test.pred_color)
93pl.show()

结果

从图中可以看出,SVM是赢家,为什么? 那么如果你看一下决策树和GLM模型的预测形状,你会注意到什么? 直线边界。 我们的输入模型不包括任何转换来解释x,y和颜色之间的非线性关系。 鉴于一系列特定的转换,我们绝对可以使GLM和DT表现更好,但为什么要浪费时间? 由于没有复杂的变换或缩放,SVM只错误分类了117/5000点(准确率为98%而DT-51%和GLM-12%!),所有错误分类的点都是红色 - 因此略有凸起。

何时不使用它

那么为什么不将SVM用于一切呢? 不幸的是,SVM的神奇之处也是最大的缺点。 复杂的数据变换和产生的边界平面很难解释。 这就是为什么它通常被称为黑匣子。 相反,GLM和决策树恰恰相反。 很容易理解DT和GLM究竟是什么和为什么会以牺牲性能为代价的。

更多资源

想进一步了解SVM? 这是我遇到的一些好资源:

  • SVM新手教程:麻省理工学院的Zoya Gavrilov教授的一些基础知识
  • 初学者SVM算法的工作原理:ThalesSehnKörting的视频
  • 来自纽约大学和范德比尔特生物医学的中级支持向量机介绍

原文链接: https://www.kdnuggets.com/2017/02/yhat-support-vector-machine.html

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2018-12-13,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 数据科学与人工智能 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 支持向量机是什么?
  • 那么是什么让它如此伟大呢?
  • 那么核技巧是什么?
  • 想要重新创建分析
  • 让支持向量机做这个棘手的工作
  • 结果
  • 何时不使用它
  • 更多资源
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档