当前位置:   article > 正文

svm及smo公式手推+代码实现_机器学习方法 李航 svm smo python代码 demo

机器学习方法 李航 svm smo python代码 demo

目录

svm推导

smo优化推导

代码部分


svm推导

smo优化推导

 

 

 

 

 

 

 

代码部分

  1. import sys
  2. from numpy import *
  3. #from math import *
  4. def loadDataSet(filename):
  5. fr = open(filename)
  6. data = []
  7. label = []
  8. for line in fr.readlines():
  9. lineAttr = line.strip().split('\t')
  10. data.append([float(x) for x in lineAttr[:-1]])
  11. label.append(float(lineAttr[-1]))
  12. return data,label
  13. def selectJrand(i,m):
  14. j = i
  15. while j == i:
  16. j = int(random.uniform(0,m))
  17. return j
  18. def clipAlpha(a_j,H,L):
  19. if a_j > H:
  20. a_j = H
  21. if L > a_j:
  22. a_j = L
  23. return a_j
  24. def smoSimple(data,label,C,toler,maxIter):
  25. dataMatrix = mat(data)
  26. labelMatrix = mat(label).transpose()
  27. b = 0.0
  28. iter = 0
  29. m,n = shape(dataMatrix)
  30. alpha = mat(zeros((m,1)))
  31. while iter < maxIter:
  32. alphapairChanged = 0
  33. for i in range(m):
  34. fxi = float(multiply(alpha,labelMatrix).T * (dataMatrix * dataMatrix[i,:].T)) + b
  35. Ei = fxi - float(labelMatrix[i])
  36. if labelMatrix[i] * Ei < -toler and alpha[i] < C or labelMatrix[i] * Ei > toler and alpha[i] > 0:
  37. j = selectJrand(i,m)
  38. fxj = float(multiply(alpha,labelMatrix).T * (dataMatrix * dataMatrix[j,:].T)) + b
  39. Ej = fxj - float(labelMatrix[j])
  40. alphaIOld = alpha[i].copy()
  41. alphaJOld = alpha[j].copy()
  42. if labelMatrix[i] != labelMatrix[j]:
  43. L = max(0,alpha[j] - alpha[i])
  44. H = min(C,C+alpha[j]-alpha[i])
  45. else:
  46. L = max(0,alpha[i]+alpha[j] - C)
  47. H = min(C,alpha[j]+alpha[i])
  48. if L==H:
  49. print ("L==H")
  50. continue
  51. eta = 2.0 * dataMatrix[i,:]*dataMatrix[j,:].T - dataMatrix[i,:]*dataMatrix[i,:].T - dataMatrix[j,:]*dataMatrix[j,:].T
  52. if eta >= 0:
  53. print ("eta >= 0")
  54. continue
  55. alpha[j] -= labelMatrix[j]*(Ei-Ej)/eta
  56. alpha[j] = clipAlpha(alpha[j],H,L)
  57. if abs(alpha[j] - alphaJOld) < 0.00001 :
  58. print ("%d not move enough"%j)
  59. continue
  60. alpha[i] += labelMatrix[j]*labelMatrix[i]*(alphaJOld - alpha[j])
  61. b1 = b - Ei -labelMatrix[i]*(alpha[i] - alphaIOld)*dataMatrix[i,:]*dataMatrix[i,:].T \
  62. -labelMatrix[j]*(alpha[j]-alphaJOld)*dataMatrix[i,:]*dataMatrix[j,:].T
  63. b2 = b - Ej -labelMatrix[i]*(alpha[i] - alphaIOld)*dataMatrix[i,:]*dataMatrix[j,:].T \
  64. -labelMatrix[j]*(alpha[j]-alphaJOld)*dataMatrix[j,:]*dataMatrix[j,:].T
  65. if alpha[i] > 0 and alpha[i] < C:
  66. b = b1
  67. elif alpha[j] > 0 and alpha[j] < C:
  68. b = b2
  69. else:
  70. b = (b1+b2)/2.0
  71. alphapairChanged +=1
  72. print ("第%d迭代 第%d个值,更改了%d次" %(iter,i,alphapairChanged))
  73. if alphapairChanged == 0:
  74. iter +=1
  75. else:
  76. iter = 0
  77. print ("迭代次数: %d" % iter)
  78. return b,alpha
  79. def main():
  80. data,label = loadDataSet('testSet.txt')
  81. print (data)
  82. print (label)
  83. b,alpha = smoSimple(data,label,0.6,0.001,40)
  84. print (b)
  85. print (alpha)
  86. for i in range(100):
  87. if alpha[i]>0:
  88. print (data[i],label[i])
  89. if __name__ == '__main__':
  90. sys.exit(main())

