支持向量机(SVM)算法应用

第一个简单的小例子:

# -*-coding:utf-8 -*-

from sklearn import svm

x = [[2, 0], [1, 1], [2, 3]]
y = [0, 0, 1]
clf = svm.SVC(kernel = 'linear')
# kernel :核函数,默认是rbf,可以是‘linear’, ‘poly’, ‘rbf’, #‘sigmoid’, ‘precomputed’ 

clf.fit(x, y)

print clf

# get support vectors
print clf.support_vectors_# 得到支持向量
# get indices of support vectors
print clf.support_ #得到支持向量的索引,[1 2]这里的1指的是[1, 1]的索引,2指的是[2,3]的索引
# get number of support vectors for each class
print clf.n_support_  #得到每一类的支持向量的个数

第二个例子:

    # -*- coding:utf-8 -*-
    import numpy as np
    import pylab as pl #pylab主要提供一些画图功能
    from sklearn import svm
    # we create 40 separable points
    X = np.r_[np.random.randn(20, 2) - [2, 2], np.random.randn(20, 2) + [2, 2]]
    #np.r_按row来组合array,np.c_按colunm来组合array
    #产生20行2列的矩阵,数据成正态分布,均值是2,标准差是2
    # >>> x = np.random.randn(20,2)
    # >>> print x                 |        print x-[2,2]
    # [[-0.63145976  0.33214385]  |       [[-2.63145976 -1.66785615]
    #  [-0.73478573 -0.20835977]  |        [-2.73478573 -2.20835977]
    #  [-1.90850336 -0.3006437 ]  |        [-3.90850336 -2.3006437 ]
    #  [-0.06334778  0.33648272]  |        [-2.06334778 -1.66351728]
    #  [-1.47163982  0.46032467]  |        [-3.47163982 -1.53967533]
    #  [ 0.62312495 -1.1757985 ]  |        [-1.37687505 -3.1757985 ]
    #  [ 1.48181577 -0.59935329]  |        [-0.51818423 -2.59935329]
    #  [ 0.85446026  0.66278357]  |        [-1.14553974 -1.33721643]
    #  [ 0.64822314  0.37816488]  |        [-1.35177686 -1.62183512]
    #  [-0.28784258  0.706399  ]  |        [-2.28784258 -1.293601  ]
    #  [ 0.7139413  -0.25064504]  |        [-1.2860587  -2.25064504]
    #  [ 1.16811313  1.16335445]  |        [-0.83188687 -0.83664555]
    #  [-0.01533964 -0.10252879]  |        [-2.01533964 -2.10252879]
    #  [-0.71322496 -1.76602087]  |        [-2.71322496 -3.76602087]
    #  [-0.26507379  1.26459475]  |        [-2.26507379 -0.73540525]
    #  [ 0.52864625 -0.62888543]  |        [-1.47135375 -2.62888543]
    #  [ 0.6315711   1.74487499]  |        [-1.3684289  -0.25512501]
    #  [-0.62708034 -1.66506671]  |        [-2.62708034 -3.66506671]
    #  [ 1.10983563 -1.05212385]  |        [-0.89016437 -3.05212385]
    #  [-0.80558621  0.1177437 ]] |        [-2.80558621 -1.8822563 ]]
    Y = [0]*20 +[1]*20
    # >>> print Y
    # [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]

    #fit the model
    clf = svm.SVC(kernel='linear')
    # sklearn中调用机器学习的方法都是一个道理,算法就是一个类,其中包含fit(), predict()等等许多方法,
    # 我们只要输入训练样本和标记,以及模型的一些可能的参数,自然就直接出分类的结果。
    # SVM既可以用来分类,就是SVC;又可以用来预测,或者成为回归,就是SVR。sklearn中的svm模块中也集成了SVR类。
    clf.fit(X, Y)

    # get the separating hyperplane 得到分离超平面
    w = clf.coef_[0] #coef_存放回归系数 print clf.coef_ 得到:[[ 0.58170366  0
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值