第六章 支持向量机

本文详细介绍了支持向量机的基础知识,包括概念定义、几何间隔、核函数及其在解决非线性分类问题中的作用。讨论了支持向量机的算法推导,特别是线性不可分时的软间隔最大化和对偶问题的求解。最后,通过序列最小最优化算法(SMO)展示了如何利用线性核和高斯核训练SVM,并在西瓜数据集上应用支持向量回归。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

支持向量机

一.基础知识

1 概念及定义

  支持向量机是一个二类分类模型,基本模型的定义为:是在特征空间上的间隔最大的线性分类器

支持向量机学习的基本想法是求解能够正确划分训练数据集并且几何间隔最大的分离超平面.

  二次规划是一类典型的优化问题,包括凸二次优化和非凸二次优化

  目标函数是变量的二次函数,约束条件是变量的线性不等式

核技巧(kernel trick):
   一般用来解决非线性分类问题,将非线性问题转换成线性问题

   第一步:首先使用一个变换将原空间的数据映射到新空间

   第二步:在新的空间里用线性分类学习方法从训练数据中学习分类模型
2.专有概念及名词

函数间隔(functional margin):用 γ^=y(wx+b) γ ^ = y ( w ⋅ x + b ) 来表示分类的正确性及确信度.

几何间隔(geometric margin):对分离超平面进行规范化,例如 w=1 ‖ w ‖ = 1 ,这个时候函数间隔成为几何间隔.

样本点 (xi,yi) ( x i , y i ) 和超平面 (w,b) ( w , b ) 的几何间隔

γi=yi(wwxi+bw) γ i = y i ( w ‖ w ‖ ⋅ x i + b ‖ w ‖ )

取集合中所有样本点中几何间隔最小值

γ=mini=1,...Nγi γ = min i = 1 , . . . N γ i

软间隔最大化:对于线性不可分的问题,通过修改约束条件实现数据的近似线性可分.叫做线性支持向量机的软间隔最大化

支持向量机不仅可以用于分类问题,在回归问题上也可以较好的使用.

支持向量回归(support vector regression)SVR:
支持向量回归有一个容忍偏差范围,当预计值和结果的偏差在范围(相当于间隔的一个区间)之内则认为预测是正确的.

二.思想脉络

所有的机器学习方法

方 法 = 策 略 + 模 型 + 算 法

模型:就是假设空间的公式(条件概率分布和决策函数)

策略:选择最优的模型(利用损失函数,风险函数来进行选择)

算法:具体的计算方法(求解最优模型)

支持向量机的学习目标:

在特征空间中找到一个分离超平面,能够将实例分到不同的类,分离超平面对应于方程

wx+b=0 w ⋅ x + b = 0

由法向量和截距决定,分离超平面将特征空间分成两类,一部分是正类,一部分是负类.法向量指向的一侧为正类,另一侧为负类.

利用间隔最大化来寻找分离超平面

间隔最大化

求解间隔最大分离超平面,可表示为下面的约束最优化问题:

maxw,bγ max w , b γ
s.t.yi(wwxi+bw)γ,i=1,2...N   s . t . y i ( w ‖ w ‖ ⋅ x i + b ‖ w ‖ ) ≥ γ , i = 1 , 2... N     公 式 1

考虑到函数间隔和几何间隔之间的关系,可将问题改写成

maxw,bγ^w max w , b γ ^ ‖ w ‖
s.t.yi(wxi+b)γ^,i=1,2...N   s . t . y i ( w ⋅ x i + b ) ≥ γ ^ , i = 1 , 2... N     公 式 2

函数间隔的取值不影响函数最优化问题的解.取 γ^=1 γ ^ = 1
注意到最大化 1w 1 ‖ w ‖ 和最小化 12w2 1 2 ‖ w ‖ 2 是等价的.

得到线性可分支持向量机的学习最优化问题:

minw,b12w2 min w , b 1 2 ‖ w ‖ 2
s.t.yi(wxi+b)10,i=1,2...N   s . t . y i ( w ⋅ x i + b ) − 1 ≥ 0 , i = 1 , 2... N     公 式 3

凸最优化问题

凸最优化问题变成凸二次规划问题(convex quadratic programming)

根据上述公式3求得最优解  w,b w ∗ , b ∗

得到分离超平面 wx+b=0 w ∗ ⋅ x + b ∗ = 0

分类决策函数

f(x)=sign(wx+b) f ( x ) = s i g n ( w ∗ ⋅ x + b ∗ )

2018.06.22 继续学习

线性不可分与软间隔最大化

主要操作就是将函数间隔大于等于1的条件进行放松.有些点可以出现在"间隔"里,

学习问题变成:

minw,b,ξ12w2+Ci=1Nξi min w , b , ξ 1 2 ‖ w ‖ 2 + C ∑ i = 1 N ξ i

s.t.yi(wxi+b)1ξi,i=1,2,...,N s . t . y i ( w ⋅ x i + b ) ≥ 1 − ξ i , i = 1 , 2 , . . . , N

ξi0,i=1,2,...,N ξ i ≥ 0 , i = 1 , 2 , . . . , N

线性不可分与核技巧

核技巧,用线性分类方法求解非线性分类问题:

第一步:利用一个变换,将原数据的空间映射到新空间
第二步:在新空间里用线性分类学习方法从训练数据集中学习分类模型

最重要的就是核函数的选择,"核函数选择"就是意味着将样本映射到一个指定的特征空间,特征空间对于支持向量机的好坏影响很大.也就是划分的选择.

三.算法推导

对偶问题求解

备注:拉格朗日对偶性 参考李航<统计学习方法>附录C.

拉格朗日对偶:https://www.cnblogs.com/ooon/p/5723725.html

dual problem

对每条约束添加拉格朗日乘子 αi0 α i ≥ 0 ,拉格朗日函数可以写成

L(w,x,α)=12w2+i=1mαi(1yi(wTxi+b)) L ( w , x , α ) = 1 2 ‖ w ‖ 2 + ∑ i = 1 m α i ( 1 − y i ( w T x i + b ) )

其中 α=(α1;α2;...αm) α = ( α 1 ; α 2 ; . . . α m ) .

L(w,b,a)w,b L ( w , b , a ) 对 w , b 分 别 进 行 求 偏 导 , 令 其 值 为 0 可得

w=i=1mαiyiwi w = ∑ i = 1 m α i y i w i

0=i=1mαiyi 0 = ∑ i = 1 m α i y i

有上述公式及定义可得

f(x)=12w2=maxw,bL(w,b,α) f ( x ) = 1 2 ‖ w ‖ 2 = max w , b L ( w , b , α )

原始问题:

求解 min f(x) min   f ( x ) 最小值 求解极小极大值

minαf(x)=minαmaxw,bL(w,b,α) min α f ( x ) = min α max w , b L ( w , b , α )

对偶问题:

求解极小极大值转换成为极大极小值.

maxαminw,bL(w,b,α) max α min w , b L ( w , b , α )

转变成对偶问题后, L(w,b,α) L ( w , b , α ) 是一个二次函数,可以求导求解其极小值.然后继续求解其中的极大值.

运算过程

1.求解 minw,bL(w,b,α) min w , b L ( w , b , α ) :

二次函数求极值问题

dL(w,b,α)dw=wi=1Nαiyixi=0 d L ( w , b , α ) d w = w − ∑ i = 1 N α i y i x i = 0
dL(w,b,α)db=i=1Nαiyi=0 d L ( w , b , α ) d b = − ∑ i = 1 N α i y i = 0
求解后得:
w=i=1Nαiyixi w = ∑ i = 1 N α i y i x i
0=i=1Nαiyi 0 = ∑ i = 1 N α i y i
将上述解代入到公式 L(w,b,a) L ( w , b , a ) 中:
L(w,b,a)=12i=1Nj=1Nαiαjyiyj(xixj)iNαiyi[(j=1Nαjyjxj)xi+b]+i=1Nαi L ( w , b , a ) = 1 2 ∑ i = 1 N ∑ j = 1 N α i α j y i y j ( x i ⋅ x j ) − ∑ i N α i y i [ ( ∑ j = 1 N α j y j x j ) ⋅ x i + b ] + ∑ i = 1 N α i

=12i=1Nj=1Nαiαjyiyj(xixj)+i=1Nαi = − 1 2 ∑ i = 1 N ∑ j = 1 N α i α j y i y j ( x i ⋅ x j ) + ∑ i = 1 N α i

故可得:

minw.bL(w,b,α)=12i=1Nj=1Nαiαjyiyj(xixj)+i=1Nαi min w . b L ( w , b , α ) = − 1 2 ∑ i = 1 N ∑ j = 1 N α i α j y i y j ( x i ⋅ x j ) + ∑ i = 1 N α i

2.求解 minw,bL(w,b,α) min w , b L ( w , b , α ) 的极大值

maxα12i=1Nj=1Nαiαjyiyj(xixj)+i=1Nαi max α − 1 2 ∑ i = 1 N ∑ j = 1 N α i α j y i y j ( x i ⋅ x j ) + ∑ i = 1 N α i

s.t.i=1Nαiyi=0 s . t . ∑ i = 1 N α i y i = 0

αi0,i=1,2,...,N α i ≥ 0 , i = 1 , 2 , . . . , N

可转换成

minα12i=1Nj=1Nαiαjyiyj(xixj)i=1Nαi min α 1 2 ∑ i = 1 N ∑ j = 1 N α i α j y i y j ( x i ⋅ x j ) − ∑ i = 1 N α i

s.t.i=1Nαiyi=0 s . t . ∑ i = 1 N α i y i = 0

αi0,i=1,2,...,N α i ≥ 0 , i = 1 , 2 , . . . , N

根据定理进行求解 w,b w ∗ , b ∗ :

w=i=1Nαiyjxj w ∗ = ∑ i = 1 N α i ∗ y j x j

b=yji=1Nαiyi(xixj) b ∗ = y j − ∑ i = 1 N α i ∗ y i ( x i ⋅ x j )

分离超平面

i=1Nαiyi(xixj)+b=0 ∑ i = 1 N α i ∗ y i ( x i ⋅ x j ) + b ∗ = 0

分类决策函数

f(x)=sign(i=1Nαiyi(xixj)+b) f ( x ) = s i g n ( ∑ i = 1 N α i ∗ y i ( x i ⋅ x j ) + b ∗ )

对于软间隔最大化,求解的思路是一样的,不同点就是构造的拉格朗日函数不同以及约束条件不同

核函数的相关求解,理论方面还是没有理解透彻.还需要继续学习

学习断点 核函数的学习  主要是理论的理解 

四.编程实现

2.序列最小最优化算法

sequential minimal optimization SMO算法
核函数的理解:将一个欧式空间映射到希尔伯特空间的一个函数
核函数的选择是支持向量机的最大变数,核函数的选取一般也是按照经验公式来进行选取
例如:文本数据通常采用线性核,情况不明确的时候可以尝试高斯核

3.利用线性核&高斯核训练一个SVM,并比较支持向量的区别

3.1数据的读取和可视化处理

# 西瓜数据集的读取
import pandas as pd
with open('/home/dengshuo/GithubCode/ML/CH06/watermelon_3a.csv') as datafile:
    df=pd.read_csv(datafile ,header=None)

print (df)
    • 0 1 2 3
      0 1 0.697 0.4600 1
      1 2 0.774 0.3760 1
      2 3 0.634 0.2640 1
      3 4 0.608 0.3180 1
      4 5 0.556 0.2150 1
      5 6 0.403 0.2370 1
      6 7 0.481 0.1490 1
      7 8 0.437 0.2110 1
      8 9 0.666 0.0910 0
      9 10 0.243 0.0267 0
      10 11 0.245 0.0570 0
      11 12 0.343 0.0990 0
      12 13 0.639 0.1610 0
      13 14 0.657 0.1980 0
      14 15 0.360 0.3700 0
      15 16 0.593 0.0420 0
      16 17 0.719 0.1030 0

    read_csv() arg
    header:
    int or list of ints, default ‘infer’
    Row number(s) to use as the column names, and the start of the data.
    Default behavior is as if set to 0 if no names passed, otherwise
    None. Explicitly pass header=0 to be able to replace existing
    names. The header can be a list of integers that specify row locations for
    a multi-index on the columns e.g. [0,1,3]. Intervening rows that are not
    specified will be skipped (e.g. 2 in this example is skipped). Note that
    this parameter ignores commented lines and empty lines if
    skip_blank_lines=True, so header=0 denotes the first line of data
    rather than the first line of the file.

# 制作表头 : 将每列数据填上标签,将每列数据进行命名,
# 当原始的数据中含有名字时,可以直接进行读取

# make header 

df.columns=['id','density','sugar_content','label']
print(df)
  • id density sugar_content label
    0 1 0.697 0.4600 1
    1 2 0.774 0.3760 1
    2 3 0.634 0.2640 1
    3 4 0.608 0.3180 1
    4 5 0.556 0.2150 1
    5 6 0.403 0.2370 1
    6 7 0.481 0.1490 1
    7 8 0.437 0.2110 1
    8 9 0.666 0.0910 0
    9 10 0.243 0.0267 0
    10 11 0.245 0.0570 0
    11 12 0.343 0.0990 0
    12 13 0.639 0.1610 0
    13 14 0.657 0.1980 0
    14 15 0.360 0.3700 0
    15 16 0.593 0.0420 0
    16 17 0.719 0.1030 0
