#!usr/bin/env python
#-*- encoding:utf-8 -*-
from numpy import *
#创建数据
def loadSimpleData():
datMax = matrix(
[[1.0,2.1],
[2.0,1.1],
[1.3,1.0],
[1.0,1.0],
[2.0,1.0]]
)
classLables = [1.0,1.0,-1.0,-1.0,1.0]
return datMax,classLables
dataMax,classLables = loadSimpleData()
#单层决策胡生成函数
def stumpClassify(dataMatrix,dimen,threshVal,threshIneq):
retArray = ones((shape(dataMatrix)[0],1))
if threshIneq == 'lt':
retArray[dataMatrix[:,dimen] <= threshVal] = -1.0
else:
retArray[dataMatrix[:,dimen] > threshVal] = -1.0
return retArray
#datArr,labelArr = loadSimpleData()
def buildStump(dataArr,classLabels,D):
dataMatrix = mat(dataArr)
labelMat = mat(classLables).T
m,n = shape(dataMatrix)
numSteps = 10.0
bestStump = {}
bestClasEst = mat(zeros((m,1)))
minError = inf
for i in range(n):
rangeMin = dataMatrix[:,i].min()
rangeMax = dataMatrix[:,i].max()
stepSize = (rangeMax - rangeMin) / numSteps
for j in range(-1,int(numSteps)+1):
for inequal in ['lt','gt']:
threshVal = (rangeMin + float(j) * stepSize)
predictedVals = stumpClassify(dataMatrix,i,threshVal,inequal)
errArr = mat(ones((m,1)))
errArr[predictedVals == labelMat] = 0
weightedError = D.T * errArr
#print "split: dim %d, thresh %.2f,thresh inequal:%s,the weighted error is %.3f" % (i,threshVal,inequal,weightedError)
if weightedError < minError:
minError = weightedError
bestClasEst = predictedVals
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)
aggClassEst = mat(zeros((m,1)))
for i in range(numIt):
bestStump,error,classEst = buildStump(dataArr,classLables,D)
print "D:",D.T
alpha = float(0.5*log(1.0-error)/max(error,1e-16))
bestStump['alpha'] = alpha
weakClassArr.append(bestStump)
print "classEst: ",classEst.T
expon = multiply(-1*alpha*mat(classLables).T,classEst)
D = multiply(D,exp(expon))
D = D / D.sum()
aggClassEst += alpha*classEst
print "aggClassEst: ",aggClassEst.T
aggErrors = multiply(sign(aggClassEst) != mat(classLables).T,ones((m,1)))
errorRate = aggErrors.sum() / m
print "total error: ",errorRate,"\n"
if errorRate == 0.0:break
#return weakClassArr
return weakClassArr
def adaClassify(datToClass,classifierArr):
dataMatrix = mat(datToClass)
m = shape(dataMatrix)[0]
aggClassEst = mat(zeros((m,1)))
for i in range(len(classifierArr)):
classEst = stumpClassify(dataMatrix,classifierArr[i]['dim'],classifierArr[i]['thresh'],classifierArr[i]['ineq'])
aggClassEst += classifierArr[i]['alpha']*classEst
print aggClassEst
return sign(aggClassEst)
#ROC曲线的控制及AUC计算函数
def plotROC(predStrenghts,classLabels):
import matplotlib.pyplot as plt
cur = (1.0,1.0)
ySum = 0.0
numPosCLas = sum(array(classLabels) == 1.0)
yStep = 1 / float(numPosCLas)
xStep = 1 / float(len(classLabels) - numPosCLas)
sortedIndicies = predStrenghts.argsort()
fig = plt.figure()
fig.clf()
ax = plt.subplot(111)
for index in sortedIndicies.tolist()[0]:
if classLabels[index] == 1.0:
delX = 0
delY = yStep
else:
delX = xStep
delY = 0
ySum += cur[1]
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.ylabel("True Positive Rate")
plt.title("ROC curve for AdaBoost Horse Colic Detection System")
ax.axis([0,1,0,1])
plt.show()
print "this Ares Under the Curve is: ",ySum*xStep
#D = mat(ones((5,1)) / 5)
# print buildStump(dataMax,classLables,D)
#datArr,labelArr = loadSimpleData()
dataMax,classLables = loadSimpleData()
classifierArr = adaBoostTrainDS(dataMax,classLables,30)
adaClassify([5,5],classifierArr)
adaboost分类算法
最新推荐文章于 2024-03-17 20:24:20 发布