首页 今日头条正文

广告词,机器学习—AdaBoost算法(手稿+代码),酷播

机器学习 — AdaBoost算法(手稿+代码)

阅览原文

一.Adaboost理论部分

1.1 adaboost运转进程

注释:算法是运用指数函数下降差错,运转进程经过迭代进行。其间函数的算法怎样来的,你不必知道!当然你也能够测验运用其它的函数替代指数函数,看看作用怎样。

1.2 举例阐明算法流程

略,花几分钟就能够看懂的比如。见:《计算学习方法》李航大大

1.3 算法差错界的证明

注释:差错的胡武帅上边界由Zm束缚,可是Zm又是由Gm(xi)束缚,所以挑选恰当的Gm(xi)能够加速差错的减小。

二.代码完成

注释:这儿参阅大神博客http://blog.csdn.net/guyuealian/article/details/70995333,举比如很具体。

2.1程序流程图

2.2根本程序完成

注释:真是倒运玩意,原本代码悉数注释好了,忽然Ubuntu奔溃了,悉数程序就GG了。。。下面的代码便是官网的代码,部分补上注释。现在运用Deepin桌面版了,其它方面都比Ubuntu好,可是有点点卡。


from numpy import *
def loadDataSet(fileName): #general function to parse tab -delimited floats
numFeat = len(open(fileName).readline().split('\t')) #get number of fields
dataMat = []; labelMat = []
fr = open(fileName)
for line gugoolin fr.readlines():
lineArr =[]
curLine = line.strip().split('\t')
for i in range(numFeat-1):
lineArr.append(float(curLine[i]))
data薛雷扫北电视剧全集Mat.append(lineArr)
labelMat.append(float(curLine[-1]))
return d广告词,机器学习—AdaBoost算法(手稿+代码),酷播ataMat,labelMat
def stumpClassify(dataMatrix,dimen,threshVal,threshIneq):#just classify the data
retArray = ones((shape(dataMatrix)[0],1))
if threshIneq == 'lt':
retArray[dataMatrix[:,dimen] <= threshVal] = -1.0
else:
retArray[dataMatrix[:,dimen] > threshVal] = -1.0
return retArray