# 更改索引列 df.set_index 将原先按第一列的数据,按要求的列进行索引

df.set_index(['id'])
.dataframe thead tr:only-child th { text-align: right; } .dataframe thead th { text-align: left; } .dataframe tbody tr th { vertical-align: top; }
densitysugar_contentlabel
id
10.6970.46001
20.7740.37601
30.6340.26401
40.6080.31801
50.5560.21501
60.4030.23701
70.4810.14901
80.4370.21101
90.6660.09100
100.2430.02670
110.2450.05700
120.3430.09900
130.6390.16100
140.6570.19800
150.3600.37000
160.5930.04200
170.7190.10300
# 数据进行可视化处理

import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns

fig=plt.figure()
ax=fig.add_subplot(111)
ax.scatter(df['density'],df['sugar_content'])
plt.show()

png

可以画出一个最基本的图形,但是这个只能反映出两个变量之间的函数关系
没有显示出好坏瓜与两个变量之间的联系.label 没有表现出来
# 利用matplotlib & seaborn 来进行画图

plt.figure(0)
sns.FacetGrid(df,hue='label',size=5).map(plt.scatter,'density','sugar_content').add_legend()
plt.show()
df[['density','sugar_content']].values

-

  • array([[ 0.697 , 0.46 ],
    [ 0.774 , 0.376 ],
    [ 0.634 , 0.264 ],
    [ 0.608 , 0.318 ],
    [ 0.556 , 0.215 ],
    [ 0.403 , 0.237 ],
    [ 0.481 , 0.149 ],
    [ 0.437 , 0.211 ],
    [ 0.666 , 0.091 ],
    [ 0.243 , 0.0267],
    [ 0.245 , 0.057 ],
    [ 0.343 , 0.099 ],
    [ 0.639 , 0.161 ],
    [ 0.657 , 0.198 ],
    [ 0.36 , 0.37 ],
    [ 0.593 , 0.042 ],
    [ 0.719 , 0.103 ]])

    利用seaborn可以表示三个维度 row ,columns,hue hue 利用颜色的不同来进行区分

    3.2 调用机器学习包来实现算法
    下面是一个最简单的基本流程,可以用来模仿

    help(plt.scatter)

    scatter(x, y, s=None, c=None, marker=None, cmap=None, norm=None, vmin=None, vmax=None, alpha=None, linewidths=None, verts=None, edgecolors=None, hold=None, data=None, **kwargs)

    help(plt.pcolorsmesh)

    pcolormesh(*args, **kwargs)
    Plot a quadrilateral mesh. 绘制一个四边形网格

    help(np.meshgrid)

    meshgrid(*xi, **kwargs)
    Return coordinate matrices from coordinate vectors.

    从坐标向量返回坐标矩阵
    Make N-D coordinate arrays for vectorized evaluations of
    N-D scalar/vector fields over N-D grids, given
    one-dimensional coordinate arrays x1, x2,…, xn.

# import library

from sklearn import svm

# assumed you have X (predictor) and Y(target)
#Create SVM classification object

model=svm.SVC(C=1000,kernel='rbf')  # Penalty parameter C of the error term.

# 这个C 是个错误项的惩罚参数
# 可直接通过改变核函数来进行
# 或者定义一个循环来进行
#  for fig_num ,kernel in enumerate(('linear','rbf')):
#       model=svm.SVC(C=1000,kernel=kernel)
#   涉及的变量进行修改

# train the model using the training sets and check score

X=df[['density','sugar_content']].values
y=df['label'].values

model.fit(X,y)

# get support vectors

sv=model.support_vectors_

# draw decision zone

plt.figure('rbf')
plt.clf()

# plot point and mark out support vectors

plt.scatter(X[:,0],X[:,1],edgecolors='k',c=y,cmap=plt.cm.Paired,zorder=10) 

# 只有这一个 zorder 参数没有看懂,不是很明白是什么意思 

plt.scatter(sv[:,0],sv[:,1],edgecolors='k',facecolors='none',s=80,linewidths=2,zorder=10)

# plot the decision boundary and decision zone into a color plot 

x_min,x_max=X[:,0].min()-0.2 ,X[:,0].max()+0.2
y_min,y_max=X[:,1].min()-0.2 ,X[:,1].max()+0.2

XX,YY=np.meshgrid(np.arange(x_min,x_max,0.02),np.arange(y_min,y_max,0.02))

Z=model.decision_function(np.c_[XX.ravel(),YY.ravel()])

