[C++]分类任务——线性分类器|人工智能基础

分类山鸢尾和变色鸢尾

测试数据(测试数据中-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;
} 

原文地址:https://blog.irow.top/index.php/archives/7/
更多精彩内容,请关注时与空的终点

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值