adaboost实现(五,完整代码c#)

前面的代码都是测试,这里整理后的完整代码放出来(adaboost算法真心不好写):

List<XandY> example = new List<XandY>();
            XandY temp = new XandY();
            temp.pos = new Point(1, 5);
            temp.res = 1;
            example.Add(temp);

            temp = new XandY();
            temp.pos = new Point(2, 2);
            temp.res = 1;
            example.Add(temp);

            temp = new XandY();
            temp.pos = new Point(3, 1);
            temp.res = -1;
            example.Add(temp);

            temp = new XandY();
            temp.pos = new Point(4, 6);
            temp.res = -1;
            example.Add(temp);

            temp = new XandY();
            temp.pos = new Point(6, 8);
            temp.res = 1;
            example.Add(temp);

            temp = new XandY();
            temp.pos = new Point(6, 5);
            temp.res = -1;
            example.Add(temp);

            temp = new XandY();
            temp.pos = new Point(7, 9);
            temp.res = 1;
            example.Add(temp);

            temp = new XandY();
            temp.pos = new Point(8, 7);
            temp.res = 1;
            example.Add(temp);

            temp = new XandY();
            temp.pos = new Point(9, 8);
            temp.res = -1;
            example.Add(temp);

            temp = new XandY();
            temp.pos = new Point(10, 2);
            temp.res = -1;
            example.Add(temp);

         
            List<int> 记录错误次数 = new List<int>();
            //遍历所有,找到所有弱分类器,优选其中分组错误次数最少的,3组分错3次,11组分错4次,其他超过5次犯错,
           // 我们还没有遍历犯错4次的,强分类器已经形成
            for (int i = 0; i < 10; i++)
            {
                float tempx1 = i + 1.5f;

                int 计数正 = 0; int 计数负 = 0;
                for (int j = 0; j < example.Count; j++)
                {
                    int temppos = example[j].pos.X;
                  
                    if (tempx1 > temppos && example[j].res == 1)                  
                    {//=1
                        计数正++;
                    }
                    if (tempx1 < temppos && example[j].res == -1)                      
                        计数负++;//=-1
                }
                int recEr = 10 - 计数正 - 计数负;
                记录错误次数.Add(recEr);            

            }
          //第一种
            for (int i = 0; i < 10; i++)
            {
                float tempx1 = i + 1.5f;

                int 计数正 = 0; int 计数负 = 0;
                for (int j = 0; j < example.Count; j++)
                {
                    int temppos = example[j].pos.Y;

                    if (tempx1 < temppos && example[j].res == 1)
                    {//=1
                        计数正++;
                    }

                    //=-1
                    if (tempx1 > temppos && example[j].res == -1)
                        计数负++;


                }
                int recEr = 10 - 计数正 - 计数负;
                记录错误次数.Add(recEr);
            }  //第二种
            ///
            for (int i = 0; i < 10; i++)
            {
                float tempx1 = i + 1.5f;

                int 计数正 = 0; int 计数负 = 0;
                for (int j = 0; j < example.Count; j++)
                {
                    int temppos = example[j].pos.X;

                    if (tempx1 < temppos && example[j].res == 1)
                    {//=1
                        计数正++;
                    }

                    //=-1
                    if (tempx1 > temppos && example[j].res == -1)
                        计数负++;

                }
                int recEr = 10 - 计数正 - 计数负;
                记录错误次数.Add(recEr);
            }  //第三种
            //
            for (int i = 0; i < 10; i++)
            {
                float tempx1 = i + 1.5f;

                int 计数正 = 0; int 计数负 = 0;
                for (int j = 0; j < example.Count; j++)
                {

                    int temppos = example[j].pos.Y;
                    if (tempx1 > temppos && example[j].res == 1)

                    {//=1
                        计数正++;
                    }
                    if (tempx1 < temppos && example[j].res == -1)

                        计数负++;//=-1
                }
                int recEr = 10 - 计数正 - 计数负;
                记录错误次数.Add(recEr);
            }  //第四种
            ///找出分错次数最少的在所有四种分类中
            int minscore = 10;
            for (int i = 0; i < 记录错误次数.Count; i++)
            {
                if (记录错误次数[i] < minscore)
                {
                    minscore = 记录错误次数[i];
                }
            }
            List<Point> minindex = new List<Point>();
            List<Point> minindexplus = new List<Point>();
            for (int j = 0; j < 记录错误次数.Count; j++)
            {
                //0-9,x<分界,=1;10-19,y>分界,=1;20-39,x>分界,=1;30-39,y<分界,=1
                if (记录错误次数[j] == minscore)//分界=index+1.5
                { minindex.Add(new Point(j,minscore)); }
                if (记录错误次数[j] == minscore + 1)
                { minindexplus.Add(new Point(j, minscore+1)); }
            }
            先使用minindex
            List<float[]> suoyou犯错低弱分类器minindex = new List<float[]>();
            List<float[]> suoyou犯错低弱分类器minindexPlus = new List<float[]>();
            for (int i = 0; i < minindex.Count; i++)
            {
                float[] temparr = 根据序号产生分类器改进(minindex[i], example);
                suoyou犯错低弱分类器minindex.Add(temparr);
            }//找到3组
            ///备用minindexplus
            for (int i = 0; i < minindexplus.Count; i++)
            {
                float[] temparr = 根据序号产生分类器改进(minindexplus[i], example);
                suoyou犯错低弱分类器minindexPlus.Add(temparr);
            }//找到11组
             ///
            float[] 最初的标签样本 = new float[] { 1, 1, -1, -1, 1, -1, 1, 1, -1, -1 };
            float[] dataP权重 = new float[] { 0.1f, 0.1f, 0.1f, 0.1f, 0.1f, 0.1f, 0.1f, 0.1f, 0.1f, 0.1f };//10组数据,1/10=0.1   
            double[] gengxin = new double[10];
            bool hii = false;
            for (int m = 0; m < suoyou犯错低弱分类器minindex.Count; m++)
            {
                List<PointF> xhqz = 找出分错的序号和权重(suoyou犯错低弱分类器minindex[m], 最初的标签样本, dataP权重);
                float cuowu总和 = 0;
                for (int i = 0; i < xhqz.Count; i++)
                {
                    cuowu总和 = cuowu总和 + xhqz[i].Y;
                }
                dataP权重 = 返回迭代后权重值(suoyou犯错低弱分类器minindex[m], 最初的标签样本, dataP权重, cuowu总和);
                double 系数 = 0.5 * Math.Log((1 - cuowu总和) / cuowu总和);

                float[] tempjieguo = new float[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
                for (int i = 0; i < 10; i++)
                {
                    double sb = 系数 * suoyou犯错低弱分类器minindex[m][i];
                    gengxin[i] = gengxin[i] + sb;
                    if (gengxin[i] > 0)
                        tempjieguo[i] = 1;
                    if (gengxin[i] < 0)
                        tempjieguo[i] = -1;
                    if (gengxin[i] == 0)
                        tempjieguo[i] = 0;
                }
                //
                hii = 判断是否成功升级为强分类器(tempjieguo, 最初的标签样本);
            }
            //如果hii==false,则进行备用minindexplus11组(犯错4),犯错3的3组弱分类器已经用完。202306211605

代码到此结束,以下是其中几个函数封装

   public bool 判断是否成功升级为强分类器(float[] A, float[] B)
        {
          
            for (int i = 0; i < 10; i++)
            {
                if (A[i] * B[i] == -1.0f)
                {
                    return false;
                }
            }
            return true;
        }
        public float[] 返回迭代后权重值(float[] 第n个弱分类器jieguo, float[] 最初的标签jieguo, float[] 迭代前权重值, float 错误总和)
        {
            //以上两个数组相乘,ok为1,ng为-1,
            //1,2,9分错值为-1,数组序号0,1,8
            //第一个弱分类器的错误率0.16*3=0.48
            //第二个弱分类器的错误率0.07*3=0.21,0.21<0.48,所以选第二个弱分类器作为第二次迭代,并且更新dataP。
            float[] 迭代后权重 = new float[10];
            for (int j = 0; j < 10; j++)
            {
                if (第n个弱分类器jieguo[j] * 最初的标签jieguo[j] == -1.0f)
                {
                    //分错的变大
                    迭代后权重[j] = 迭代前权重值[j] / (2 * (错误总和));
                }
                if (第n个弱分类器jieguo[j] * 最初的标签jieguo[j] == 1.0f)
                { //分对的变小
                    迭代后权重[j] = 迭代前权重值[j] / (2 * (1 - 错误总和));
                }
            }
            return 迭代后权重;
        }
        public List<PointF> 找出分错的序号和权重(float[] A, float[] B, float[] quanzhong)
        {
            List<PointF> 序号权重 = new List<PointF>();
            for (int i = 0; i < 10; i++)
            {
                if (A[i] * B[i] == -1.0f)
                {
                    PointF temp = new PointF(i, quanzhong[i]);
                    序号权重.Add(temp);
                }
            }
            return 序号权重;
        }
        public float[] 根据序号产生分类器改进(Point indexAndVal, List<XandY> yangben)//样本有十个
        {//一共分四类
            //先判断类别
            int index = indexAndVal.X;
            float[] linshi弱分类标签 = new float[10];

            if (index >= 0 && index <= 9)
            { 
                for (int i = 0; i < 10; i++)
                {
                    int temppos = yangben[i].pos.X;//0-9,x<分界,=1;
                    if (index + 1.5f > temppos)//保持一致,用值不好,可以用序号,在list查找
                    { linshi弱分类标签[i] = 1; }
                    else
                    { linshi弱分类标签[i] = -1; }                   
                }              
            }
            if (index >= 10 && index <= 19)//10-19,y>分界,=1;
            {
                for (int i = 0; i < 10; i++)
                {
                    int temppos = yangben[i].pos.Y;
                    if (index + 1.5f-10 < temppos)//保持一致,用值不好,可以用序号,在list查找
                    { linshi弱分类标签[i] = 1; }
                    else
                    { linshi弱分类标签[i] = -1; }
                }
            }
            if (index >= 20 && index <= 29)//20-39,x>分界,=1;
            {
                for (int i = 0; i < 10; i++)
                {
                    int temppos = yangben[i].pos.X;
                    if (index + 1.5f - 20 < temppos)//保持一致,用值不好,可以用序号,在list查找
                    { linshi弱分类标签[i] = 1; }
                    else
                    { linshi弱分类标签[i] = -1; }
                }
            }
            if (index >= 30 && index <= 39)//30-39,y<分界,=1
            {
                for (int i = 0; i < 10; i++)
                {
                    int temppos = yangben[i].pos.Y;
                    if (index + 1.5f - 30 > temppos)//保持一致,用值不好,可以用序号,在list查找
                    { linshi弱分类标签[i] = 1; }
                    else
                    { linshi弱分类标签[i] = -1; }
                }
            }
            return linshi弱分类标签;
        }

贴上代码,也是自己以后备用。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Adaboost算法的原理和代码实现如下: 1. 初始化训练数据的权重,使每个样本的权重相等。 2. 对于每个弱分类器: a. 根据当前样本权重训练一个弱分类器。 b. 计算该弱分类器的错误率。 c. 根据错误率计算该弱分类器的权重。 d. 更新样本权重,增加被错误分类的样本的权重,减少被正确分类的样本的权重。 3. 将所有弱分类器的权重线性组合,得到最终的强分类器。 具体的代码实现可以参考引用\[1\]中的链接,其中提供了Adaboost算法代码实例。这段代码会根据训练数据和弱分类器的选择,实现Adaboost算法的原理。 #### 引用[.reference_title] - *1* *3* [Bagging与Boosting算法的原理与区别,Boosting算法之一Adaboost原理与代码实现](https://blog.csdn.net/YDC123458/article/details/88353663)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [AdaBoost算法原理及python实现(手动感叹号)](https://blog.csdn.net/weixin_44598249/article/details/125302988)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值