#   decision_function(X) method of sklearn.svm.classes.SVC instance
#   Distance of the samples X to the separating hyperplane.


Z=Z.reshape(XX.shape)

plt.pcolormesh(XX,YY,Z>0,cmap=plt.cm.Paired)

plt.contour(XX,YY,Z,colors=['k','k','k'],linestyles=['--','-','--'],levels=[-.5,0,.5])

plt.title('svm:kernel=rbf')
plt.axis('tight')
plt.xlabel('density')
plt.ylabel('sugar_content')
plt.show()

png

4. 利用西瓜数据集3.0a ,密度”density”为输入,含糖率”sugar_content”为输出.训练一个SVR
df.columns=['id','density','sugar_content','label']
df.set_index(['id'])
.dataframe thead tr:only-child th { text-align: right; } .dataframe thead th { text-align: left; } .dataframe tbody tr th { vertical-align: top; }
densitysugar_contentlabel
id
10.6970.46001
20.7740.37601
30.6340.26401
40.6080.31801
50.5560.21501
60.4030.23701
70.4810.14901
80.4370.21101
90.6660.09100
100.2430.02670
110.2450.05700
120.3430.09900
130.6390.16100
140.6570.19800
150.3600.37000
160.5930.04200
170.7190.10300
X=df['density']
y=df['sugar_content']
plt.figure()
plt.scatter(X,y,c='g')
plt.xlabel('density')
plt.ylabel('sugar_content')
plt.show()

# 但是这个图 没办法显示 好坏 瓜的关系  应该还有其他的办法

png

# 试着调用机器学习包
from sklearn import svm

svr=svm.SVR()

X=df['density'].values
y=df['sugar_content'].values

svr.fit(X,y)
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn.svm import SVR  # for SVR model
from sklearn.model_selection import GridSearchCV  # for optimal parameter search

# loading data
df = pd.read_csv('/home/dengshuo/GithubCode/ML/CH06/watermelon_3a.csv', header=None, )
df.columns = ['id', 'density', 'sugar_content', 'label']
df.set_index(['id'])
X = df[['density']].values
y = df[['sugar_content']].values

# generate model and fitting
# linear
# svr = GridSearchCV(SVR(kernel='linear'), cv=5, param_grid={"C": [1e0, 1e1, 1e2, 1e3]})

# rbf
# svr = GridSearchCV(SVR(kernel='rbf'), cv=5, param_grid={"C": [1e-4, 1e-3, 1e-2, 1e-1, 1, 1e1, 1e2, 1e3, 1e4]})

# ploy

svr = GridSearchCV(SVR(kernel='poly'), cv=5, param_grid={"C": [1e-4, 1e-3, 1e-2, 1e-1, 1, 1e1, 1e2, 1e3, 1e4]})

svr.fit(X, y)

sv_ratio = svr.best_estimator_.support_.shape[0] / len(X)
print("Support vector ratio: %.3f" % sv_ratio)

y_svr = svr.predict(X)

sv_ind = svr.best_estimator_.support_
plt.scatter(X[sv_ind], y[sv_ind], c='r', s=50, label='SVR support vectors', zorder=2)
plt.scatter(X, y, c='k', label='data', zorder=1)
plt.plot(X, y_svr, c='orange', label='SVR fit curve with poly kernel')
plt.xlabel('density')
plt.ylabel('sugar_ratio')
plt.title('SVR on watermelon3.0a')
plt.legend()
plt.show()
/home/dengshuo/anaconda3/lib/python3.6/site-packages/sklearn/utils/validation.py:578: DataConversionWarning: A column-vector y was passed when a 1d array was expected. Please change the shape of y to (n_samples, ), for example using ravel().
  y = column_or_1d(y, warn=True)
/home/dengshuo/anaconda3/lib/python3.6/site-packages/sklearn/utils/validation.py:578: DataConversionWarning: A column-vector y was passed when a 1d array was expected. Please change the shape of y to (n_samples, ), for example using ravel().
  y = column_or_1d(y, warn=True)
/home/dengshuo/anaconda3/lib/python3.6/site-packages/sklearn/utils/validation.py:578: DataConversionWarning: A column-vector y was passed when a 1d array was expected. Please change the shape of y to (n_samples, ), for example using ravel().
  y = column_or_1d(y, warn=True)
/home/dengshuo/anaconda3/lib/python3.6/site-packages/sklearn/utils/validation.py:578: DataConversionWarning: A column-vector y was passed when a 1d array was expected. Please change the shape of y to (n_samples, ), for example using ravel().
  y = column_or_1d(y, warn=True)
