逻辑回归采用对数据二分类结果很差,找不到原因,是我的梯度下降有问题吗?求各位给分析分析
//测试数据(按列排的,使用期间对数据进行了中心化)
double[,] xTraMat = {{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},{0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}
,{18,21,23,23,28,31,36,42,46,48,55,56,58,18,20,25,27,28,30,32,33,33,38,41,45,48,52,56}
,{850,1200,850,950,1200,850,1500,1000,950,1200,1800,2100,1800,850,1000,1200,1300,1500,950,1000,1800,1000,1200,1500,1800,1000,1500,1800}
};
//测试标签
double[,] yDatas = {{0,0,1,1,1,0,1,1,1,0,1,1,1,0,0,0,0,0,1,0,0,0,0,0,1,0,1,1}};
double[,] rBeta = Regression.FGDInLogisticRegression(csx, yMat, 0.05, 10000);
string[] strs = new string[(int)n];
for (int i = 0; i < n; i++)
{
double verf = Regression.Sigmoid(Matrix.MultiplyMatrix(Matrix.VectorGenerate(Matrix.MatrixGetVector(csx, i, true), true), rBeta)[0, 0]);
strs[i]= $"Real:{yMat[i, 0]}" + "," + $"Verf:{verf}";
}
public static double[,] FGDInLogisticRegression(double[,] x, double[,] y,double alpha,int iterations)
{
int n = x.GetLength(0);
int p = x.GetLength(1);
double[,] xTran= Matrix.Transpose(x);
double epsilon = 1e-8;
double[,] beta = new double[p, 1];
for(int t = 0; t < iterations; t++)
{
double[,] error = new double[n, 1];
//计算当前beta系数下,损失函数对未知参数梯度(在此选择全部样本)
for (int i = 0; i < n; i++)
{
double[,] currRow = Matrix.VectorGenerate(Matrix.MatrixGetVector(x, i, true), true);
error[i, 0] = y[i, 0] - Sigmoid(Matrix.MultiplyMatrix(currRow, beta)[0, 0]);
}
double[,] dl = new double[p, 1];
dl = Matrix.MultiplyConst(Matrix.MultiplyMatrix(xTran, error),1.0/(double)n);//梯度值
if (Matrix.Norm(dl, 1) < epsilon)
{
break;
}
beta = Matrix.AddMatrix(beta, Matrix.MultiplyConst(dl, alpha));//梯度递减迭代
}
return beta;
}
验证结果