既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上C C++开发知识点,真正体系化!
由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新
Ej = calcEk(j);
return j;
}
}
void updateEk(int k)
{
double Ek = calcEk(k);
os.eCache.mat[k][0] = 1;
os.eCache.mat[k][1] = Ek;
}
int innerL(int i)
{
double Ei = calcEk(i);
if (((os.labelMat.mat[i][0] * Ei < -os.tol) && (os.alpahs.mat[i][0] < os.C)) ||
((os.labelMat.mat[i][0] * Ei > os.tol) && (os.alpahs.mat[i][0] > 0)))
{
double Ej = 0.0;
int j = selectJ(i, Ei, Ej);
double alphaIold = os.alpahs.mat[i][0];
double alphaJold = os.alpahs.mat[j][0];
double L = 0.0;
double H = 0.0;
if (os.labelMat.mat[i][0] != os.labelMat.mat[j][0])
{
L = max(0.0, os.alpahs.mat[j][0] - os.alpahs.mat[i][0]);
H = min(os.C, os.C + os.alpahs.mat[j][0] - os.alpahs.mat[i][0]);
}
else
{
L = max(0.0, os.alpahs.mat[j][0] + os.alpahs.mat[i][0] - os.C);
H = min(os.C, os.alpahs.mat[j][0] + os.alpahs.mat[i][0]);
}
if (abs(L - H) < 0.00001)
{
cout<<"L == H"<<endl;
return 0;
}
double eta = 0.0;
eta += 2.0 * dataIJ(os.X, i, j);
eta -= dataIJ(os.X, i, i);
eta -= dataIJ(os.X, j, j);
if (eta >= 0)
{
cout<<"eta >= 0"<<endl;
return 0;
}
os.alpahs.mat[j][0] -= os.labelMat.mat[j][0] * (Ei - Ej) / eta;
os.alpahs.mat[j][0] = clipAlpha(os.alpahs.mat[j][0], H, L);
updateEk(j);
if (abs(os.alpahs.mat[j][0] - alphaJold) < 0.00001)
{
cout<<"j not moving enought"<<endl;
return 0;
}
os.alpahs.mat[i][0] += os.labelMat.mat[j][0] * os.labelMat.mat[i][0] *
(alphaJold - os.alpahs.mat[j][0]);
updateEk(i);
double b1 = os.b - Ei;
b1 -= os.labelMat.mat[i][0] * (os.alpahs.mat[i][0] - alphaIold) * dataIJ(os.X, i, i);
b1 -= os.labelMat.mat[j][0] * (os.alpahs.mat[j][0] - alphaJold) * dataIJ(os.X, i, j);
double b2 = os.b - Ej;
b2 -= os.labelMat.mat[i][0] * (os.alpahs.mat[i][0] - alphaIold) * dataIJ(os.X, i, j);
b2 -= os.labelMat.mat[j][0] * (os.alpahs.mat[j][0] - alphaJold) * dataIJ(os.X, j, j);
if ((0 < os.alpahs.mat[i][0]) && (os.C > os.alpahs.mat[i][0]))
{
os.b = b1;
}
else if ((0 < os.alpahs.mat[j][0]) && (os.C > os.alpahs.mat[j][0]))
{
os.b = b2;
}
else
os.b = (b1 + b2) / 2.0;
return 1;
}
else
return 0;
}
double smoP(Matrix dataMatIn, Matrix classLabels, Matrix &alphas, double C, double toler,int maxIter)
{
}
};
/优化的版本+核函数/
//参数保持在数据结构体中
struct OSK{
Matrix X;
Matrix labelMat;
double C;
double tol;
int m;
Matrix alpahs;
double b;
Matrix eCache;
Matrix k;
};
struct KTup{
string stringType;
double val;
};
class SMOPK{
public:
OSK os;
public:
int initOs(Matrix dataMatIn, Matrix classLabels, double C, double toler, KTup kTup)
{
os.X.initMatrix(&os.X, dataMatIn.col, dataMatIn.row);
os.X.copy(dataMatIn, &os.X);
os.labelMat.initMatrix(&os.labelMat, classLabels.col, classLabels.row);
os.labelMat.copy(classLabels, &os.labelMat);
os.C = C;
os.tol = toler;
os.m = dataMatIn.col;
os.alpahs.initMatrix(&os.alpahs, os.m, 1, 0.0);
os.b = 0.0;
os.eCache.initMatrix(&os.eCache, os.m, 2, 0.0);
os.k.initMatrix(&os.k, os.m, os.m);
Matrix Ktemp;
Ktemp.initMatrix(&Ktemp, os.m, 1);
Matrix A;
A.initMatrix(&A, 1, os.X.row);
for (int i = 0; i < os.m; i++)
{
A = A.getOneCol(os.X, i);
Ktemp = kernelTrans(os.X, A, kTup);
cout<<endl;
for (int j = 0; j < os.m; j++)
{
os.k.mat[j][i] = Ktemp.mat[j][0];
}
}
os.k.print(os.k);
return 0;
}
Matrix kernelTrans(Matrix X, Matrix &A, KTup kTup)
{
int m = X.col;
int n = X.row;
Matrix K;
K.initMatrix(&K,m,1);
Matrix AT;
AT.initMatrix(&AT, A.row, 1);
AT.transposematrix(A, &AT);
if (kTup.stringType == "lin")
{
K.multsmatrix(&K, X, AT);
}
else if (kTup.stringType == "rbf")
{
for (int j = 0; j < m; j++)
{
for (int i = 0; i < n; i++)
{
K.mat[j][0] += (X.mat[j][i] - A.mat[0][i]) * (X.mat[j][i] - A.mat[0][i]);
}
K.mat[j][0] = exp(K.mat[j][0] / (-1 * pow(kTup.val,2)));
}
}
else
{
cout<<"That Kernel is not recognized"<<endl;
exit(-1);
}
return K;
}
double calcEk(int k)
{
double Ek = 0.0;
double fXk = 0.0;
Matrix alphasMultsLabel;
alphasMultsLabel.initMatrix(&alphasMultsLabel, os.m, 1);
alphasMultsLabel.multsmatrixDot(&alphasMultsLabel, os.alpahs, os.labelMat);
Matrix alphasMultsLabelT;
alphasMultsLabelT.initMatrix(&alphasMultsLabelT, 1, os.m);
alphasMultsLabelT.transposematrix(alphasMultsLabel, &alphasMultsLabelT);
Matrix osXk;
osXk.initMatrix(&osXk, os.X.col, 1);
osXk = osXk.getOneRow( os.k, k); //获得第k列
for (int i = 0; i < os.m; i++)
{
fXk += alphasMultsLabelT.mat[0][i] * osXk.mat[i][0];
}
fXk += os.b;
Ek = fXk - os.labelMat.mat[k][0];
return Ek;
}
//如果处理返回 maxK和Ej 否则返回j和Ej
double selectJ(int i, double Ei , double &Ej)
{
int maxK = os.m - 1;
double maxDeltaE = 0.0;
Ej = 0.0;
os.eCache.mat[i][0] = 1;
os.eCache.mat[i][1] = Ei;
vector<int>validEcacheList;
//统计第一列不为0的序号
for (int k = 0; k < os.m; k++)
{
if (os.eCache.mat[k][0] != 0)
{
validEcacheList.push_back(k);
}
}
if (validEcacheList.size() > 1)
{
for (int k = 0; k < validEcacheList.size(); k++)
{
if (validEcacheList[k] == i)
{
continue;
}
double Ek = calcEk(validEcacheList[k]);
double deltaE = abs(Ei - Ek);
if (deltaE > maxDeltaE)
{
maxK = validEcacheList[k];
maxDeltaE = deltaE;
Ej = Ek;
}
}
return maxK;
}
else
{
int j = selectJrand(i, os.m);
Ej = calcEk(j);
return j;
}
}
void updateEk(int k)
{
double Ek = calcEk(k);
os.eCache.mat[k][0] = 1;
os.eCache.mat[k][1] = Ek;
}
int innerL(int i)
{
double Ei = calcEk(i);
if (((os.labelMat.mat[i][0] * Ei < -os.tol) && (os.alpahs.mat[i][0] < os.C)) ||
((os.labelMat.mat[i][0] * Ei > os.tol) && (os.alpahs.mat[i][0] > 0)))
{
double Ej = 0.0;
int j = selectJ(i, Ei, Ej);
double alphaIold = os.alpahs.mat[i][0];
double alphaJold = os.alpahs.mat[j][0];
double L = 0.0;
double H = 0.0;
if (os.labelMat.mat[i][0] != os.labelMat.mat[j][0])
{
L = max(0.0, os.alpahs.mat[j][0] - os.alpahs.mat[i][0]);
H = min(os.C, os.C + os.alpahs.mat[j][0] - os.alpahs.mat[i][0]);
}
else
{
L = max(0.0, os.alpahs.mat[j][0] + os.alpahs.mat[i][0] - os.C);
H = min(os.C, os.alpahs.mat[j][0] + os.alpahs.mat[i][0]);
}
if (abs(L - H) < 0.00001)
{
cout<<"L == H"<<endl;
return 0;
}
double eta = 0.0;
eta += 2.0 * os.k.mat[i][j];
eta -= os.k.mat[i][i];
eta -= os.k.mat[j][j];
if (eta >= 0)
{
cout<<"eta >= 0"<<endl;
return 0;
}
os.alpahs.mat[j][0] -= os.labelMat.mat[j][0] * (Ei - Ej) / eta;
os.alpahs.mat[j][0] = clipAlpha(os.alpahs.mat[j][0], H, L);
updateEk(j);
if (abs(os.alpahs.mat[j][0] - alphaJold) < 0.00001)
{
cout<<"j not moving enought"<<endl;
return 0;
}
os.alpahs.mat[i][0] += os.labelMat.mat[j][0] * os.labelMat.mat[i][0] *
(alphaJold - os.alpahs.mat[j][0]);
updateEk(i);
double b1 = os.b - Ei;
b1 -= os.labelMat.mat[i][0] * (os.alpahs.mat[i][0] - alphaIold) * os.k.mat[i][i];
b1 -= os.labelMat.mat[j][0] * (os.alpahs.mat[j][0] - alphaJold) * os.k.mat[i][j];
double b2 = os.b - Ej;
b2 -= os.labelMat.mat[i][0] * (os.alpahs.mat[i][0] - alphaIold) * os.k.mat[i][j];
b2 -= os.labelMat.mat[j][0] * (os.alpahs.mat[j][0] - alphaJold) * os.k.mat[j][j];
if ((0 < os.alpahs.mat[i][0]) && (os.C > os.alpahs.mat[i][0]))
{
os.b = b1;
}
else if ((0 < os.alpahs.mat[j][0]) && (os.C > os.alpahs.mat[j][0]))
{
os.b = b2;
}
else
os.b = (b1 + b2) / 2.0;
return 1;
}
else
return 0;
}
double smoP(Matrix dataMatIn, Matrix classLabels, Matrix &alphas, double C, double toler,int maxIter, KTup kTup)
{
}
};
int main()
{
srand((unsigned)time(NULL));
dataToMatrix dtm;
cout<<“load data”<<endl;
cout<<“------------”<<endl;
char file[20] = “G:/data/svm.txt”;
dtm.loadData(&dtm,file);
Matrix dataMatrix;
dataMatrix.loadMatrix(&dataMatrix,dtm);
Matrix labelMatrix;
labelMatrix.initMatrix(&labelMatrix, dataMatrix.col,1);
labelMatrix = labelMatrix.getOneRow(dataMatrix,dataMatrix.row - 1);
dataMatrix.deleteOneRow(&dataMatrix, dataMatrix.row);
Matrix alphas;
alphas.initMatrix(&alphas, dataMatrix.col, 1, 0);
// cout<<smoSimple(dataMatrix, labelMatrix, alphas, 0.6, 0.001, 40)<<endl;
// SMOP smop;
// cout<<smop.smoP(dataMatrix, labelMatrix, alphas, 0.6, 0.001, 40)<<endl;
SMOPK smopk;
KTup kTup;
kTup.stringType = "rbf";
kTup.val = 1.3;
cout<<smopk.smoP(dataMatrix, labelMatrix, alphas, 0.6, 0.001,40, kTup)<<endl;
for (int i = 0; i < 100; i++)
{
if (alphas.mat[i][0] > 0.0)
{
for (int j = 0; j < dataMatrix.row; j++)
{
cout<<dataMatrix.mat[i][j]<<" ";
}
cout<<labelMatrix.mat[i][0]<<endl;
}
}
return 0;
}
**第三步:运行过程**
运行结果
![img](https://img-blog.csdnimg.cn/img_convert/8b85fe1042f77ee9d23fa80a3560aa7a.png)
![img](https://img-blog.csdnimg.cn/img_convert/f65c61a0ff09e7ddffc6ba6cb7585299.png)
**既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上C C++开发知识点,真正体系化!**
**由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新**
**[如果你需要这些资料,可以戳这里获取](https://bbs.csdn.net/topics/618668825)**
t<<labelMatrix.mat[i][0]<<endl;
}
}
return 0;
}
第三步:运行过程
运行结果
[外链图片转存中…(img-QfJ9ySCX-1715564112052)]
[外链图片转存中…(img-uaWUT5gp-1715564112052)]
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上C C++开发知识点,真正体系化!
由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新