分类山鸢尾和变色鸢尾

测试数据(测试数据中-1,1表示不同种类):

8
1.1 0.1 -1
1.7 0.5 -1
1.4 0.3 -1
1.6 0.6 -1
5.0 1.7 1
4.0 1.0 1
4.5 1.5 1
3.0 1.1 1

源代码:

#include<iostream>
#include<fstream>

#define fin cin
#define fout cout

using namespace std;

struct flower{//define struct for datas of flower
    double x1;
    double x2;
    short y;
};
double a1,a2,b; 
double rate;

double max(double a,double b){//define max function
    return a>=b?a:b;
}
double f(double x1,double x2){//define linear classifier
    return a1*x1+a2*x2+b;
}
short g(double x1,double x2){//define result y
    return f(x1,x2)>=1?1:-1;
}

int main(){
    cin>>a1>>a2>>b>>rate;
    ifstream cin("in.txt");
    ofstream cout("out.txt");
    int i=0;int n;
    cin>>n;
    flower data[100];
    while(cin>>data[i].x1>>data[i].x2>>data[i].y&&i<n){//set datas
        if(-g(data[i].x1,data[i].x2)*f(data[i].x1,data[i].x2)){//if loss function>0,change a1,a2,b
            a1+=rate*g(data[i].x1,data[i].x2)*data[i].x1;
            a2+=rate*g(data[i].x1,data[i].x2)*data[i].x2;
            b+=rate*g(data[i].x1,data[i].x2);
            i--;
        }
        i++;
    }
    cout<<a1<<" "<<a2<<" "<<b<<endl;
    return 0;
} 
最后修改:2019 年 07 月 27 日 06 : 36 PM
如果觉得我的文章对你有用,请随意赞赏