image.png
    image.png
    SMO算法的工作原理是:每次循环中选择两个alpha进行优化处理。一旦找到了一对合适的alpha,那么就增大其中一个同时减小另一个。这里所谓的”合适”就是指两个alpha必须符合以下两个条件,条件之一就是两个alpha必须要在间隔边界之外,而且第二个条件则是这两个alpha还没有进进行过区间化处理或者不在边界上。

    1. import matplotlib.pyplot as plt
    2. import numpy as np
    3. def loadDataSet(fileName):
    4. dataMat = []; labelMat = []
    5. fr = open(fileName)
    6. for line in fr.readlines(): #逐行读取,滤除空格等
    7. lineArr = line.strip().split('\t')
    8. dataMat.append([float(lineArr[0]), float(lineArr[1])]) #添加数据
    9. labelMat.append(float(lineArr[2])) #添加标签
    10. return dataMat,labelMat
    11. def showDataSet(dataMat, labelMat):
    12. data_plus = [] #正样本
    13. data_minus = [] #负样本
    14. for i in range(len(dataMat)):
    15. if labelMat[i] > 0:
    16. data_plus.append(dataMat[i])
    17. else:
    18. data_minus.append(dataMat[i])
    19. data_plus_np = np.array(data_plus) #转换为numpy矩阵
    20. data_minus_np = np.array(data_minus) #转换为numpy矩阵
    21. plt.scatter(np.transpose(data_plus_np)[0], np.transpose(data_plus_np)[1]) #正样本散点图
    22. plt.scatter(np.transpose(data_minus_np)[0], np.transpose(data_minus_np)[1]) #负样本散点图
    23. plt.show()
    24. if __name__ == '__main__':
    25. dataMat, labelMat = loadDataSet('testSet.txt')
    26. showDataSet(dataMat, labelMat)

    支持向量机 - 图3

    1. def loadDataSet(fileName):
    2. dataMat = []; labelMat = []
    3. fr = open(fileName)
    4. for line in fr.readlines(): #逐行读取,滤除空格等
    5. lineArr = line.strip().split('\t')
    6. dataMat.append([float(lineArr[0]), float(lineArr[1])]) #添加数据
    7. labelMat.append(float(lineArr[2])) #添加标签
    8. return dataMat,labelMat
    9. def selectJrand(i, m):
    10. j = i #选择一个不等于i的j
    11. while (j == i):
    12. j = int(random.uniform(0, m))
    13. return j
    14. def clipAlpha(aj,H,L):
    15. if aj > H:
    16. aj = H
    17. if L > aj:
    18. aj = L
    19. return aj
    20. def smoSimple(dataMatIn, classLabels, C, toler, maxIter):
    21. #转换为numpy的mat存储
    22. dataMatrix = np.mat(dataMatIn); labelMat = np.mat(classLabels).transpose()
    23. #初始化b参数,统计dataMatrix的维度
    24. b = 0; m,n = np.shape(dataMatrix)
    25. #初始化alpha参数,设为0
    26. alphas = np.mat(np.zeros((m,1)))
    27. #初始化迭代次数
    28. iter_num = 0
    29. #最多迭代matIter次
    30. while (iter_num < maxIter):
    31. alphaPairsChanged = 0
    32. for i in range(m):
    33. #步骤1:计算误差Ei
    34. fXi = float(np.multiply(alphas,labelMat).T*(dataMatrix*dataMatrix[i,:].T)) + b
    35. Ei = fXi - float(labelMat[i])
    36. #优化alpha,更设定一定的容错率。
    37. if ((labelMat[i]*Ei < -toler) and (alphas[i] < C)) or ((labelMat[i]*Ei > toler) and (alphas[i] > 0)):
    38. #随机选择另一个与alpha_i成对优化的alpha_j
    39. j = selectJrand(i,m)
    40. #步骤1:计算误差Ej
    41. fXj = float(np.multiply(alphas,labelMat).T*(dataMatrix*dataMatrix[j,:].T)) + b
    42. Ej = fXj - float(labelMat[j])
    43. #保存更新前的aplpha值,使用深拷贝
    44. alphaIold = alphas[i].copy(); alphaJold = alphas[j].copy();
    45. #步骤2:计算上下界L和H
    46. if (labelMat[i] != labelMat[j]):
    47. L = max(0, alphas[j] - alphas[i])
    48. H = min(C, C + alphas[j] - alphas[i])
    49. else:
    50. L = max(0, alphas[j] + alphas[i] - C)
    51. H = min(C, alphas[j] + alphas[i])
    52. if L==H: print("L==H"); continue
    53. #步骤3:计算eta
    54. eta = 2.0 * dataMatrix[i,:]*dataMatrix[j,:].T - dataMatrix[i,:]*dataMatrix[i,:].T - dataMatrix[j,:]*dataMatrix[j,:].T
    55. if eta >= 0: print("eta>=0"); continue
    56. #步骤4:更新alpha_j
    57. alphas[j] -= labelMat[j]*(Ei - Ej)/eta
    58. #步骤5:修剪alpha_j
    59. alphas[j] = clipAlpha(alphas[j],H,L)
    60. if (abs(alphas[j] - alphaJold) < 0.00001): print("alpha_j变化太小"); continue
    61. #步骤6:更新alpha_i
    62. alphas[i] += labelMat[j]*labelMat[i]*(alphaJold - alphas[j])
    63. #步骤7:更新b_1和b_2
    64. b1 = b - Ei- labelMat[i]*(alphas[i]-alphaIold)*dataMatrix[i,:]*dataMatrix[i,:].T - labelMat[j]*(alphas[j]-alphaJold)*dataMatrix[i,:]*dataMatrix[j,:].T
    65. b2 = b - Ej- labelMat[i]*(alphas[i]-alphaIold)*dataMatrix[i,:]*dataMatrix[j,:].T - labelMat[j]*(alphas[j]-alphaJold)*dataMatrix[j,:]*dataMatrix[j,:].T
    66. #步骤8:根据b_1和b_2更新b
    67. if (0 < alphas[i]) and (C > alphas[i]): b = b1
    68. elif (0 < alphas[j]) and (C > alphas[j]): b = b2
    69. else: b = (b1 + b2)/2.0
    70. #统计优化次数
    71. alphaPairsChanged += 1
    72. #打印统计信息
    73. print("第%d次迭代 样本:%d, alpha优化次数:%d" % (iter_num,i,alphaPairsChanged))
    74. #更新迭代次数
    75. if (alphaPairsChanged == 0): iter_num += 1
    76. else: iter_num = 0
    77. print("迭代次数: %d" % iter_num)
    78. return b,alphas
    79. def showClassifer(dataMat, w, b):
    80. #绘制样本点
    81. data_plus = [] #正样本
    82. data_minus = [] #负样本
    83. for i in range(len(dataMat)):
    84. if labelMat[i] > 0:
    85. data_plus.append(dataMat[i])
    86. else:
    87. data_minus.append(dataMat[i])
    88. data_plus_np = np.array(data_plus) #转换为numpy矩阵
    89. data_minus_np = np.array(data_minus) #转换为numpy矩阵
    90. plt.scatter(np.transpose(data_plus_np)[0], np.transpose(data_plus_np)[1], s=30, alpha=0.7) #正样本散点图
    91. plt.scatter(np.transpose(data_minus_np)[0], np.transpose(data_minus_np)[1], s=30, alpha=0.7) #负样本散点图
    92. #绘制直线
    93. x1 = max(dataMat)[0]
    94. x2 = min(dataMat)[0]
    95. a1, a2 = w
    96. b = float(b)
    97. a1 = float(a1[0])
    98. a2 = float(a2[0])
    99. y1, y2 = (-b- a1*x1)/a2, (-b - a1*x2)/a2
    100. plt.plot([x1, x2], [y1, y2])
    101. #找出支持向量点
    102. for i, alpha in enumerate(alphas):
    103. if abs(alpha) > 0:
    104. x, y = dataMat[i]
    105. plt.scatter([x], [y], s=150, c='none', alpha=0.7, linewidth=1.5, edgecolor='red')
    106. plt.show()
    107. def get_w(dataMat, labelMat, alphas):
    108. alphas, dataMat, labelMat = np.array(alphas), np.array(dataMat), np.array(labelMat)
    109. w = np.dot((np.tile(labelMat.reshape(1, -1).T, (1, 2)) * dataMat).T, alphas)
    110. return w.tolist()
    111. if __name__ == '__main__':
    112. dataMat, labelMat = loadDataSet('testSet.txt')
    113. b,alphas = smoSimple(dataMat, labelMat, 0.6, 0.001, 40)
    114. w = get_w(dataMat, labelMat, alphas)
    115. showClassifer(dataMat, w, b)

    支持向量机 - 图4