/home/dengshuo/anaconda3/lib/python3.6/site-packages/sklearn/utils/validation.py:578: DataConversionWarning: A column-vector y was passed when a 1d array was expected. Please change the shape of y to (n_samples, ), for example using ravel().
  y = column_or_1d(y, warn=True)
/home/dengshuo/anaconda3/lib/python3.6/site-packages/sklearn/utils/validation.py:578: DataConversionWarning: A column-vector y was passed when a 1d array was expected. Please change the shape of y to (n_samples, ), for example using ravel().
  y = column_or_1d(y, warn=True)
/home/dengshuo/anaconda3/lib/python3.6/site-packages/sklearn/utils/validation.py:578: DataConversionWarning: A column-vector y was passed when a 1d array was expected. Please change the shape of y to (n_samples, ), for example using ravel().
  y = column_or_1d(y, warn=True)
/home/dengshuo/anaconda3/lib/python3.6/site-packages/sklearn/utils/validation.py:578: DataConversionWarning: A column-vector y was passed when a 1d array was expected. Please change the shape of y to (n_samples, ), for example using ravel().
  y = column_or_1d(y, warn=True)
/home/dengshuo/anaconda3/lib/python3.6/site-packages/sklearn/utils/validation.py:578: DataConversionWarning: A column-vector y was passed when a 1d array was expected. Please change the shape of y to (n_samples, ), for example using ravel().
  y = column_or_1d(y, warn=True)
/home/dengshuo/anaconda3/lib/python3.6/site-packages/sklearn/utils/validation.py:578: DataConversionWarning: A column-vector y was passed when a 1d array was expected. Please change the shape of y to (n_samples, ), for example using ravel().
  y = column_or_1d(y, warn=True)
/home/dengshuo/anaconda3/lib/python3.6/site-packages/sklearn/utils/validation.py:578: DataConversionWarning: A column-vector y was passed when a 1d array was expected. Please change the shape of y to (n_samples, ), for example using ravel().
  y = column_or_1d(y, warn=True)
/home/dengshuo/anaconda3/lib/python3.6/site-packages/sklearn/utils/validation.py:578: DataConversionWarning: A column-vector y was passed when a 1d array was expected. Please change the shape of y to (n_samples, ), for example using ravel().
  y = column_or_1d(y, warn=True)
/home/dengshuo/anaconda3/lib/python3.6/site-packages/sklearn/utils/validation.py:578: DataConversionWarning: A column-vector y was passed when a 1d array was expected. Please change the shape of y to (n_samples, ), for example using ravel().
  y = column_or_1d(y, warn=True)
/home/dengshuo/anaconda3/lib/python3.6/site-packages/sklearn/utils/validation.py:578: DataConversionWarning: A column-vector y was passed when a 1d array was expected. Please change the shape of y to (n_samples, ), for example using ravel().
  y = column_or_1d(y, warn=True)
/home/dengshuo/anaconda3/lib/python3.6/site-packages/sklearn/utils/validation.py:578: DataConversionWarning: A column-vector y was passed when a 1d array was expected. Please change the shape of y to (n_samples, ), for example using ravel().
  y = column_or_1d(y, warn=True)
/home/dengshuo/anaconda3/lib/python3.6/site-packages/sklearn/utils/validation.py:578: DataConversionWarning: A column-vector y was passed when a 1d array was expected. Please change the shape of y to (n_samples, ), for example using ravel().
  y = column_or_1d(y, warn=True)
/home/dengshuo/anaconda3/lib/python3.6/site-packages/sklearn/utils/validation.py:578: DataConversionWarning: A column-vector y was passed when a 1d array was expected. Please change the shape of y to (n_samples, ), for example using ravel().
  y = column_or_1d(y, warn=True)
/home/dengshuo/anaconda3/lib/python3.6/site-packages/sklearn/utils/validation.py:578: DataConversionWarning: A column-vector y was passed when a 1d array was expected. Please change the shape of y to (n_samples, ), for example using ravel().
  y = column_or_1d(y, warn=True)
/home/dengshuo/anaconda3/lib/python3.6/site-packages/sklearn/utils/validation.py:578: DataConversionWarning: A column-vector y was passed when a 1d array was expected. Please change the shape of y to (n_samples, ), for example using ravel().
  y = column_or_1d(y, warn=True)
/home/dengshuo/anaconda3/lib/python3.6/site-packages/sklearn/utils/validation.py:578: DataConversionWarning: A column-vector y was passed when a 1d array was expected. Please change the shape of y to (n_samples, ), for example using ravel().
  y = column_or_1d(y, warn=True)
