线性回归算法实现

原理参考:python实现线性回归算法

代码传送门:https://github.com/taifyang/machine-learning

import numpy as np
 
#线性回归模型
def LinearRegression(x, y, alpha, iters):   
    x = np.insert(x, 0, np.ones(x.shape[0]), axis=1)
    y = y.T  
    w = np.mat(np.zeros(x.shape[1]))
    #每迭代一次,就要循环更新一次所有参数的值
    for i in range(iters):
        print('iters:', i,' cost:', 1 / (2 * y.size) * np.sum(np.power(x * w.T - y, 2))) 
        w -= (alpha / y.size) * (x * w.T - y).T * x
    return np.array(w)[0][0], np.array(w)[0][1:]


if __name__ == '__main__':
    x = np.array([[1], [2], [3], [4]])
    y = np.array([[1, 2, 2.9, 4.1]])  
    w, b = LinearRegression(x, y, alpha=0.1, iters=100)
    print( '最终训练得到的w和b为:', w, b)

python调包:

import numpy as np
from sklearn.linear_model import LinearRegression

#线性回归模型
def MyLinearRegression(x, y):   
    clf = LinearRegression()
    clf.fit(x, y)  #训练感知机   
    w = clf.coef_ #得到权重矩阵    
    b = clf.intercept_ #得到截距
    return w, b


if __name__ == '__main__':
    x = np.array([[1], [2], [3], [4]])
    y = np.array([1, 2, 2.9, 4.1]) 
    w, b = MyLinearRegression(x, y)
    print('最终训练得到的w和b为:', w, ',', b)   

C++实现:

#include <iostream>
#include <vector>
#include <Eigen/Dense>

//线性回归模型
void LinearRegression(std::vector<std::vector<float>> x, std::vector<float> y, float alpha, int iters)
{
	for (size_t i = 0; i < x.size(); i++)
	{
		x[i].insert(x[i].begin(), 1);
	}
	Eigen::MatrixXf mat_x(x.size(), x[0].size());
	for (size_t i = 0; i < mat_x.rows(); i++)
	{
		for (size_t j = 0; j < mat_x.cols(); j++)
		{
			mat_x(i, j) = x[i][j];
		}
	}

	Eigen::MatrixXf mat_y(y.size(), 1);
	for (size_t i = 0; i < mat_y.rows(); i++)
	{
		mat_y(i, 0) = y[i];
	}

	Eigen::MatrixXf mat_w = Eigen::MatrixXf::Zero(1, x[0].size());

	for (size_t i = 0; i < iters; i++)
	{
		mat_w -= (alpha / y.size())*(mat_x*mat_w.transpose() - mat_y).transpose()*mat_x;
		std::cout <<"iters: " << i << " cost: " << 1.0 / (2 * y.size())*(mat_x*mat_w.transpose() - mat_y).squaredNorm() << std::endl;
	}
	std::cout << "最终训练得到的w为:" << mat_w << std::endl;
}


int main(int argc, char* argv[])
{
	std::vector<std::vector<float>> x = { { 1 },{ 2 },{ 3 },{ 4 } };
	std::vector<float> y = { 1, 2, 2.9f, 4.1f };

	LinearRegression(x, y, 0.1, 100);

	system("pause");
	return EXIT_SUCCESS;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

给算法爸爸上香

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值