机器学习李航——Adaboost课本例题实现

Posted realwuxiong

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了机器学习李航——Adaboost课本例题实现相关的知识,希望对你有一定的参考价值。

例8.1Adaboost的例子

注意求D3或者D4的时候只需要把w替换一下就行,记得还得改阈值。这个代码算个半自动的,因为还需要手动改一下。

import numpy as np
def getA(e1):
    print("A="+str(1/2*np.log((1-e1)/e1)))
    return 1/2*np.log((1-e1)/e1)
def getZ(a,y1,y2,w1,w2):
    return np.sum(w1* np.exp(y1*(-a)))+ np.sum(w2* np.exp((-y2)*(-a)))
def getD2(a,y1,y2,w1,w2,Z):
    d1 = w1/Z*np.exp(y1*(-a))
    print("d1="+str(d1))
    d2 = w2/Z* np.exp((-y2)*(-a))
    print("d2="+str(d2))
    return np.append(d1,d2)
if __name__ == '__main__':
    data = np.array([[0,1],
                    [1,1],
                    [2,1],
                    [3,-1],
                    [4,-1],
                    [5,-1],
                    [6,1],
                    [7,1],
                    [8,1],
                    [9,-1]])
    w = np.zeros(10, dtype=float)+0.1 #初始化权重 D1
    #w = np.array([0.07142857,0.07142857,0.07142857,0.07142857,0.07142857,0.07142857,0.16666667,0.16666667,0.16666667,0.07142857])#D2
    #w = np.array([0.04545454,0.04545454,0.04545454,0.16666667,0.16666667,0.16666667,0.10606061,0.10606061,0.10606061,0.04545454])#D3
    x = data[:,0]#取x的值
    y = data[:,1]#取y的值
    v = 2.5#取阈值
    y1 = y[0:int((v//1+1))]
    y2 = y[int((v//1+1)):]
    w1 = w[0:int((v//1+1))]
    w2 = w[int((v//1+1)):]
    e1=0#误差率
    print("y1="+str(y1))
    print("y2="+str(y2))
    for i,j in enumerate(y1):
        if j < 0:
            e1 = e1+w1[i]
    for i,j in enumerate(y2):
        if j > 0:
            e1 = e1+w2[i]
    print("e1="+str(e1))
    a = getA(e1)
    z = getZ(a,y1,y2,w1,w2)
    print("Z="+str(z))
    print("D2="+str(getD2(a,y1,y2,w1,w2,z)))
    
    

以上是关于机器学习李航——Adaboost课本例题实现的主要内容,如果未能解决你的问题,请参考以下文章

Python机器学习(十七)AdaBoost原理与代码实现

AdaBoost算法

机器学习实战笔记(Python实现)-06-AdaBoost

《统计学习方法(李航)》讲义 第08章 提升方法

AdaBoost算法详解及python实现Python机器学习系列(十八)

机器学习Python实现AdaBoost