【读书笔记】机器学习实战 6.3节 SVM-简化版SMO算法
来源:互联网 发布:p正装照软件 编辑:程序博客网 时间:2024/06/01 10:37
机器学习实战
6.3节 支持向量机SVM -简化版SMO算法
#!/usr/bin/python# -*- coding: utf-8 -*-import randomfrom numpy import *def loadDataSet(fileName): # 打开文件,返回特征列表,标签列表 dataMat = [];labelMat = [] fr = open(fileName) for line in fr.readlines(): lineArr = line.strip().split('\t') dataMat.append([float(lineArr[0]),float(lineArr[1])]) # 特征 labelMat.append(float(lineArr[2])) # 标签 return dataMat,labelMatdef selectJrand(i,m): # 随机选择函数:返回选择索引 j = i while (j==i): j = int(random.uniform(0,m)) return jdef clipAlpha(aj,H,L): # 限制aj范围 if aj>H: aj = H if L>aj: aj =L return ajdef smoSimple(dataMatIn, classLabels, C, toler, maxIter): # 简化版smo算法 ,输入:样本列表,标签列表,C,容错率,最大迭代次数 dataMatrix = mat(dataMatIn); labelMat = mat(classLabels).transpose() # 样本列表,标签列表转化为numpy矩阵 b = 0; m,n = shape(dataMatrix) alphas = mat(zeros((m,1))) # alpha向量初始化为0 iter = 0 # alpha保持没有改变状态的迭代次数统计 while (iter < maxIter): alphaPairsChanged = 0 # flag:记录alpha是否被优化 for i in range(m): # 对每个样本数据 tmp_1 = multiply(alphas,labelMat) tmp_2 = multiply(tmp_1,dataMatrix) tmp_3 = tmp_2*dataMatrix[i,:].T fXi = tmp_3.T*mat(ones((m,1))) + b # 模型计算f(Xi) fXi = float(multiply(alphas,labelMat).T*(dataMatrix*dataMatrix[i,:].T)) + b # 预测样本类别f(Xi),判定是否可以优化 Ei = fXi - float(labelMat[i]) # 判断样本i误差Ei if checks if an example violates KKT conditions if ((labelMat[i]*Ei < -toler) and (alphas[i] < C)) or ((labelMat[i]*Ei > toler) and (alphas[i] > 0)): # 若误差较大,且alpha没有到边界 j = selectJrand(i,m) # 随机选择另一个要优化的alpha fXj = float(multiply(alphas,labelMat).T*(dataMatrix*dataMatrix[j,:].T)) + b # 预测f(Xj) Ej = fXj - float(labelMat[j]) # 判断样本j误差Ej alphaIold = alphas[i].copy(); alphaJold = alphas[j].copy(); # 深拷贝oldalpha值 # SMO算法参考:http://blog.csdn.net/luoshixian099/article/details/51227754 if (labelMat[i] != labelMat[j]): # 参考 http://staff.ustc.edu.cn/~ketang/PPT/PRLec5.pdf L = max(0, alphas[j] - alphas[i]) H = min(C, C + alphas[j] - alphas[i]) else: L = max(0, alphas[j] + alphas[i] - C) H = min(C, alphas[j] + alphas[i]) if L==H: print "L==H" continue eta = 2.0 * dataMatrix[i,:]*dataMatrix[j,:].T - dataMatrix[i,:]*dataMatrix[i,:].T - dataMatrix[j,:]*dataMatrix[j,:].T # 二阶导数 计算alphas[j]的最优修改量 if eta >= 0: print "eta>=0" continue alphas[j] -= labelMat[j]*(Ei - Ej)/eta # 修改alphas[j]的值 alphas[j] = clipAlpha(alphas[j],H,L) # 限制alphas[j]的值 if (abs(alphas[j] - alphaJold) < 0.00001): # 判定alphas[j]是否轻微改变 print "j not moving enough" continue alphas[i] += labelMat[j]*labelMat[i]*(alphaJold - alphas[j]) # 相反方向修改alphas[i],改变的大小一样 update i by the same amount as j #the update is in the oppostie direction b1 = b - Ei- labelMat[i]*(alphas[i]-alphaIold)*dataMatrix[i,:]*dataMatrix[i,:].T - labelMat[j]*(alphas[j]-alphaJold)*dataMatrix[i,:]*dataMatrix[j,:].T # 确定偏移项b b2 = b - Ej- labelMat[i]*(alphas[i]-alphaIold)*dataMatrix[i,:]*dataMatrix[j,:].T - labelMat[j]*(alphas[j]-alphaJold)*dataMatrix[j,:]*dataMatrix[j,:].T if (0 < alphas[i]) and (C > alphas[i]): b = b1 elif (0 < alphas[j]) and (C > alphas[j]): b = b2 else: b = (b1 + b2)/2.0 alphaPairsChanged += 1 # 更新修改flag print "iter: %d i:%d, pairs changed %d" % (iter,i,alphaPairsChanged) if (alphaPairsChanged == 0): # 迭代统计 iter += 1 else: iter = 0 print "iteration number: %d" % iter return b,alphasdataArr,labelArr = loadDataSet('testSet.txt')b,alphas = smoSimple(dataArr,labelArr,0.6,0.001,40)tmp = shape(alphas[alphas>0])for i in range(100): if alphas[i]>0.0: print dataArr[i],labelArr[i]pass
0 1
- 【读书笔记】机器学习实战 6.3节 SVM-简化版SMO算法
- py2.7 : 《机器学习实战》 SVM支持向量机:1.26号 6-1 SMO算法简化版
- 【机器学习算法-python实现】svm支持向量机(2)—简化版SMO算法
- 《机器学习实战》(六)—— SVM(SMO算法)
- 机器学习实战SMO算法源码解析
- Python3:《机器学习实战》之支持向量机(2)简化版SMO
- 深入浅出机器学习之支持向量机SVM(SMO算法)
- 【机器学习笔记】SVM part2: 核函数与SMO算法
- SVM实现(简化版SMO)
- 机器学习实战【7】(SMO算法实现)
- 机器学习实战(6):SVM-SMO-核函数 手写识别
- 深入解析python版SVM源码系列(二)——简化版SMO算法
- 《机器学习实战》读书笔记之利用PCA来简化数据
- 机器学习实战--svm
- SVM学习笔记(五)---SMO算法
- 机器学习第八课——SVM(3)核函数与SMO算法
- 机器学习经典算法详解及Python实现--基于SMO的SVM分类器
- Andrew Ng机器学习笔记+Weka相关算法实现(六)SMO/LibSVM/SVM参数
- ORACLE事物隔离级别
- Palindrome Linked List
- [Leetcode] 150. Evaluate Reverse Polish Notation 解题报告
- 第一行代码第二版(郭霖著)笔记之第六章(详解持久化技术)
- BZOJ1585 Earthquake Damage 2 地震伤害
- 【读书笔记】机器学习实战 6.3节 SVM-简化版SMO算法
- 通过java.net.URLConnection发送HTTP请求的方法
- linux route cache相关变迁
- Android AppCompatActivity去掉标题栏,全屏
- Sqlserver命令备份恢复数据库
- 对一些 关键词、语句、函数、专业名词 等的解释和理解(一)
- SpringMVC+Html+jquery_easyui做上传文件,带进度条
- grunt压缩js和css文件(1)
- qt之图形视图框架(上)