class CPoint
{
public:
double x;
double y;
CPoint()
{
x = 0.0;
y = 0.0;
}
CPoint(double x, double y)
{
this->x = x;
this->y = y;
}
double getX()
{
return x;
}
double getY()
{
return y;
}
};
//利用线性回归模型进行预测
//y = a+bx1+cx2...(为简化计算量,设方程为y = a + bx)
//实现方法:梯度下降法
#include "CPoint.h"
#include <iostream>
#include <vector>
#include <Cmath>
using namespace std;
class LinearRegression
{
private:
double a, b;
double lasta, lastb;
const double alpha = 0.5;
public:
LinearRegression()
{
a = 0.0;
b = 0.0;
}
void GradentDescent(CPoint * p, int n)
{
do
{
lasta = a;
lastb = b;
//首先更新a
for (int i = 0; i < n; i++)
{
double hx = a + b*p[i].getX();
a = a + alpha*(p[i].getY() - hx);
}