数据集 

  1. 3.542485 1.977398 -1
  2. 3.018896 2.556416 -1
  3. 7.551510 -1.580030 1
  4. 2.114999 -0.004466 -1
  5. 8.127113 1.274372 1
  6. 7.108772 -0.986906 1
  7. 8.610639 2.046708 1
  8. 2.326297 0.265213 -1
  9. 3.634009 1.730537 -1
  10. 0.341367 -0.894998 -1
  11. 3.125951 0.293251 -1
  12. 2.123252 -0.783563 -1
  13. 0.887835 -2.797792 -1
  14. 7.139979 -2.329896 1
  15. 1.696414 -1.212496 -1
  16. 8.117032 0.623493 1
  17. 8.497162 -0.266649 1
  18. 4.658191 3.507396 -1
  19. 8.197181 1.545132 1
  20. 1.208047 0.213100 -1
  21. 1.928486 -0.321870 -1
  22. 2.175808 -0.014527 -1
  23. 7.886608 0.461755 1
  24. 3.223038 -0.552392 -1
  25. 3.628502 2.190585 -1
  26. 7.407860 -0.121961 1
  27. 7.286357 0.251077 1
  28. 2.301095 -0.533988 -1
  29. -0.232542 -0.547690 -1
  30. 3.457096 -0.082216 -1
  31. 3.023938 -0.057392 -1
  32. 8.015003 0.885325 1
  33. 8.991748 0.923154 1
  34. 7.916831 -1.781735 1
  35. 7.616862 -0.217958 1
  36. 2.450939 0.744967 -1
  37. 7.270337 -2.507834 1
  38. 1.749721 -0.961902 -1
  39. 1.803111 -0.176349 -1
  40. 8.804461 3.044301 1
  41. 1.231257 -0.568573 -1
  42. 2.074915 1.410550 -1
  43. -0.743036 -1.736103 -1
  44. 3.536555 3.964960 -1
  45. 8.410143 0.025606 1
  46. 7.382988 -0.478764 1
  47. 6.960661 -0.245353 1
  48. 8.234460 0.701868 1
  49. 8.168618 -0.903835 1
  50. 1.534187 -0.622492 -1
  51. 9.229518 2.066088 1
  52. 7.886242 0.191813 1
  53. 2.893743 -1.643468 -1
  54. 1.870457 -1.040420 -1
  55. 5.286862 -2.358286 1
  56. 6.080573 0.418886 1
  57. 2.544314 1.714165 -1
  58. 6.016004 -3.753712 1
  59. 0.926310 -0.564359 -1
  60. 0.870296 -0.109952 -1
  61. 2.369345 1.375695 -1
  62. 1.363782 -0.254082 -1
  63. 7.279460 -0.189572 1
  64. 1.896005 0.515080 -1
  65. 8.102154 -0.603875 1
  66. 2.529893 0.662657 -1
  67. 1.963874 -0.365233 -1
  68. 8.132048 0.785914 1
  69. 8.245938 0.372366 1
  70. 6.543888 0.433164 1
  71. -0.236713 -5.766721 -1
  72. 8.112593 0.295839 1
  73. 9.803425 1.495167 1
  74. 1.497407 -0.552916 -1
  75. 1.336267 -1.632889 -1
  76. 9.205805 -0.586480 1
  77. 1.966279 -1.840439 -1
  78. 8.398012 1.584918 1
  79. 7.239953 -1.764292 1
  80. 7.556201 0.241185 1
  81. 9.015509 0.345019 1
  82. 8.266085 -0.230977 1
  83. 8.545620 2.788799 1
  84. 9.295969 1.346332 1
  85. 2.404234 0.570278 -1
  86. 2.037772 0.021919 -1
  87. 1.727631 -0.453143 -1
  88. 1.979395 -0.050773 -1
  89. 8.092288 -1.372433 1
  90. 1.667645 0.239204 -1
  91. 9.854303 1.365116 1
  92. 7.921057 -1.327587 1
  93. 8.500757 1.492372 1
  94. 1.339746 -0.291183 -1
  95. 3.107511 0.758367 -1
  96. 2.609525 0.902979 -1
  97. 3.263585 1.367898 -1
  98. 2.912122 -0.202359 -1
  99. 1.731786 0.589096 -1
  100. 2.387003 1.573131 -1

本文涉及两部分内容:SVM和SMO。

SVM,即支持向量机,是一种二分类模型。它的基本思想是找到一个能够将两类样本分开的超平面,使得两类样本距离该超平面的最小距离最大。在推导SVM的过程中,首先需要引入拉格朗日乘子法,将约束条件转化为拉格朗日函数的形式。然后通过求解拉格朗日函数的极值来得到模型的解。具体而言,首先推导出对偶问题,然后通过求解对偶问题得到模型的解。

SMO,即序列最小优化算法,是一种用于解决SVM中拉格朗日函数的对偶问题的优化算法。SMO算法的基本思想是,将大优化问题分解为多个小优化问题,每次只优化其中的两个变量,通过依次优化这些变量来求解整个问题。具体而言,SMO算法每次选择两个变量进行更新,而其它变量不变。这两个变量可以通过启发式的方法(如最大化步长)来选择。SMO算法中用到了KKT条件,通过检查KKT条件来判断哪些变量应该参与到优化中。同时,SMO算法还利用了SMO中的启发式规则,可以大大降低求解SVM的时间。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家小花儿/article/detail/545703
推荐阅读
相关标签
  

闽ICP备14008679号