线性回归1.0.0

#include <iostream>
using namespace std;

double w = 0.001;
double r = 0.0001;
double a = 1.0,b = 1.0;

double f(double x)
{
    double res;
    
    res = a*x + b;
    
    return res;
}

void df(double x,double y)
{   
    double t1,t2;
    int n;
    while( max( (f(x) - y) , (y - f(x)) ) > w )
    {
        cout << max( (f(x)-y) , (y-f(x)) ) << " " << a << " " << b << endl;
        
        if(f(x) < y)
        {
            n = 1.0;
        }
        else
        {
            n = -1.0;
        }
        
        t1 = max( ((f(x)+x*r*n) - y) , (y - (f(x)+x*r*n)) );
        t2 = max( ((f(x)+r*n) - y) , (y - (f(x)+r*n)) ); 

        if(t1 < t2)
        {
            a += r*n;
        }   
        else
        {
            b += r*n;
        }
    }

    cout << "...OK" << endl;
}

int main()
{
    string s;
    double x,y;
    while(true)
    {
        cin >> s;
        if(s == "in")
        {
            cin >> x >> y;
            df(x,y);
        }
        else if(s == "out")
        {
            cin >> x;       
            cout << f(x) << endl;
        }
    }
    
    return 0;
}

一个副本而已

  • 9
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值