-
大小: 9KB文件類型: .rar金幣: 2下載: 0 次發(fā)布日期: 2021-06-05
- 語(yǔ)言: C/C++
- 標(biāo)簽: 機(jī)器學(xué)習(xí)??邏輯回歸??線性回歸??
資源簡(jiǎn)介
C++實(shí)現(xiàn)回歸算法, 包含線性回歸和邏輯回歸, 代碼干凈, 整潔, 有注釋, 具有良好的封裝性, 可直接遷移使用

代碼片段和文件信息
#include?“LRegression.h“
#include?
LLinearRegression::LLinearRegression()
{
}
LLinearRegression::~LLinearRegression()
{
}
bool?LLinearRegression::TrainModel(
????IN?const?LRegressionProblem&?problem?
????IN?float?learningRate?
????IN?unsigned?int?trainTimes)
{
????//?檢查參數(shù)
????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;
????//?每個(gè)樣本中最后一項(xiàng)增加常數(shù)項(xiàng)的特征值:1.0
????m_xMatrix.Reset(problem.XMatrix.RowLen?problem.XMatrix.ColumnLen?+?1);
????for?(unsigned?int?row?=?0;?row?????{
????????for?(unsigned?int?col?=?0;?col?????????{
????????????m_xMatrix[row][col]?=?problem.XMatrix[row][col];
????????}
????????m_xMatrix[row][m_xMatrix.ColumnLen-1]?=?1.0f;?
????}
????m_yVector?=?problem.YVector;
????//?初始化權(quán)重向量
????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?????{
????????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)
{
????//?檢查參數(shù)
????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?????{
????????if?(problem.YVector[i][0]?!=?REGRESSION_ONE?&&
????????????problem.YVector[i][0]?!=?REGRESSION_ZERO)
????????????return?false;
????}
????//?每個(gè)樣本中最后一項(xiàng)增加常數(shù)項(xiàng)的特征值:1.0
????m_xMatrix.Reset(problem.XMatrix.RowLen?problem.XMatrix.ColumnLen?+?1);
????for?(unsigned?in
?屬性????????????大小?????日期????時(shí)間???名稱
-----------?---------??----------?-----??----
?????文件??????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變換
評(píng)論
共有 條評(píng)論