资源简介
C++实现回归算法, 包含线性回归和逻辑回归, 代码干净, 整洁, 有注释, 具有良好的封装性, 可直接迁移使用
代码片段和文件信息
#include “LRegression.h“
#include
LLinearRegression::LLinearRegression()
{
}
LLinearRegression::~LLinearRegression()
{
}
bool LLinearRegression::TrainModel(
IN const LRegressionProblem& problem
IN float learningRate
IN unsigned int trainTimes)
{
// 检查参数
if (problem.XMatrix.RowLen < 2)
return false;
if (problem.XMatrix.ColumnLen < 1)
return false;
if (problem.YVector.ColumnLen != 1)
return false;
if (problem.YVector.RowLen != problem.XMatrix.RowLen)
return false;
if (learningRate <= 0.0f)
return false;
// 每个样本中最后一项增加常数项的特征值:1.0
m_xMatrix.Reset(problem.XMatrix.RowLen problem.XMatrix.ColumnLen + 1);
for (unsigned int row = 0; row < m_xMatrix.RowLen; row++)
{
for (unsigned int col = 0; col < m_xMatrix.ColumnLen-1; col++)
{
m_xMatrix[row][col] = problem.XMatrix[row][col];
}
m_xMatrix[row][m_xMatrix.ColumnLen-1] = 1.0f;
}
m_yVector = problem.YVector;
// 初始化权重向量
m_wVector.Reset(m_xMatrix.ColumnLen 1 0.0f);
const LRegressionMatrix& X = m_xMatrix;
const LRegressionMatrix& Y = m_yVector;
LRegressionMatrix& W = m_wVector;
float A = learningRate;
LRegressionMatrix XT = X.T();
LRegressionMatrix XW;
LRegressionMatrix O;
for (unsigned int i = 0; i < trainTimes; i++)
{
XW = X * W;
O = XT * (XW - Y);
W = W - O.ScalarMul(A);
}
return true;
}
bool LLinearRegression::GetWeightVector(OUT LRegressionMatrix& weightVector)
{
if (m_wVector.RowLen < 1)
return false;
weightVector = m_wVector;
return true;
}
float LLinearRegression::GetErrorValue()
{
if (m_wVector.RowLen < 1)
return -1.0f;
LRegressionMatrix dif = m_xMatrix * m_wVector - m_yVector;
LRegressionMatrix square = dif.T() * dif;
float squareValue = square[0][0];
squareValue = sqrt(squareValue);
return squareValue;
}
LLogisticRegression::LLogisticRegression()
{
}
LLogisticRegression::~LLogisticRegression()
{
}
bool LLogisticRegression::TrainModel(IN const LRegressionProblem& problem IN float learningRate IN unsigned int trainTimes)
{
// 检查参数
if (problem.XMatrix.RowLen < 2)
return false;
if (problem.XMatrix.ColumnLen < 1)
return false;
if (problem.YVector.ColumnLen != 1)
return false;
if (problem.YVector.RowLen != problem.XMatrix.RowLen)
return false;
if (learningRate <= 0.0f)
return false;
for (unsigned int i = 0; i < problem.YVector.RowLen; i++)
{
if (problem.YVector[i][0] != REGRESSION_ONE &&
problem.YVector[i][0] != REGRESSION_ZERO)
return false;
}
// 每个样本中最后一项增加常数项的特征值:1.0
m_xMatrix.Reset(problem.XMatrix.RowLen problem.XMatrix.ColumnLen + 1);
for (unsigned in
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 14193 2016-02-29 15:00 Regression\LMatrix.h
文件 4949 2016-02-29 15:00 Regression\LRegression.cpp
文件 3714 2016-03-17 10:52 Regression\LRegression.h
文件 680 2016-03-17 10:52 Regression\main.cpp
文件 886 2016-03-17 10:50 Regression\Regression.sln
..A..H. 10240 2016-03-17 10:52 Regression\Regression.suo
文件 3382 2016-03-17 10:52 Regression\Regression.vcxproj
文件 1270 2016-03-17 10:52 Regression\Regression.vcxproj.filters
文件 143 2016-03-17 10:50 Regression\Regression.vcxproj.user
目录 0 2016-03-17 10:52 Regression
----------- --------- ---------- ----- ----
39457 10
- 上一篇:Htran 0.22源码 c++
- 下一篇:混合基fft变换
评论
共有 条评论