示例#1
0
        public static double[][][][] ResetSize(this LMatrix source, int depth, int row, int column)
        {
            int sampleLen = source.Length;

            double[][][][] res        = new double[sampleLen][][][];
            int            total      = depth * row * column;
            int            channelLen = row * column;

            for (int index = 0; index < sampleLen; index++)
            {
                double[] value = source[index];
                if (total != value.Length)
                {
                    throw new Exception("值不正确");
                }
                //int singleDepth = value.Length / depth;
                res[index] = new double[depth][][];
                //还原单个样本数据
                for (int sig = 0; sig < depth; sig++)
                {
                    res[index][sig] = new double[row][];
                    double[] channel = value.Skip(channelLen * sig).Take(channelLen).ToArray();
                    for (int r = 0; r < row; r++)
                    {
                        res[index][sig][r] = channel.Skip(r * column).Take(column).ToArray();
                    }
                }
            }
            return(res);
        }
示例#2
0
 public void ChangeValue(LMatrix v, double value = 0)
 {
     for (int r = 0; r < Row; r++)
     {
         for (int i = 0; i < Column; i++)
         {
             Matrix[r][i] = v[r][i] > value ? Matrix[r][i] : value;
         }
     }
 }
示例#3
0
        /// <summary>
        /// 求矩阵最大值
        /// </summary>
        /// <param name="axis">轴值或者纬度值</param>
        /// <returns></returns>
        public static LMatrix Max(this LMatrix mat, int axis = -1)
        {
            double[,] res = null;
            switch (axis)
            {
            case -1:
                //所有元素的最大值
                res = new double[1, 1];
                for (int row = 0; row < mat.Row; row++)
                {
                    for (int col = 0; col < mat.Column; col++)
                    {
                        if (res[0, 0] < mat.Matrix[row, col])
                        {
                            res[0, 0] = mat.Matrix[row, col];
                        }
                    }
                }
                break;

            case 0:
                //所有行的相同列最大值,返回相同列 【计算每一列的最大值】
                res = new double[1, mat.Column];
                for (int col = 0; col < mat.Column; col++)
                {
                    for (int row = 0; row < mat.Row; row++)
                    {
                        if (res[0, col] < mat.Matrix[row, col])
                        {
                            res[0, col] = mat.Matrix[row, col];
                        }
                    }
                }
                break;

            case 1:
                //同一行所有列的最大值 【计算每一行的最大值】
                res = new double[mat.Row, 1];
                for (int row = 0; row < mat.Row; row++)
                {
                    for (int col = 0; col < mat.Column; col++)
                    {
                        if (res[row, 0] < mat.Matrix[row, col])
                        {
                            res[row, 0] = mat.Matrix[row, col];
                        }
                    }
                }
                break;

            default:
                return(null);
            }
            return(res);
        }
示例#4
0
        /// <summary>
        /// 归一化
        /// </summary>
        public LMatrix NormalizationMaxMin()
        {
            int     row       = Matrix.Length;
            LMatrix img       = Matrix;
            LMatrix maxMatrix = img.Max(0);
            LMatrix minMatrix = img.Min(0);
            LMatrix ranges    = maxMatrix - minMatrix + 0.00001;
            var     val       = (Matrix - minMatrix) / ranges;

            return(val);
        }
示例#5
0
        /// <summary>
        /// 求矩阵均值
        /// </summary>
        /// <param name="axis">轴值或者纬度值</param>
        /// <returns></returns>
        public static LMatrix Mean(this LMatrix mat, int axis = -1)
        {
            double[,] res = null;
            double value = 0;

            switch (axis)
            {
            case -1:
                //所有元素的平均值
                res = new double[1, 1];
                for (int row = 0; row < mat.Row; row++)
                {
                    for (int col = 0; col < mat.Column; col++)
                    {
                        value += mat.Matrix[row, col];
                    }
                }
                res[0, 0] = value / mat.Total;
                break;

            case 0:
                //所有行的相同列平均值,返回相同列 【计算每一列的平均值】
                res = new double[1, mat.Column];
                for (int col = 0; col < mat.Column; col++)
                {
                    value = 0;
                    for (int row = 0; row < mat.Row; row++)
                    {
                        value += mat.Matrix[row, col];
                    }
                    res[0, col] = value / mat.Row;
                }
                break;

            case 1:
                //同一行所有列的平均值 【计算每一行的平均值】
                res = new double[mat.Row, 1];
                for (int row = 0; row < mat.Row; row++)
                {
                    value = 0;
                    for (int col = 0; col < mat.Column; col++)
                    {
                        value += mat.Matrix[row, col];
                    }
                    res[row, 0] = value / mat.Column;
                }
                break;

            default:
                return(null);
            }
            return(res);
        }
