Skip to content

Instantly share code, notes, and snippets.

@Yeaseen
Created December 1, 2018 19:53
Show Gist options
  • Save Yeaseen/32df82d87d77b99e06d8a2df547ba964 to your computer and use it in GitHub Desktop.
Save Yeaseen/32df82d87d77b99e06d8a2df547ba964 to your computer and use it in GitHub Desktop.
def AdaBoost(funcDcsn, sampleFrames, attList, \
parentFrames,strtdepth, maxdepth,funcClassify,rootClass, K):
Y=sampleFrames.iloc[:,-1].values.tolist().copy()
#print(Y)
N=len(sampleFrames)
w= [1/N] * N
h= []
z= []
#print(w)
for x in range(0,K):
data=sampleFrames.sample(n=N,weights=w,replace=True).copy()
error=0.0001
attList=list(data.drop(data.columns[-1],axis=1).columns.values)
roott=dcsnTreeRoot(data,attList.copy(),data,0,maxdepth)
predictAns=classPrint(data,roott)
for i in range(0,N):
if(predictAns[i] != Y[i]):
error=error + w[i]
if(error > 0.5):
#print(x)
continue
for j in range(0,N):
if(predictAns[j] == Y[j]):
w[j] = w[j] *(error/(1-error))
maxVal=sum(w)
w = [float(i)/maxVal for i in w]
h.append(roott)
#print(type(roott))
wT= math.log2(((1-error)/error))
z.append(wT)
return h,z
learner, weights=AdaBoost(dcsnTreeRoot,dfN,att,dfN,0,1,classPrint,dcsnTreeNodeClass,20)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment