机器学习算法:支持向量机(SVM)|python与r语言代码实现

2018-10-1123:58:19数据结构与算法Comments3,307 views字数 926阅读模式

支持向量机(SVM)

这是一种分类方法。在该算法中,我们将每个数据项绘制为n维空间中的一个点(其中n是具有的特征数),其中每个特征的值是特定坐标的值。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/suanfa/6544.html

例如,如果我们只有像个体的高度和毛发长度这样的两个特征,我们首先在二维空间中绘制这两个变量,其中每个点有两个坐标(这些坐标称为支持向量)文章源自菜鸟学院-https://www.cainiaoxueyuan.com/suanfa/6544.html

机器学习算法:支持向量机(SVM)|python与r语言代码实现文章源自菜鸟学院-https://www.cainiaoxueyuan.com/suanfa/6544.html

现在,我们将发现在两个不同分类的数据组之间分割数据的一些线。这将是一条直线,使得两组中最靠近的点的距离将最远。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/suanfa/6544.html

机器学习算法:支持向量机(SVM)|python与r语言代码实现文章源自菜鸟学院-https://www.cainiaoxueyuan.com/suanfa/6544.html

在上面所示的示例中,将数据分成两个不同分类组的线是黑线,因为最接近的两个点是离线最远的。这条线是我们的分类器。然后,取决于测试数据落在行的哪一边,这样可以知道要将新数据分成什么类。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/suanfa/6544.html

更多:支持向量机的简化版本文章源自菜鸟学院-https://www.cainiaoxueyuan.com/suanfa/6544.html

把这个算法看作是在n维空间中玩Jazzball游戏,游戏中的调整是:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/suanfa/6544.html

  • 你可以在任何角度(不只是水平或垂直,如在经典游戏)画线/平面
  • 游戏的目的是在不同的房间中分离不同颜色的球
  • 球不动

Python代码

#Import Library
from sklearn import svm
#Assumed you have, X (predictor) and Y (target) for training data set and x_test(predictor) of test_dataset
# Create SVM classification object 
model = svm.svc() # there is various option associated with it, this is simple for classification. You can refer link, for mo# re detail.
# Train the model using the training sets and check score
model.fit(X, y)
model.score(X, y)
#Predict Output
predicted= model.predict(x_test)

R语言代码文章源自菜鸟学院-https://www.cainiaoxueyuan.com/suanfa/6544.html

library(e1071)
x <- cbind(x_train,y_train)
# Fitting model
fit <-svm(y_train ~ ., data = x)
summary(fit)
#Predict Output 
predicted= predict(fit,x_test)
文章源自菜鸟学院-https://www.cainiaoxueyuan.com/suanfa/6544.html
  • 本站内容整理自互联网,仅提供信息存储空间服务,以方便学习之用。如对文章、图片、字体等版权有疑问,请在下方留言,管理员看到后,将第一时间进行处理。
  • 转载请务必保留本文链接:https://www.cainiaoxueyuan.com/suanfa/6544.html

Comment

匿名网友 填写信息

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定