本文主要是介绍C++实现感知器算法用于多类情况(二维),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
研究生期间的模式识别课程。
具体源代码:
#include <iostream>
#include<stdlib.h>
#include <math.h>
using namespace std;
int s_count;
int par_count;class sample // 样本类s
{
private:int par[3];char type; // 样本类别 M F I 三类public:void setpar(int *a){// par = new int[2 + 1]; for (int i = 0; i < 2 + 1; i++)par[i] = a[i];}sample(){}void setchar(char c){type = c;}void getpar(int *s){for (int i = 0; i < 2 + 1; i++){s[i] = par[i];}}char gettype(){return type;}~sample(){}
};
class vector // 向量类
{
private:int par[3];
public:vector() {}int getpar(int i){return par[i];}void getpar(int *a){for (int i = 0; i < 2 + 1; i++){a[i] = par[i];}}void setpar(int *a){//par = new int[2+1];for (int i = 0; i < 2 + 1; i++){par[i] = a[i];}}~vector(){}
};
int martix_multiply(vector w, sample x)
{int end = 0;/*int *v;v = new int[2 + 1];int *s;s = new int[2 + 1];*/int v[3];int s[3];w.getpar(v);x.getpar(s);for (int i = 0; i < 2 + 1; i++){end += v[i] * s[i];}return end;//delete[]v;//delete[]s;
}
void martix_sub(vector &w, sample x)
{/*int *v;v = new int[2 + 1];int *s;s = new int[2 + 1];*/int v[3];int s[3];w.getpar(v);x.getpar(s);for (int i = 0; i < 2 + 1; i++){v[i] = v[i] - s[i];}w.setpar(v);//delete []v;//delete []s;
}
void martix_add(vector &w, sample x)
{/*int *v;v = new int[2 + 1];int *s;s = new int[2 + 1];*/int v[3];int s[3];w.getpar(v);x.getpar(s);for (int i = 0; i < 2 + 1; i++){v[i] = v[i] + s[i];}w.setpar(v);//delete[]v;//delete[]s;
}
struct s_v
{sample s[100];vector v[3];
};
int main()
{s_v as;cin >> s_count; // s_count : count of sample cin >> par_count; // 2:count of parmeterint *temp;temp = new int[2 + 1];//三维的一个数组int i;int j;for (i = 0; i < s_count; i++){for (j = 0; j < 2; j++){cin >> temp[j];}//循环为每个样本中的二维点赋值temp[2] = 1;as.s[i].setpar(temp);}for (i = 0; i < 3; i++){temp[i] = 0;}for (i = 0; i < 3; i++){as.v[i].setpar(temp);}int d[3] = { 0,0,0 };int times = 0; //迭代次数bool charge[3];for (i = 0; i < 3; i++){charge[i] = false;}while (charge[0] == false || charge[1] == false || charge[2] == false){i = times % s_count;d[i % 3] = martix_multiply(as.v[i % 3], as.s[i]);d[(i + 1) % 3] = martix_multiply(as.v[(i + 1) % 3], as.s[i]);d[(i + 2) % 3] = martix_multiply(as.v[(i + 2) % 3], as.s[i]);if (d[i] > d[(i + 1) % 3] && d[i] > d[(i + 2) % 3]){charge[i] = true;}else if (d[i] <= d[(i + 1) % 3] && d[i] <= d[(i + 2) % 3]){martix_add(as.v[i % 3], as.s[i]);martix_sub(as.v[(i + 1) % 3], as.s[i]);martix_sub(as.v[(i + 2) % 3], as.s[i]);charge[0] = false;charge[1] = false;charge[2] = false;}else if (d[i] > d[(i + 1) % 3 && d[i] <= d[(i + 2) % 3]]){martix_add(as.v[i % 3], as.s[i]);martix_sub(as.v[(i + 2) % 3], as.s[i]);charge[0] = false;charge[1] = false;charge[2] = false;}else if (d[i] > d[i + 2] % 3 && d[i] <= d[(i + 1) % 3]){martix_add(as.v[i % 3], as.s[i]);martix_sub(as.v[(i + 1) % 3], as.s[i]);charge[0] = false;charge[1] = false;charge[2] = false;}times++;}for (i = 0; i < 3; i++){cout << "[";for (j = 0; j < 2 + 1; j++){if (j == 2){cout << as.v[i].getpar(j);}elsecout << as.v[i].getpar(j) << ",";}cout << "]" << endl;}cout << times << endl;cin >> i;
}
例子:
w1: (-1,-1)T , w2: (0,0)T , w3: (1,1)T
运行结果:
这篇关于C++实现感知器算法用于多类情况(二维)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!