def buildStump(dataArr,classLabels,D):
dataMatrix = mat(dataArr); labelMat = mat(classLabels).T
m,n = sha光头强运送配备pe(dataMatrix)
numSteps = 10.0; bestStump = {}; bestClasEst = mat(zeros((m,1)))
minError = inf #广告词,机器学习—AdaBoost算法(手稿+代码),酷播init error sum, to +infinity
for i in range(n):#loop over all dimensions
rangeMin = dataMatrix[:,i].min(); rangeMax主婚词简略经典 = dataMatrix[:,i].max();
stepSize = (rangeMax-rangeMin)/numSteps
for j in range(-1,int(numSteps)+1):#loop over all range in current dimension
for inequal in ['lt', 'gt']: #go over less than and greate光之美少女剧场版心之朋友r than
threshVal = (rangeMin + float(j) * stepSize)
predictedVals = stumpClassify(dataMatrix,i,threshVal,inequal)#call stump classify with i, j, lessThan
errArr = mat(ones((m,1)))
errArr[predictedVals == labelM碉堡浴血战at] = 0
weightedError = D.T*errArr #calc total error multiplied by D
#print "split: dim %d, thresh谷露 %.2f, thresh ineqal: %s, the weighted error is %.3f" % (i, threshVal, inequal, weightedError)
if weightedError < minError:
mi关照妇nError = weightedError
bestClasEst = predictedVals.copy()
bestStump['dim'] = i
bestStump['thresh'] = threshVal下堂王妃值千金
bestStump['ineq'] = inequal
return bestStump,minError,bestClasEst
def adaBoostTrainDS(dataArr,classLabels,numIt=40):
weakClassArr = []
m = shape(dataArr)[0]
D = mat(ones((m,1))/m) #init D to all equal
aggClassEst = mat(zeros((m,1)))
for i in range(numIt):
bestStump,error,classEst = buil广告词,机器学习—AdaBoost算法(手稿+代码),酷播dStump(怎样办惹尘土dataArr,classLabels,D)#build Stump
#print "D:",D.T
alpha = float(0.5*log((1.0-error)/max(error,1e-16)))#calc alpha, throw in max(error锁阴,eps) to account f山东志广世纪集团or error=0
bes广告词,机器学习—AdaBoost算法(手稿+代码),酷播tStump['alpha'] = alpha
weakClassArr.append(bestStump) #store Stump Params in Array
#print "classEst: ",classEst.T
expon = multiply(-1*alpha*mat(classLabels).T,classEst) #exponent for D calc, getting messy
D = mul广告词,机器学习—AdaBoost算法(手稿+代码),酷播tiply(D,exp(expon)) #Calc New D for next iteration
D = D/D.sum()
#calc training error of all classifiers, if this is 0 quit for loop early (use break)
aggClassEst += alpha*classEst
#print "aggClassEst: ",aggClassEst.T
aggErrors = multiply(sign(aggClassEst) != mat(classLabels).T,ones((m,1)))
errorRate = aggErrors.sum()/m
print ("total error: ",errorRate)
if errorRate == 0.0: break
return weakClassArr,aggClassEst
def adaClassify(datToClass,classifierArr):
dataMatrix = mat(datToClass)#do stuff similar to last aggClassEst in adaBoostTrainDS
m = shape(dataMatrix)[0]
aggClass女仆体系Est = mat(zeros((m,1)))
for i in range(len(classifierArr)):
classEst = stumpClassify(dataMatrix,classifierArr[i]['dim'],\
classifierArr[i]['thresh'],\
classifierArr[i]['ineq'])#call stump classify
aggClassEst += classifierArr[i]['alpha']*classEst
#print aggClassEst
return sign(aggClassEst)
def plotROC(predStrengths, classLa杨玉娣bels):
import matplotlib.pyplot as plt
cur = (1.0,1.0) #cursor
ySum = 0.0 #variable to calculate AUC
numPosClas = sum(array(classLabels)==1.0)#标签等于1的和(也等于个数)
yStep = 1/float(numPosClas); xStep = 1/float(len(classLabels)-numPosClas)
飞飞btsortedIndicies = predStrengths.argsort()#get sorted index, it's reverse
sortData = sorted(predStrengths.tolist()[0])
fig = plt.figure()
fig.clf()
ax = pl仓本t.subplot(111)
#loop through all the values, drawing a line segment at each point
for index in sortedIndicies.tolist(广告词,机器学习—AdaBoost算法(手稿+代码),酷播)[0]:
if classLabels[index] == 1.0:
delX = 0; delY = yStep;
else:
delX = xStep; de广告词,机器学习—AdaBoost算法(手稿+代码),酷播lY = 0;
ySum += cur[1]
#draw line from cur to (cur[0]-delX,cur[1]-delY)
ax.plot([cur[0],cur[0]-delX],[cur[1],cur[1]-delY], c='b')
cur = (cur[0]-delX,cur[1]-delY)
ax.plot([0,1],[0,1],'b唐山地震七大疑团--')
plt.xlabel('False positive rate'); plt.y金刚之子label('True positive rate')
plt.title('ROC curve for AdaBoost horse colic detection system')
ax.axis([0,1,0,1])
plt.show()
print ("the Area Under the Curve is: ",ySum*xStep)
仿制代码

注释:要点阐明一下非均衡分类的图画制作问题,想了好久才想了解!

都是相对而言的,其间本文说的曲线在左上方就为好,也是相对而言的,看你怎样界说个了解!

相关阅览: 十大经典数据发掘算法——PageRank参阅文献:K码农-http://kmanong.top/kmn/qxw/form/home?top_cate=28

版权声明

本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。