⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 shibie.cpp

📁 模式识别感知器算法实现(样本分类)
💻 CPP
字号:
#include <iostream.h>
#include <string.h>
#include <math.h>
//固定增量感知器算法 
void main()
{
struct pattern
 {
  double y1;
  double y2;
  double y3;
  double y4;
  int w;
 };
 struct pattern  y[40]={
	 {5.1,3.5,1.4,0.2,1},
	 {4.9,3.0,1.4,0.2,1},
	 {4.7,3.2,1.3,0.2,1},
	 {4.6,3.1,1.5,0.2,1},
	 {5.0,3.6,1.4,0.2,1},
	 {5.4,3.9,1.7,0.4,1},
	 {4.6,3.4,1.4,0.3,1},
	 {5.0,3.4,1.5,0.2,1},
	 {4.4,2.9,1.4,0.2,1},
	 {4.9,3.1,1.5,0.1,1},
	 {5.4,3.7,1.5,0.2,1},
	 {4.8,3.4,1.6,0.2,1},
	 {4.8,3.0,1.4,0.1,1},
	 {4.3,3.0,1.1,0.1,1},
	 {5.8,4.0,1.2,0.2,1},
	 {5.7,4.4,1.5,0.4,1},
	 {5.4,3.9,1.3,0.4,1},
	 {5.1,3.5,1.4,0.3,1},
	 {5.7,3.8,1.7,0.3,1},
	 {5.1,3.8,1.5,0.3,1},
	 {-7.0,-3.2,-4.7,-1.4,-1},
	 {-6.4,-3.2,-4.5,-1.5,-1},
	 {-6.9,-3.1,-4.9,-1.5,-1},
	 {-5.5,-2.3,-4.0,-1.3,-1},
	 {-6.5,-2.8,-4.6,-1.5,-1},
	 {-5.7,-2.8,-4.5,-1.3,-1},
	 {-6.3,-3.3,-4.7,-1.6,-1},
	 {-4.9,-2.4,-3.3,-1.0,-1},
	 {-6.6,-2.9,-4.6,-1.3,-1},
	 {-5.2,-2.7,-3.9,-1.4,-1},
	 {-5.0,-2.0,-3.5,-1.0,-1},
	 {-5.9,-3.0,-4.2,-1.5,-1},
	 {-6.0,-2.2,-4.0,-1.0,-1},
	 {-6.1,-2.9,-4.7,-1.4,-1},
	 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5,1.5,1},
	 {5.7,2.6,3.5,1.0,1},
	 {5.5,2.4,3.8,1.1,1},
	 {5.5,2.4,3.7,1.0,1},
	 {5.8,2.7,3.9,1.2,1},
	 {6.0,2.7,5.1,1.6,1},
	 {5.4,3.0,4.5,1.5,1},
	 {6.0,3.4,4.5,1.6,1},
	 {6.7,3.1,4.7,1.5,1},
	 {6.3,2.3,4.4,1.3,1},
	 {5.6,3.0,4.1,1.3,1},
	 {5.5,2.5,4.0,1.3,1},
	 {5.5,2.6,4.4,1.2,1},
	 {6.1,3.0,4.6,1.4,1},
	 {5.8,2.6,4.0,1.2,1},
	 {5.0,2.3,3.3,1.0,1},
	 {5.6,2.7,4.2,1.3,1},
	 {5.7,3.0,4.2,1.2,1},
	 {5.7,2.9,4.2,1.3,1},
	 {6.2,2.9,4.3,1.3,1},
	 {5.1,2.5,3.0,1.1,1},
	 {5.7,2.8,4.1,1.3,1},
 };
 double a[5]={0,0,0,0,0};//初始广义权向量
 int m;
 
///////////////////////////
 do 
 { m=0;  
	 for(int i=0;i<40;i++)
	 {
	   if (y[i].w=1) 
              {if (a[0]*y[i].y1+a[1]*y[i].y2+a[2]*y[i].y3+a[3]*y[i].y4+a[4]<=0) 
                     {a[0]+=y[i].y1;
                      a[1]+=y[i].y2;
                      a[2]+=y[i].y3;
                      a[3]+=y[i].y4;
                      a[4]+=y[i].w;
					  m+=1;}  }
	   
           else if(y[i].w=-1)
		   {if(a[0]*y[i].y1+a[1]*y[i].y2+a[2]*y[i].y3+a[3]*y[i].y4-a[4]<=0)
		              {a[0]+=y[i].y1;
                       a[1]+=y[i].y2;
	                   a[2]+=y[i].y3;
                       a[3]+=y[i].y4;
		               a[4]+=y[i].w;
                       m+=1;}
	         }
         }

 }
 while (m!=0);

cout<<"y="<<a[0]<<"X0+("<<a[1]<<")X1+("<<a[2]<<")X2+("<<a[3]<<")X3+"<<a[4]<<endl;
    int j=0,l=0;
	 for(int i=0;i<60;i++)
	 {
		 if (a[0]*z[i].y1+a[1]*z[i].y2+a[2]*z[i].y3+a[3]*y[i].y4+a[4]>0) 
		 
			 j++;

		 else if (a[0]*z[i].y1+a[1]*z[i].y2+a[2]*z[i].y3+a[3]*y[i].y4+a[4]<0) 
		 
			 l++;

	 }
         cout<<"属于setosa的样本数量为:"<<j<<endl;
         cout<<"属于versicolor的样本数量为:"<<l<<endl;
}




⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -