public void test_compute_class_weight()
 {
     int[] classes = new[] {2, 3, 4};
     int[] y_ind = new[] {0, 0, 0, 1, 1, 2};
     Vector cw = ClassWeightEstimator<int>.Auto.ComputeWeights(classes, y_ind);
     Assert.AreEqual(cw.Sum(), classes.Length);
     Assert.IsTrue(cw[0] < cw[1] && cw[1] < cw[2]);
 }
Example #2
0
        /// <summary>
        /// Return a column of the matrix as an array
        /// </summary>
        /// <param name="matrix">Matrix</param>
        /// <param name="id">Column id</param>
        /// <returns></returns>
        public static double[] Column(MN.DenseMatrix matrix, int id)
        {
            var col = new double[matrix.RowCount];
              for (int idx = 0; idx < matrix.RowCount; idx++)
            col[idx] = matrix[idx, id];

              return col;
        }
 public void HeapSortWithRandomIntegerArray()
 {
     var sortedIndices = new[] {5, 2, 8, 6, 0, 4, 1, 7, 3, 9};
     ILUTPElementSorter.SortIntegersDecreasing(sortedIndices);
     for (var i = 0; i < sortedIndices.Length; i++)
     {
         Assert.AreEqual(sortedIndices.Length - 1 - i, sortedIndices[i], "#01-" + i);
     }
 }
 public void test_compute_class_weight_not_present()
 {
     int[] classes = new[] {0, 1, 2, 3};
     int[] y = new[] {0, 0, 0, 1, 1, 2};
     Vector cw = ClassWeightEstimator<int>.Auto.ComputeWeights(classes, y);
     Assert.AreEqual(cw.Sum(), classes.Length);
     Assert.AreEqual(cw.Count, classes.Length);
     Assert.IsTrue(cw[0] < cw[1] && cw[1] < cw[2] && cw[2] <= cw[3]);
 }
Example #5
0
        public static bool MatrixEqual(MN.DenseMatrix a, MN.DenseMatrix b)
        {
            if (a.RowCount != b.RowCount || a.ColumnCount != b.ColumnCount)
            return false;

              for (int idx1 = 0; idx1 < a.RowCount; idx1++)
            for (int idx2 = 0; idx2 < a.ColumnCount; idx2++)
              if (a[idx1, idx2] != b[idx1, idx2])
            return false;

              return true;
        }
Example #6
0
 /// <summary>
 /// Perform a QR factorization of the matrix
 /// </summary>
 /// <param name="matrix"></param>
 /// <returns></returns>
 public static MN.DenseMatrix[] FactorQR(MN.DenseMatrix matrix)
 {
     try
       {
     var qr = new MN.Factorization.DenseQR(matrix);
     return new MN.DenseMatrix[] { qr.Q as MN.DenseMatrix, qr.R as MN.DenseMatrix };
       }
       catch
       {
     return null;
       }
 }
Example #7
0
 /// <summary>
 /// Perform a LU factorization of the matrix
 /// </summary>
 /// <param name="matrix"></param>
 /// <returns></returns>
 public static MN.DenseMatrix[] FactorLU(MN.DenseMatrix matrix)
 {
     try
       {
     var lu = new MN.Factorization.DenseLU(matrix);
     return new MN.DenseMatrix[] { lu.L as MN.DenseMatrix, lu.U as MN.DenseMatrix };
       }
       catch
       {
     return null;
       }
 }
Example #8
0
 /// <summary>
 /// Perform a SVD factorization of the matrix
 /// </summary>
 /// <param name="matrix"></param>
 /// <returns></returns>
 public static MN.DenseMatrix FactorCholessky(MN.DenseMatrix matrix)
 {
     try
       {
     var cholessky = new MN.Factorization.DenseCholesky(matrix);
     return cholessky.Factor as MN.DenseMatrix;
       }
       catch
       {
     return null;
       }
 }