示例#6
0
        /// <summary>
        /// 复制出来一个相同值的矩阵
        /// </summary>
        /// <param name="a"></param>
        /// <returns></returns>
        public static LMatrix Copy(this LMatrix a)
        {
            var res = new double[a.Row, a.Column];

            for (int row = 0; row < a.Row; row++)
            {
                for (int col = 0; col < a.Column; col++)
                {
                    res[row, col] = a.Matrix[row, col];
                }
            }
            return(res);
        }
示例#7
0
        /// <summary>
        /// 展开二维数组
        /// </summary>
        /// <param name="a">二维数组</param>
        /// <returns></returns>
        public static double[] Flatten(this LMatrix a)
        {
            var flat = new double[a.Total];

            for (var i = 0; i < a.Row; i++)
            {
                for (var j = 0; j < a.Column; j++)
                {
                    flat[i * a.Column + j] = a.Matrix[i, j];
                }
            }
            return(flat);
        }
示例#8
0
        public static double Sum(this LMatrix mat)
        {
            double value = 0;

            for (int row = 0; row < mat.Row; row++)
            {
                for (int col = 0; col < mat.Column; col++)
                {
                    value += mat.Matrix[row, col];
                }
            }
            return(value);
        }
示例#9
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;
                }
            }
        }
示例#10
0
 /// <summary>
 /// 二维数组重新设置大小
 /// </summary>
 /// <param name="matrix">矩阵</param>
 /// <param name="row">矩阵的行</param>
 /// <param name="column">矩阵的列</param>
 /// <returns></returns>
 public static double[,] ReShape(this LMatrix matrix, int row, int column)
 {
     if (matrix == null)
     {
         return(null);
     }
     if (matrix.Total % row != 0)
     {
         throw new Exception(string.Format("不能转换大小:{0},{1}", matrix.Total, row));
     }
     double[,] res = new double[row, column];
     for (int r = 0; r < row; r++)
     {
         for (int c = 0; c < column; c++)
         {
             int _index = r * column + c;
             res[r, c] = matrix.Matrix[_index / matrix.Column, _index % matrix.Column];
         }
     }
     return(res);
 }
示例#11
0
        /// <summary>
        /// 求矩阵标准差
        /// </summary>
        /// <param name="axis"></param>
        /// <returns></returns>
        public LMatrix Std(int axis = -1)
        {
            switch (axis)
            {
            case -1:
                //所有元素的标准差
                LMatrix  avg = Mean(-1);
                double[] res = new double[1];
                double   sum = 0;
                for (int r = 0; r < Row; r++)
                {
                    for (int c = 0; c < Column; c++)
                    {
                        sum += Math.Pow(Matrix[r][c] - avg[0][0], 2);
                    }
                }
                res[0] = Math.Sqrt(sum / (Row * Column - 1));
                return(res);

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

            case 1:
                //同一行所有列的平均值 【计算每一行的标准差】
                LMatrix    avg1 = Mean(1);
                double[][] res1 = new double[Row][];
                for (int r = 0; r < Row; r++)
                {
                    res1[r] = new double[1];
                    double sum1 = 0;
                    for (int c = 0; c < Column; c++)
                    {
                        sum1 += Math.Pow(Matrix[r][c] - avg1[r][0], 2);
                    }
                    if (sum1 == 0)
                    {
                        sum1 = 1e-7;
                    }
                    res1[r][0] = Math.Sqrt(sum1 / (Column));
                    if (double.IsNaN(res1[r][0]) || res1[r][0] == 0)
                    {
                    }
                }
                return(res1);

            default:
                return(null);
            }
        }
示例#12
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);
        }