/home/dengshuo/anaconda3/lib/python3.6/site-packages/sklearn/utils/validation.py:578: DataConversionWarning: A column-vector y was passed when a 1d array was expected. Please change the shape of y to (n_samples, ), for example using ravel().
  y = column_or_1d(y, warn=True)
/home/dengshuo/anaconda3/lib/python3.6/site-packages/sklearn/utils/validation.py:578: DataConversionWarning: A column-vector y was passed when a 1d array was expected. Please change the shape of y to (n_samples, ), for example using ravel().
  y = column_or_1d(y, warn=True)
/home/dengshuo/anaconda3/lib/python3.6/site-packages/sklearn/utils/validation.py:578: DataConversionWarning: A column-vector y was passed when a 1d array was expected. Please change the shape of y to (n_samples, ), for example using ravel().
  y = column_or_1d(y, warn=True)
/home/dengshuo/anaconda3/lib/python3.6/site-packages/sklearn/utils/validation.py:578: DataConversionWarning: A column-vector y was passed when a 1d array was expected. Please change the shape of y to (n_samples, ), for example using ravel().
  y = column_or_1d(y, warn=True)
/home/dengshuo/anaconda3/lib/python3.6/site-packages/sklearn/utils/validation.py:578: DataConversionWarning: A column-vector y was passed when a 1d array was expected. Please change the shape of y to (n_samples, ), for example using ravel().
  y = column_or_1d(y, warn=True)
/home/dengshuo/anaconda3/lib/python3.6/site-packages/sklearn/utils/validation.py:578: DataConversionWarning: A column-vector y was passed when a 1d array was expected. Please change the shape of y to (n_samples, ), for example using ravel().
  y = column_or_1d(y, warn=True)
/home/dengshuo/anaconda3/lib/python3.6/site-packages/sklearn/utils/validation.py:578: DataConversionWarning: A column-vector y was passed when a 1d array was expected. Please change the shape of y to (n_samples, ), for example using ravel().
  y = column_or_1d(y, warn=True)
/home/dengshuo/anaconda3/lib/python3.6/site-packages/sklearn/utils/validation.py:578: DataConversionWarning: A column-vector y was passed when a 1d array was expected. Please change the shape of y to (n_samples, ), for example using ravel().
  y = column_or_1d(y, warn=True)
/home/dengshuo/anaconda3/lib/python3.6/site-packages/sklearn/utils/validation.py:578: DataConversionWarning: A column-vector y was passed when a 1d array was expected. Please change the shape of y to (n_samples, ), for example using ravel().
  y = column_or_1d(y, warn=True)
/home/dengshuo/anaconda3/lib/python3.6/site-packages/sklearn/utils/validation.py:578: DataConversionWarning: A column-vector y was passed when a 1d array was expected. Please change the shape of y to (n_samples, ), for example using ravel().
  y = column_or_1d(y, warn=True)
/home/dengshuo/anaconda3/lib/python3.6/site-packages/sklearn/utils/validation.py:578: DataConversionWarning: A column-vector y was passed when a 1d array was expected. Please change the shape of y to (n_samples, ), for example using ravel().
  y = column_or_1d(y, warn=True)
/home/dengshuo/anaconda3/lib/python3.6/site-packages/sklearn/utils/validation.py:578: DataConversionWarning: A column-vector y was passed when a 1d array was expected. Please change the shape of y to (n_samples, ), for example using ravel().
  y = column_or_1d(y, warn=True)
/home/dengshuo/anaconda3/lib/python3.6/site-packages/sklearn/utils/validation.py:578: DataConversionWarning: A column-vector y was passed when a 1d array was expected. Please change the shape of y to (n_samples, ), for example using ravel().
  y = column_or_1d(y, warn=True)
/home/dengshuo/anaconda3/lib/python3.6/site-packages/sklearn/utils/validation.py:578: DataConversionWarning: A column-vector y was passed when a 1d array was expected. Please change the shape of y to (n_samples, ), for example using ravel().
  y = column_or_1d(y, warn=True)
/home/dengshuo/anaconda3/lib/python3.6/site-packages/sklearn/utils/validation.py:578: DataConversionWarning: A column-vector y was passed when a 1d array was expected. Please change the shape of y to (n_samples, ), for example using ravel().
  y = column_or_1d(y, warn=True)
/home/dengshuo/anaconda3/lib/python3.6/site-packages/sklearn/utils/validation.py:578: DataConversionWarning: A column-vector y was passed when a 1d array was expected. Please change the shape of y to (n_samples, ), for example using ravel().
  y = column_or_1d(y, warn=True)