Example #9
0
        /// <summary>
        /// Print a matrix
        /// </summary>
        /// <param name="matrix">Matrix</param>
        private static void PrintMatrix(MN.DenseMatrix matrix)
        {
            var padding = 2;
              var rows = matrix.RowCount;
              var cols = matrix.ColumnCount;

              // detect maximum length of each column
              var lengths = new int[cols];
              for(var idx1 = 0; idx1 < cols; idx1++)
              {
            var maxLength = 0;
            for(var idx2 = 0; idx2 < rows; idx2++)
              maxLength = Math.Max(maxLength, matrix[idx2, idx1].ToString().Length);

            lengths[idx1] = maxLength + padding;
              }

              // print matrix
              Console.WriteLine("[[");

              for(var idx1 = 0; idx1 < rows; idx1++)
              {
            for(var idx2 = 0; idx2 < cols; idx2++)
              Console.Write("{0," + lengths[idx2].ToString() + "}", matrix[idx1, idx2]);

            Console.WriteLine();
              }

              Console.WriteLine("]]");
        }
 public void HeapSortWithDuplicateEntries()
 {
     var sortedIndices = new[] {1, 1, 1, 1, 2, 2, 2, 2, 3, 4};
     ILUTPElementSorter.SortIntegersDecreasing(sortedIndices);
     for (var i = 0; i < sortedIndices.Length; i++)
     {
         if (i == 0)
         {
             Assert.AreEqual(4, sortedIndices[i], "#01-" + i);
         }
         else
         {
             if (i == 1)
             {
                 Assert.AreEqual(3, sortedIndices[i], "#01-" + i);
             }
             else
             {
                 if (i < 6)
                 {
                     if (sortedIndices[i] != 2)
                     {
                         Assert.Fail("#01-" + i);
                     }
                 }
                 else
                 {
                     if (sortedIndices[i] != 1)
                     {
                         Assert.Fail("#01-" + i);
                     }
                 }
             }
         }
     }
 }
        public void HeapSortWithSpecialConstructedIntegerArray()
        {
            var sortedIndices = new[] {0, 0, 0, 0, 0, 1, 0, 0, 0, 0};
            ILUTPElementSorter.SortIntegersDecreasing(sortedIndices);
            for (var i = 0; i < sortedIndices.Length; i++)
            {
                if (i == 0)
                {
                    Assert.AreEqual(1, sortedIndices[i], "#01-" + i);
                    break;
                }
            }

            sortedIndices = new[] {1, 0, 0, 0, 0, 0, 0, 0, 0, 0};
            ILUTPElementSorter.SortIntegersDecreasing(sortedIndices);
            for (var i = 0; i < sortedIndices.Length; i++)
            {
                if (i == 0)
                {
                    Assert.AreEqual(1, sortedIndices[i], "#02-" + i);
                    break;
                }
            }

            sortedIndices = new[] {0, 0, 0, 0, 0, 0, 0, 0, 0, 1};
            ILUTPElementSorter.SortIntegersDecreasing(sortedIndices);
            for (var i = 0; i < sortedIndices.Length; i++)
            {
                if (i == 0)
                {
                    Assert.AreEqual(1, sortedIndices[i], "#03-" + i);
                    break;
                }
            }

            sortedIndices = new[] {1, 1, 1, 0, 1, 1, 1, 1, 1, 1};
            ILUTPElementSorter.SortIntegersDecreasing(sortedIndices);
            for (var i = 0; i < sortedIndices.Length; i++)
            {
                if (i == 9)
                {
                    Assert.AreEqual(0, sortedIndices[i], "#04-" + i);
                    break;
                }
            }
        }
Example #12
0
        /// <summary>
        /// Save the matrix to file
        /// </summary>
        /// <param name="matr">Matrix</param>
        public void SaveMatrix(MN.DenseMatrix matr)
        {
            Create();

              // write matrix dimensions
              var lenBuf = BitConverter.GetBytes(matr.RowCount);
              FileStream.Write(lenBuf, 0, lenBuf.Length);

              lenBuf = BitConverter.GetBytes(matr.ColumnCount);
              FileStream.Write(lenBuf, 0, lenBuf.Length);

              // write matrix
              for (int idx = 0; idx < matr.RowCount; idx++)
              {
            for (int idx2 = 0; idx2 < matr.ColumnCount; idx2++)
            {
              var buf = BitConverter.GetBytes(matr[idx, idx2]);
              FileStream.Write(buf, 0, buf.Length);
            }
              }

              Close();
        }
Example #13
0
        /// <summary>
        /// Flatten the matrix down to an array
        /// </summary>
        /// <param name="matrix">Matrix</param>
        /// <returns></returns>
        public static double[] Flatten(MN.DenseMatrix matrix)
        {
            var array = new double[matrix.RowCount * matrix.ColumnCount];

              var idx3 = 0;
              for (int idx1 = 0; idx1 < matrix.RowCount; idx1++)
              {
            for (int idx2 = 0; idx2 < matrix.ColumnCount; idx2++)
            {
              array[idx3] = matrix[idx1, idx2];
              idx3++;
            }
              }

              return array;
        }
Example #14
0
        /// <summary>
        /// Return a row of the matrix as an array
        /// </summary>
        /// <param name="matrix">Matrix</param>
        /// <param name="id">Row id</param>
        /// <returns></returns>
        public static double[] Row(MN.DenseMatrix matrix, int id)
        {
            var row = new double[matrix.ColumnCount];
              for (int idx = 0; idx < matrix.ColumnCount; idx++)
            row[idx] = matrix[id, idx];

              return row;
        }
Example #15
0
 /// <summary>
 /// Invert a matrix if it can be inverted
 /// </summary>
 /// <param name="matrix">Matrix to invert</param>
 /// <returns></returns>
 public static MN.DenseMatrix Invert(MN.DenseMatrix matrix)
 {
     if (matrix.Determinant() == 0)
     return null;
       else
     return (MN.DenseMatrix)matrix.Inverse();
 }