线性回归中的结构向量

问题描述 投票:0回答:1

我是 C++ 编程新手,我正在尝试编写一个简单的线性回归程序,该程序返回仿射函数 y=ax+b 的参数。

我担心的是,我无法在“fit”和“mse”函数中“使用”包含代表点云的 p 点的 x 和 y 坐标的向量。 预先感谢您的帮助。

问候

弗雷德里克

#include <iostream>
#include <iomanip>
#include <cmath>
#include <vector>

using namespace std;

struct Point {
    double x;
    double y;
};

struct line {
    double a;
    double b;
};

const vector<Point> points({ { 1.2, 2.3 },
                             { 2.4, 2.9 },
                             { 1.9, 2.5 },
                             { 2.5, 3.8 },
                                          });

void affiche(const Point& p);
void fit(const Point& p, line& d);
void line_value(const line& d, double x);
void affiche_line(const line& d);
double mse(const line& d, const Point& p);

int main() {

    line l;
    Point p;
    
    fit(p, l);
    cout << endl;
    double x(2.0);
    line_value(l, x);
    cout << endl;
    affiche_line(l);
    cout << endl;
    cout << "MSE = " << mse(l, p) << endl;

    return 0;
}

void affiche(const Point& p) {
    for(auto p : points) {
        cout << "x= " << p.x << " , y= " << p.y << endl;
    }
}

void fit(const Point& p, line& d) {
    double sum_x(0.0);
    double sum_y(0.0);
    
    cout << setw(10) << "x_i"
         << setw(10) << "y_i"
         << setw(10) << "sum x"
         << setw(10) << "sum y"
         << setw(10) << "x^2"
         << setw(10) << "xy"
         << endl;

    for(size_t i(0); i < points.size(); ++i) {
        sum_x += p.x;
        sum_y += p.y;
        
        d.a += (p.x * p.y - 1.0 / points.size() * sum_x * sum_y) /
               (p.x * p.x - 1.0 / points.size() * sum_x * sum_x);
        
        d.b += 1.0 / points.size() * (p.y - d.a * p.x);
        
        cout << setw(10) << p.x << setw(10) << p.y
             << setw(10) << sum_x << setw(10) << sum_y
             << setw(10) << p.x * p.x << setw(10) << p.x * p.y
             << endl;
    }
}

void line_value(const line& d, double x) {
    cout << "x= " << x << endl;
    cout << "y= " << d.a * x + d.b << endl;
}

void affiche_line(const line& d) {
    cout << "Parameter a= " << d.a << endl;
    cout << "Parameter b= " << d.b << endl;
}

double mse(const line& d, const Point& p) {
    double MSE(0.0);
    
    for(size_t i(0); i < points.size(); ++i){
        MSE += 1.0 / points.size() * pow(p.y - (d.a * p.x + d.b), 2.0);
    }
    return MSE;
}

我尝试初始化主要的点向量,但它根本不起作用。

正如我所说,我是初学者,我没有任何解决方案。

不可能使用我的向量中的数字来生成a和b的值

c++ vector statistics regression standard-deviation
1个回答
0
投票

当然,弗雷德里克!这里的问题是,您实际上并没有在

points
fit
函数中使用
mse
向量。您仅使用参数
p
,它是单个
Point
结构,而不是点向量。

这是对您的代码的简洁修复:

  1. 修改
    fit
    mse
    的函数签名以接受
    points
    向量。
  2. 更新
    fit
    mse
    内的循环以迭代
    points
    向量。
void fit(const vector<Point>& points, line& d); // Function signature change
double mse(const line& d, const vector<Point>& points); // Function signature change

int main() {
    //...
    fit(points, l); // Pass the points vector
    //...
    cout << "MSE = " << mse(l, points) << endl; // Pass the points vector
    //...
}

void fit(const vector<Point>& points, line& d) { // Function definition change
    //...
    for(size_t i(0); i < points.size(); ++i) {
        double x = points[i].x;
        double y = points[i].y;
        sum_x += x;
        sum_y += y;
        // Modify d.a and d.b calculations accordingly
        //...
    }
    //...
}

double mse(const line& d, const vector<Point>& points) { // Function definition change
    //...
    for(size_t i(0); i < points.size(); ++i){
        double x = points[i].x;
        double y = points[i].y;
        MSE += 1.0 / points.size() * pow(y - (d.a * x + d.b), 2.0);
    }
    return MSE;
}

这些更改将使代码使用向量中的实际点来拟合直线并计算均方误差。确保根据正确的线性回归计算修改

a
b
的公式。

© www.soinside.com 2019 - 2024. All rights reserved.