/home/dengshuo/anaconda3/lib/python3.6/site-packages/sklearn/utils/validation.py:578: DataConversionWarning: A column-vector y was passed when a 1d array was expected. Please change the shape of y to (n_samples, ), for example using ravel().
  y = column_or_1d(y, warn=True)
/home/dengshuo/anaconda3/lib/python3.6/site-packages/sklearn/utils/validation.py:578: DataConversionWarning: A column-vector y was passed when a 1d array was expected. Please change the shape of y to (n_samples, ), for example using ravel().
  y = column_or_1d(y, warn=True)
/home/dengshuo/anaconda3/lib/python3.6/site-packages/sklearn/utils/validation.py:578: DataConversionWarning: A column-vector y was passed when a 1d array was expected. Please change the shape of y to (n_samples, ), for example using ravel().
  y = column_or_1d(y, warn=True)
/home/dengshuo/anaconda3/lib/python3.6/site-packages/sklearn/utils/validation.py:578: DataConversionWarning: A column-vector y was passed when a 1d array was expected. Please change the shape of y to (n_samples, ), for example using ravel().
  y = column_or_1d(y, warn=True)
/home/dengshuo/anaconda3/lib/python3.6/site-packages/sklearn/utils/validation.py:578: DataConversionWarning: A column-vector y was passed when a 1d array was expected. Please change the shape of y to (n_samples, ), for example using ravel().
  y = column_or_1d(y, warn=True)
/home/dengshuo/anaconda3/lib/python3.6/site-packages/sklearn/utils/validation.py:578: DataConversionWarning: A column-vector y was passed when a 1d array was expected. Please change the shape of y to (n_samples, ), for example using ravel().
  y = column_or_1d(y, warn=True)
/home/dengshuo/anaconda3/lib/python3.6/site-packages/sklearn/utils/validation.py:578: DataConversionWarning: A column-vector y was passed when a 1d array was expected. Please change the shape of y to (n_samples, ), for example using ravel().
  y = column_or_1d(y, warn=True)
/home/dengshuo/anaconda3/lib/python3.6/site-packages/sklearn/utils/validation.py:578: DataConversionWarning: A column-vector y was passed when a 1d array was expected. Please change the shape of y to (n_samples, ), for example using ravel().
  y = column_or_1d(y, warn=True)
/home/dengshuo/anaconda3/lib/python3.6/site-packages/sklearn/utils/validation.py:578: DataConversionWarning: A column-vector y was passed when a 1d array was expected. Please change the shape of y to (n_samples, ), for example using ravel().
  y = column_or_1d(y, warn=True)
/home/dengshuo/anaconda3/lib/python3.6/site-packages/sklearn/utils/validation.py:578: DataConversionWarning: A column-vector y was passed when a 1d array was expected. Please change the shape of y to (n_samples, ), for example using ravel().
  y = column_or_1d(y, warn=True)


Support vector ratio: 0.471

png

### 关于支持向量机的课后练习解决方案 对于希望深入理解并实践支持向量机(SVM)的学习者而言,获取高质量的课后习题及其解答至关重要。在吴恩达教授提供的机器学习课程中,关于支持向量机的内容被安排在第十二周的教学计划里[^1]。 #### 练习题目设计思路 为了帮助学生更好地掌握SVM理论基础以及实际应用技巧,在该章节设置了多项选择题形式的小测验来检验学员的理解程度。这些测试不仅涵盖了优化目标函数等核心概念,还包括如何调整参数C和核函数的选择等内容。 #### 实践项目建议 除了完成在线平台上的随堂练习外,还可以尝试参与Kaggle竞赛中的分类问题挑战赛;通过真实数据集的操作加深对算法原理的认识,并学会解决工程实现过程中遇到的各种难题。此外,GitHub上有许多开源的支持向量机案例可供参考学习。 ```python from sklearn import datasets from sklearn.model_selection import train_test_split from sklearn.preprocessing import StandardScaler from sklearn.svm import SVC # 加载鸢尾花数据集作为示例 iris = datasets.load_iris() X, y = iris.data[:, [2, 3]], iris.target # 数据预处理 scaler = StandardScaler().fit(X) X_std = scaler.transform(X) # 划分训练集与测试集 X_train, X_test, y_train, y_test = train_test_split( X_std, y, test_size=0.3, random_state=1, stratify=y) # 使用线性核构建模型 svm = SVC(kernel='linear', C=1.0, random_state=1) svm.fit(X_train, y_train) ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值