AdaBoost algorithm Implementation

Source: Internet
Author: User

#-*-coding:utf-8-*-# ---------------------------------------------------------------------------#adaboost.py#Created on:2014-06-12 09:49:56.00000#Description:# ---------------------------------------------------------------------------ImportSYSImportMathImportNumPy as Npbreakvalues= (2.5, 5.5, 8.5) X= Np.array ([0,1,2,3,4,5,6,7,8,9]) Y= Np.array ([1,1,1,-1,-1,-1,1,1,1,-1]) W1= Np.array ([0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1])    defClassifier25 (x):ifX <= 2.5:        return1Else:        return-1defClassifier55 (x):ifX >= 5.5:        return1Else:        return-1defClassifier85 (x):ifX <= 8.5:        return1Else:        return-1defClassifyarray (Xarray, Classifier): YY= []     forXinchXArray:YY.append (Classifier (x))Print(YY)returnYYdeferrorsum (YY): I=0 Errorvalue=0;  forYinchYY:ifY! =Y[i]: Errorvalue+=W1[i] I= I+1returnErrorvaluedeferrorallsum (Expressarray): I=0 Errorvalue=0;  forXinchX:value=0 forExpressinchExpressarray:value+ = express[0] * express[1] (x)ifValue >0:value= 1Else: Value=-1ifValue! =Y[i]: Errorvalue+ = 0.1I= I+1returnErrorvaluedefselectclassifierfunction (Xarray): Classifierarray=[Classifier25, Classifier55, Classifier85] Errarray=[] Value= Float ('NaN') Errmin= Float ('INF')     forClassifierinchClassifierarray:#calculate the result value of a classificationYY =Classifyarray (Xarray, classifier)#calculate the error rate for a classificationErrorvalue =errorsum (YY) errarray.append (errorvalue)ifErrorvalue <Errmin:errmin=Errorvalue Value=classifierPrint(Errarray)Print(value.)__name__)    returnvaluePrint(W1)" "Print ('--------------------------------') classifier = selectclassifierfunction (x) #计算分类的结果值G = Classifyarray (x, Classifier) #计算分类的错误率e = Errorsum (g) A = 0.5 * Math.log ((1-E)/e) A = Round (A, 4) print (a) W2 = W1*np.exp (-a*y*np.array (g)) Zm = Np.sum (W2) #Zm = Round (ZM, 4) print (ZM) W1 = W2/zmprint (W1) print ('--------------------------------') W1 = Np.array ([ 0.0715,0.0715,0.0715,0.0715,0.0715,0.0715,0.1666,0.1666,0.1666,0.07151]) classifier = selectclassifierfunction (X) #计算分类的结果值G = Classifyarray (X, classifier) #计算分类的错误率e = Errorsum (G) A = 0.5 * Math.log ((1-E)/e) A = Round (A, 4) print (a) W2 = W1 *np.exp (-a*y*np.array (G)) Zm = Np.sum (W2) #Zm = Round (ZM, 4) print (ZM) W1 = W2/zmprint (W1) print ('------------------------ --------') W1 = Np.array ([0.0455, 0.0455, 0.0455, 0.1667, 0.1667, 0.01667, 0.1060, 0.1060, 0.1060, 0.0455]) classifier = Sel Ectclassifierfunction (x) #计算分类的结果值G = Classifyarray (x, classifier) #计算分类的错误率e = Errorsum (G) A = 0.5 * Math.log ((1-E)/e) A = R Ound (A, 4) print (a) W2 = W1*np.exp (-a*y*np.array (G)) Zm = NP. SUM (W2) #Zm = Round (ZM, 4) print (ZM) W1 = W2/zmprint (W1) " "Errorall= 100Expressarray= [] whileErrorall > 0.1:    Print('--------------------------------') Classifier=selectclassifierfunction (X)#calculate the result value of a classificationG =Classifyarray (X, classifier)#calculate the error rate for a classificationE =Errorsum (G) a= 0.5 * Math.log ((1-E)/e) A= Round (A, 4)    Print('A:'+Str (a)) W2= W1*np.exp (-a*y*Np.array (G)) Zm=np.sum (W2)#ZM = Round (ZM, 4)    Print(Zm)Print('Zm:'+str (ZM)) W1= W2/ZmPrint('W1:'+str (W1)) Expressarray.append ([a,classifier]) Errorall=errorallsum (Expressarray)Print('Errorall:'+str (errorall)) expressstring='G (x) = sign ('I=0 forExpressinchExpressarray:ifi >0:expressstring+=' + 'expressstring+ = str (express[0]) +' * '+ express[1].__name__+'(x)'I+ = 1expressstring+=' )'Print('--------------------------------')Print('The classification function is: \ n'+expressstring)Print('--------------------------------')

AdaBoost Algorithm Implementation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.