具体公式百度:参看统计学方法中算法描述实现的
一,原始形式
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34<pre name="code" class="cpp">#include<iostream> #include<stdlib.h> #include<string.h> #include<stdio.h> using namespace std; int main() { int x1[]={3,4,1}; int x2[]={3,3,1}; int y[]={1,1,-1}; int w[]={0,0}; int b=0; int index=0; int n=1; while(index<3) { int temp=y[index]*(x1[index]*w[0]+x2[index]*w[1]+b); if(temp<=0) { w[0]+=n*y[index]*x1[index]; w[1]+=n*y[index]*x2[index]; b+=n*y[index]; index=0; } else{ index++; } } cout<<"result:"<<endl; cout<<"w:"<<w[0]<<" "<<w[1]<<endl; cout<<"b:"<<b; getchar(); return 0; }
复制代码
对偶形式
1
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67#include<stdio.h> #include<string.h> #include<iostream> using namespace std; int main() { int x1[]={3,4,1}; int x2[]={3,3,1}; int y[]={1,1,-1}; int b=0; int a[]={0,0,0}; int G[3][3]; //计算Gram矩阵 cout<<"Gram矩阵为:"<<endl; for(int i=0;i<3;i++) for(int j=0;j<3;j++) { G[i][j]=x1[i]*x1[j]+x2[i]*x2[j]; cout<<G[i][j]<<"t"; if(j==2) cout<<endl; } cout<<"开始计算a,b:"<<endl; int index=0; int n=1; while(index<3) { int temp=0; for(int j=0;j<3;j++) { temp+=a[j]*y[j]*G[j][index]; } int all=y[index]*(temp+b); if(all<=0) { a[index]+=n; b+=n*y[index]; index=0; } else { index++; } } cout<<"a:"<<a[0]<<" "<<a[1]<<" "<<a[2]<<endl; cout<<"b:"<<b; //计算w和b int w[]={0,0}; for(int i=0;i<3;i++) { w[0]+=a[i]*y[i]*x1[i]; w[1]+=a[i]*y[i]*x2[i]; } b=0; for(int i=0;i<3;i++) { b+=a[i]*y[i]; } cout<<endl<<"w:"<<w[0]<<" "<<w[1]<<endl; cout<<"b:"<<b; getchar(); return 0; }
最后
以上就是长情西牛最近收集整理的关于感知机c/c++代码实现的全部内容,更多相关感知机c/c++代码实现内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复