线性回归c++实现

本文分享了使用C++编程实现线性回归的过程,详细介绍了如何通过代码处理数据集并进行线性回归分析。读者可以在此链接下载相关数据集及源代码:https://download.csdn.net/download/wz2671/11129334。
摘要由CSDN通过智能技术生成
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);
			}
	
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值