示例#1
0
        /// <summary>
        /// 正则化数据
        /// </summary>
        public void Regularization()
        {
            int     row       = Matrix.Length;
            LMatrix img       = Matrix;
            LMatrix avgMatrix = img.Mean(0);
            LMatrix stdMatrix = img.Std(0);
            int     column    = Matrix[0].Length;

            for (int r = 0; r < row; r++)
            {
                for (int c = 0; c < column; c++)
                {
                    if (stdMatrix[0][c] == 0)
                    {
                        MessageBox.Show("有空值");
                    }
                    double val = (Matrix[r][c] - avgMatrix[0][c]) / stdMatrix[0][c];
                    Matrix[r][c] = val;
                }
            }
        }
示例#2
0
        /// <summary>
        /// 求矩阵标准差
        /// </summary>
        /// <param name="axis"></param>
        /// <returns></returns>
        public static LMatrix Std(this LMatrix mat, int axis = -1)
        {
            double[,] res = null;
            LMatrix avg = null;
            double  sum = 0;

            switch (axis)
            {
            case -1:
                //所有元素的标准差
                avg = mat.Mean(-1);
                res = new double[1, 1];
                for (int r = 0; r < mat.Row; r++)
                {
                    for (int c = 0; c < mat.Column; c++)
                    {
                        sum += Math.Pow(mat.Matrix[r, c] - avg.Matrix[0, 0], 2);
                    }
                }
                if (sum == 0)
                {
                    res[0, 0] = 0;
                }
                else
                {
                    res[0, 0] = Math.Sqrt(sum / mat.Total);
                }
                break;

            case 0:
                //所有行的相同列平均值,返回相同列 【计算每一列的标准差】
                avg = mat.Mean(0);
                res = new double[1, mat.Column];
                for (int c = 0; c < mat.Column; c++)
                {
                    sum = 0;
                    for (int r = 0; r < mat.Row; r++)
                    {
                        sum += Math.Pow(mat.Matrix[r, c] - avg.Matrix[0, c], 2);
                    }
                    if (sum == 0)
                    {
                        res[0, c] = 0;
                    }
                    else
                    {
                        res[0, c] = Math.Sqrt(sum / mat.Row);
                    }
                }
                break;

            case 1:
                //同一行所有列的平均值 【计算每一行的标准差】
                avg = mat.Mean(1);
                res = new double[mat.Row, 1];
                for (int r = 0; r < mat.Row; r++)
                {
                    sum = 0;
                    for (int c = 0; c < mat.Column; c++)
                    {
                        sum += Math.Pow(mat.Matrix[r, c] - avg.Matrix[r, 0], 2);
                    }
                    if (sum == 0)
                    {
                        res[r, 0] = 0;
                    }
                    else
                    {
                        res[r, 0] = Math.Sqrt(sum / mat.Column);
                    }
                }
                break;

            default:
                return(null);
            }
            return(res);
        }