Exemplo n.º 1
0
        public void CalculateGraphTest()
        {
            var testData   = MatlabReader.ReadAll <double>("data.mat")["Data"];
            var testResult = MatlabReader.ReadAll <double>("testResult.mat")["SecPathKJ"];

            Algorithm algorithm = new Algorithm();

            Vector <double> result = Vector <double> .Build.Dense(512, 0);

            //for (int i = 0; i < testData.RowCount; i = i + 512)
            //{
            for (int j = 0; j < 1; j++)
            {
                //var chanData = testData.Column(j, i, 512);
                //var outData = testData.Column(16, i, 512);

                var chanData = testData.Column(j);
                var outData  = testData.Column(16);

                result = algorithm.Nlms(outData, chanData, 512, 0.1);
            }
            //}

            for (int i = 0; i < testResult.RowCount; i++)
            {
                if (Math.Abs(testResult[i, 0] - result[i]) > 1e-10)
                {
                    Assert.Fail();
                }
            }
        }
Exemplo n.º 2
0
        public void CanReadNamedMatrices()
        {
            var matrices = MatlabReader.ReadAll <double>("./data/Matlab/collection.mat", "Ad", "Au64");

            Assert.AreEqual(2, matrices.Count);
            foreach (var matrix in matrices)
            {
                Assert.AreEqual(typeof(LinearAlgebra.Double.DenseMatrix), matrix.Value.GetType());
            }
        }
Exemplo n.º 3
0
        public void CanReadFloatAllMatrices()
        {
            var matrices = MatlabReader.ReadAll <float>("./data/Matlab/collection.mat");

            Assert.AreEqual(30, matrices.Count);
            foreach (var matrix in matrices)
            {
                Assert.AreEqual(typeof(LinearAlgebra.Single.DenseMatrix), matrix.Value.GetType());
            }
        }
Exemplo n.º 4
0
        /// <summary>
        /// Lee una matriz con  una matriz de entrada  de filas como  numero de muestras y columnas como cararateristicas
        /// </summary>
        /// <param name="path">Ruta al archivo .mat</param>
        /// <param name="InputKey"> nombre de la matriz de caracteristicas en matlab</param>
        /// <param name="TargetKey">nombre de la matriz (n x 1) de targets en Matlab</param>
        /// <returns>entrega una lista con los registros de eentradas y objetivo</returns>
        public static List <NetEvaluation.InputAndTarget> ReadDataMatlabMatrix(string path, string InputKey, string TargetKey)
        {
            Dictionary <string, Matrix <float> > ms   = MatlabReader.ReadAll <float>(path);
            List <NetEvaluation.InputAndTarget>  list = new List <NetEvaluation.InputAndTarget>();

            for (int i = 0; i < ms.Values.First().RowCount; i++)
            {
                list.Add(new NetEvaluation.InputAndTarget(ms[InputKey].Row(i).ToArray(), ms[TargetKey].At(i, 0)));
            }
            return(list);
        }
Exemplo n.º 5
0
        static void Main(string[] args)
        {
            if (!System.Console.IsOutputRedirected)
            {
                System.Console.Clear();
            }

            CultureInfo.CurrentCulture = CultureInfo.CreateSpecificCulture("en-US");

            System.Console.WriteLine("Principal Component Analysis ex.7_pca");
            System.Console.WriteLine("=====================================\n");

            var M = Matrix <double> .Build;
            var V = Vector <double> .Build;

            //// ================== Part 1: Load Example Dataset  ===================
            //  We start this exercise by using a small dataset that is easily to
            //  visualize
            //

            // read all matrices of a file by name into a dictionary
            Dictionary <string, Matrix <double> > mr = MatlabReader.ReadAll <double>("data\\ex7data1.mat");

            // loads dataset
            System.Console.WriteLine("Loading dataset....\n");
            Matrix <double> X = mr["X"];

            System.Console.WriteLine(X);

            //// =============== Part 2: Principal Component Analysis ===============
            //  You should now implement PCA, a dimension reduction technique. You
            //  should complete the code in pca.m
            //

            System.Console.WriteLine("\nRunning PCA on example dataset.\n\n");

            //  Before running PCA, it is important to first normalize X
            System.Console.WriteLine("Features normalization...");
            (Matrix <double> X_norm, Matrix <double> mu, Matrix <double> sigma)norm_res;
            norm_res = FeatureNormalize(X);

            System.Console.WriteLine(norm_res.X_norm);

            //  Run PCA
            (Matrix <double> U, Vector <double> S)pca_res;
            pca_res = pca(norm_res.X_norm);

            System.Console.WriteLine("Top eigenvector: \n");
            System.Console.WriteLine(" U(:,1) = {0:f6} {1:f6} \n", pca_res.U[0, 0], pca_res.U[1, 0]);
            System.Console.WriteLine("\n(you should expect to see -0.707107 -0.707107)\n");

            Pause();
        }
Exemplo n.º 6
0
 public void CanReadFloatAllMatrices()
 {
     using (var stream = TestData.Data.ReadStream("Matlab.collection.mat"))
     {
         var matrices = MatlabReader.ReadAll <float>(stream);
         Assert.AreEqual(30, matrices.Count);
         foreach (var matrix in matrices)
         {
             Assert.AreEqual(typeof(LinearAlgebra.Single.DenseMatrix), matrix.Value.GetType());
         }
     }
 }
Exemplo n.º 7
0
        public void CanWriteDoubleMatrices()
        {
            Matrix <double> mat1 = Matrix <double> .Build.Dense(5, 5);

            for (var i = 0; i < mat1.ColumnCount; i++)
            {
                mat1[i, i] = i + .1;
            }

            Matrix <double> mat2 = Matrix <double> .Build.Dense(4, 5);

            for (var i = 0; i < mat2.RowCount; i++)
            {
                mat2[i, i] = i + .1;
            }

            Matrix <double> mat3 = Matrix <double> .Build.Sparse(5, 4);

            mat3[0, 0] = 1.1;
            mat3[0, 2] = 2.2;
            mat3[4, 3] = 3.3;

            Matrix <double> mat4 = Matrix <double> .Build.Sparse(3, 5);

            mat4[0, 0] = 1.1;
            mat4[0, 2] = 2.2;
            mat4[2, 4] = 3.3;

            Matrix <double>[] write = { mat1, mat2, mat3, mat4 };
            string[]          names = { "mat1", "dense_matrix_2", "s1", "sparse2" };

            if (File.Exists("testd.mat"))
            {
                File.Delete("testd.mat");
            }

            MatlabWriter.Write("testd.mat", write, names);

            var read = MatlabReader.ReadAll <double>("testd.mat", names);

            Assert.AreEqual(write.Length, read.Count);

            for (var i = 0; i < write.Length; i++)
            {
                var w = write[i];
                var r = read[names[i]];
                Assert.AreEqual(w.RowCount, r.RowCount);
                Assert.AreEqual(w.ColumnCount, r.ColumnCount);
                Assert.IsTrue(w.Equals(r));
            }

            File.Delete("testd.mat");
        }
Exemplo n.º 8
0
 public void CanReadNamedMatrices()
 {
     using (var stream = TestData.Data.ReadStream("Matlab.collection.mat"))
     {
         var matrices = MatlabReader.ReadAll <double>(stream, "Ad", "Au64");
         Assert.AreEqual(2, matrices.Count);
         foreach (var matrix in matrices)
         {
             Assert.AreEqual(typeof(LinearAlgebra.Double.DenseMatrix), matrix.Value.GetType());
         }
     }
 }
Exemplo n.º 9
0
        public void CanWriteComplexMatrices()
        {
            var mat1 = Matrix <Complex> .Build.Dense(5, 3);

            for (var i = 0; i < mat1.ColumnCount; i++)
            {
                mat1[i, i] = new Complex(i + .1, i + .1);
            }

            var mat2 = Matrix <Complex> .Build.Dense(4, 5);

            for (var i = 0; i < mat2.RowCount; i++)
            {
                mat2[i, i] = new Complex(i + .1, i + .1);
            }

            var mat3 = Matrix <Complex> .Build.Sparse(5, 4);

            mat3[0, 0] = new Complex(1.1, 1.1);
            mat3[0, 2] = new Complex(2.2, 2.2);
            mat3[4, 3] = new Complex(3.3, 3.3);

            var mat4 = Matrix <Complex> .Build.Sparse(3, 5);

            mat4[0, 0] = new Complex(1.1, 1.1);
            mat4[0, 2] = new Complex(2.2, 2.2);
            mat4[2, 4] = new Complex(3.3, 3.3);

            Matrix <Complex>[] write = { mat1, mat2, mat3, mat4 };
            string[]           names = { "mat1", "dense_matrix_2", "s1", "sparse2" };

            if (File.Exists("testz.mat"))
            {
                File.Delete("testz.mat");
            }

            MatlabWriter.Write("testz.mat", write, names);

            var read = MatlabReader.ReadAll <Complex>("testz.mat", names);

            Assert.AreEqual(write.Length, read.Count);

            for (var i = 0; i < write.Length; i++)
            {
                var w = write[i];
                var r = read[names[i]];
                Assert.AreEqual(w.RowCount, r.RowCount);
                Assert.AreEqual(w.ColumnCount, r.ColumnCount);
                Assert.IsTrue(w.Equals(r));
            }

            File.Delete("testz.mat");
        }
Exemplo n.º 10
0
        public void CanReadNamedMatrix()
        {
            var matrices = MatlabReader.ReadAll <double>("./data/Matlab/collection.mat", "Ad");

            Assert.AreEqual(1, matrices.Count);
            var ad = matrices["Ad"];

            Assert.AreEqual(100, ad.RowCount);
            Assert.AreEqual(100, ad.ColumnCount);
            AssertHelpers.AlmostEqual(100.431635988639, ad.FrobeniusNorm(), 5);
            Assert.AreEqual(typeof(LinearAlgebra.Double.DenseMatrix), ad.GetType());
        }
Exemplo n.º 11
0
 public void CanReadFloatNamedMatrix()
 {
     using (var stream = TestData.Data.ReadStream("Matlab.collection.mat"))
     {
         var matrices = MatlabReader.ReadAll <float>(stream, "Ad");
         Assert.AreEqual(1, matrices.Count);
         var ad = matrices["Ad"];
         Assert.AreEqual(100, ad.RowCount);
         Assert.AreEqual(100, ad.ColumnCount);
         AssertHelpers.AlmostEqual(100.431635988639f, ad.FrobeniusNorm(), 6);
         Assert.AreEqual(typeof(LinearAlgebra.Single.DenseMatrix), ad.GetType());
     }
 }
Exemplo n.º 12
0
        public void CanReadComplex32AllMatrices()
        {
            var matrices = MatlabReader.ReadAll <Complex32>("./data/Matlab/complex.mat");

            Assert.AreEqual(3, matrices.Count);
            foreach (var matrix in matrices)
            {
                Assert.AreEqual(typeof(LinearAlgebra.Complex32.DenseMatrix), matrix.Value.GetType());
            }

            var a = matrices["a"];

            Assert.AreEqual(100, a.RowCount);
            Assert.AreEqual(100, a.ColumnCount);
            AssertHelpers.AlmostEqual(27.232498979698409, a.L2Norm(), 5);
        }
Exemplo n.º 13
0
        public void CanReadSparseComplex32AllMatrices()
        {
            var matrices = MatlabReader.ReadAll <Complex32>("./data/Matlab/sparse_complex.mat");

            Assert.AreEqual(3, matrices.Count);
            foreach (var matrix in matrices)
            {
                Assert.AreEqual(typeof(LinearAlgebra.Complex32.SparseMatrix), matrix.Value.GetType());
            }

            var a = matrices["sa"];

            Assert.AreEqual(100, a.RowCount);
            Assert.AreEqual(100, a.ColumnCount);
            AssertHelpers.AlmostEqual(13.223654390985379, a.L2Norm(), 5);
        }
Exemplo n.º 14
0
        public void CanReadSparseComplexAllMatrices()
        {
            using (var stream = TestData.Data.ReadStream("Matlab.sparse_complex.mat"))
            {
                var matrices = MatlabReader.ReadAll <Complex>(stream);
                Assert.AreEqual(3, matrices.Count);
                foreach (var matrix in matrices)
                {
                    Assert.AreEqual(typeof(LinearAlgebra.Complex.SparseMatrix), matrix.Value.GetType());
                }

                var a = matrices["sa"];

                Assert.AreEqual(100, a.RowCount);
                Assert.AreEqual(100, a.ColumnCount);
                AssertHelpers.AlmostEqual(13.223654390985379, a.L2Norm(), 13);
            }
        }
Exemplo n.º 15
0
        public void CanReadComplexAllMatrices()
        {
            using (var stream = TestData.Data.ReadStream("Matlab.complex.mat"))
            {
                var matrices = MatlabReader.ReadAll <Complex>(stream);
                Assert.AreEqual(3, matrices.Count);
                foreach (var matrix in matrices)
                {
                    Assert.AreEqual(typeof(LinearAlgebra.Complex.DenseMatrix), matrix.Value.GetType());
                }

                var a = matrices["a"];

                Assert.AreEqual(100, a.RowCount);
                Assert.AreEqual(100, a.ColumnCount);
                AssertHelpers.AlmostEqual(27.232498979698409, a.L2Norm(), 13);
            }
        }
Exemplo n.º 16
0
        static void Main(string[] args)
        {
            if (!System.Console.IsOutputRedirected)
            {
                System.Console.Clear();
            }

            CultureInfo.CurrentCulture = CultureInfo.CreateSpecificCulture("en-US");

            var M = Matrix <double> .Build;
            var V = Vector <double> .Build;


            //// =============== Part 1: Loading and Visualizing Data ================
            //  We start the exercise by first loading and visualizing the dataset.
            //  The following code will load the dataset into your environment and plot
            //  the data.
            //

            System.Console.WriteLine("Loading and Visualizing Data ...\n");

            // Load from ex6data1:
            // You will have X, y in your environment
            Dictionary <string, Matrix <double> > ms = MatlabReader.ReadAll <double>("data\\ex6data1.mat");

            Matrix <double> X = ms["X"];                 // 51 X 2
            Vector <double> y = ms["y"].Column(0);       // 51 X 1

            // Plot training data
            GnuPlot.HoldOn();
            PlotData(X, y);

            Pause();

            //// ==================== Part 2: Training Linear SVM ====================
            //  The following code will train a linear SVM on the dataset and plot the
            //  decision boundary learned.
            //

            System.Console.WriteLine("\nTraining Linear SVM ...\n");

            // You should try to change the C value below and see how the decision
            // boundary varies (e.g., try C = 1000)
            double C            = 1.0;
            var    linearKernel = KernelHelper.LinearKernel();

            List <List <double> > libSvmData = ConvertToLibSvmFormat(X, y);
            svm_problem           prob       = ProblemHelper.ReadProblem(libSvmData);
            var svc = new C_SVC(prob, linearKernel, C);

            PlotBoundary(X, svc);
            GnuPlot.HoldOff();

            System.Console.WriteLine();

            Pause();

            //// =============== Part 3: Implementing Gaussian Kernel ===============
            //  You will now implement the Gaussian kernel to use
            //  with the SVM. You should complete the code in gaussianKernel.m
            //

            System.Console.WriteLine("\nEvaluating the Gaussian Kernel ...\n");

            double sigma = 2.0;
            double sim   = GaussianKernel(
                V.DenseOfArray(new [] { 1.0, 2, 1 }),
                V.DenseOfArray(new [] { 0.0, 4, -1 }),
                sigma
                );

            System.Console.WriteLine("Gaussian Kernel between x1 = [1; 2; 1], x2 = [0; 4; -1], sigma = {0:f6} :\n\t{1:f6}\n(for sigma = 2, this value should be about 0.324652)\n", sigma, sim);

            Pause();

            //// =============== Part 4: Visualizing Dataset 2 ================
            //  The following code will load the next dataset into your environment and
            //  plot the data.
            //

            System.Console.WriteLine("Loading and Visualizing Data ...\n");

            // Load from ex6data2:
            // You will have X, y in your environment
            ms = MatlabReader.ReadAll <double>("data\\ex6data2.mat");

            X = ms["X"];                 // 863 X 2
            y = ms["y"].Column(0);       // 863 X 1

            // Plot training data
            GnuPlot.HoldOn();
            PlotData(X, y);

            Pause();

            //// ========== Part 5: Training SVM with RBF Kernel (Dataset 2) ==========
            //  After you have implemented the kernel, we can now use it to train the
            //  SVM classifier.
            //

            System.Console.WriteLine("\nTraining SVM with RBF Kernel (this may take 1 to 2 minutes) ...\n");

            // SVM Parameters
            C     = 1;
            sigma = 0.1;
            double gamma = 1 / (2 * sigma * sigma);

            var rbfKernel = KernelHelper.RadialBasisFunctionKernel(gamma);

            libSvmData = ConvertToLibSvmFormat(X, y);
            prob       = ProblemHelper.ReadProblem(libSvmData);
            svc        = new C_SVC(prob, rbfKernel, C);


            PlotBoundary(X, svc);
            GnuPlot.HoldOff();

            Pause();

            double acc = svc.GetCrossValidationAccuracy(10);

            System.Console.WriteLine("\nCross Validation Accuracy: {0:f6}\n", acc);

            Pause();

            //// =============== Part 6: Visualizing Dataset 3 ================
            //  The following code will load the next dataset into your environment and
            //  plot the data.
            //

            System.Console.WriteLine("Loading and Visualizing Data ...\n");

            // Load from ex6data2:
            // You will have X, y in your environment
            ms = MatlabReader.ReadAll <double>("data\\ex6data3.mat");

            Matrix <double> Xval;
            Vector <double> yval;

            X    = ms["X"];              // 211 X 2
            y    = ms["y"].Column(0);    // 211 X 1
            Xval = ms["Xval"];           // 200 X 2
            yval = ms["yval"].Column(0); // 200 X 1

            // Plot training data
            GnuPlot.HoldOn();
            PlotData(X, y);

            //// ========== Part 7: Training SVM with RBF Kernel (Dataset 3) ==========

            //  This is a different dataset that you can use to experiment with. Try
            //  different values of C and sigma here.
            //


            (C, sigma) = Dataset3Params(X, y, Xval, yval);

            gamma     = 1 / (2 * sigma * sigma);
            rbfKernel = KernelHelper.RadialBasisFunctionKernel(gamma);

            libSvmData = ConvertToLibSvmFormat(X, y);
            prob       = ProblemHelper.ReadProblem(libSvmData);
            svc        = new C_SVC(prob, rbfKernel, C);

            PlotBoundary(X, svc);

            GnuPlot.HoldOff();
            Pause();
        }
Exemplo n.º 17
0
        public void KalmanFilteringTest()
        {
            var vectorBuilder = Vector <double> .Build;
            var matrixBuild   = Matrix <double> .Build;

            var resultPositon = vectorBuilder.Dense(6, 0);

            resultPositon[0] = -0.11712893894299961;
            resultPositon[1] = -0.0085413873210193024;
            resultPositon[2] = -0.00034048259997198428;
            resultPositon[3] = 0.47421163145835582;
            resultPositon[4] = -0.00690331375794314;
            resultPositon[5] = -0.00027109000134432605;

            double[,] prevariance =
            {
                {
                    0.242766594755489, 0.0523105449659795, 0.00101097341848172, 7.62628147622445e-09,
                    1.83912407997933e-06, 0.000104585919975806
                },
                {
                    0.0523105449659795, 7.99334660284189, 0.220587578642543, 1.83912407997933e-06, 0.000460038020961047,
                    0.0287783990337413
                },
                {
                    0.00101097341848172, 0.220587578642543, 10.3999959410456, 0.000104585919975806, 0.0287783990337413,
                    2.39999917381918
                },
                {
                    7.62628147622445e-09, 1.83912407997933e-06, 0.000104585919975806, 0.242766594755489,
                    0.0523105449659795, 0.00101097341848172
                },
                {
                    1.83912407997933e-06, 0.000460038020961047, 0.0287783990337413, 0.0523105449659795,
                    7.99334660284189, 0.220587578642543
                },
                {
                    0.000104585919975806, 0.0287783990337413, 2.39999917381918, 0.00101097341848172, 0.220587578642543,
                    10.3999959410456
                }
            };

            var varianceTemp = matrixBuild.DenseOfArray(prevariance);

            var m1 = MatlabReader.ReadAll <double>("testData.mat");

            KalmanFiltering kalmanFiltering = new KalmanFiltering();

            foreach (Matrix <double> testData in m1.Values)
            {
                var result = kalmanFiltering.TraceTableEstablishment(testData);
                kalmanFiltering.PrePosition[0] = -0.0950698731643758;
                kalmanFiltering.PrePosition[1] = 0.00483652853477623;
                kalmanFiltering.PrePosition[2] = 9.61518040733592e-05;
                kalmanFiltering.PrePosition[3] = 0.487928863356650;
                kalmanFiltering.PrePosition[4] = 0.00140521891949567;
                kalmanFiltering.PrePosition[5] = 3.61578884580855e-05;


                kalmanFiltering.PreVarience = varianceTemp;
                kalmanFiltering.Tracking(result);

                Assert.AreEqual(resultPositon, kalmanFiltering.CurrentPosition);
            }
        }
Exemplo n.º 18
0
        static void Main(string[] args)
        {
            if (!System.Console.IsOutputRedirected)
            {
                System.Console.Clear();
            }

            CultureInfo.CurrentCulture = CultureInfo.CreateSpecificCulture("en-US");

            System.Console.WriteLine("Multi-class Classification and Neural Networks ex.3");
            System.Console.WriteLine("================================================\n");

            var M = Matrix <double> .Build;
            var V = Vector <double> .Build;

            // read all matrices of a file by name into a dictionary
            Dictionary <string, Matrix <double> > ms = MatlabReader.ReadAll <double>("data\\ex3data1.mat");

            Matrix <double> X = ms["X"];
            Vector <double> y = ms["y"].Column(0);

            // get a casual sequence of 100 int numbers
            var srs = new MathNet.Numerics.Random.SystemRandomSource();
            var seq = srs.NextInt32Sequence(0, 5000).Take(100).ToList();

            // Randomly select 100 data points to display
            Vector <double>[] sel = new Vector <double> [100];
            int             idx   = 0;
            Vector <double> v     = V.Dense(400);

            foreach (int i in seq)
            {
                sel[idx++] = X.Row(i);
            }

            // display
            DisplayData(sel);

            Pause();

            //// ============ Part 2a: Vectorize Logistic Regression ============
            //  In this part of the exercise, you will reuse your logistic regression
            //  code from the last exercise. You task here is to make sure that your
            //  regularized logistic regression implementation is vectorized. After
            //  that, you will implement one-vs-all classification for the handwritten
            //  digit dataset.
            //

            // Test case for lrCostFunction
            System.Console.WriteLine("\nTesting Cost Function with regularization");

            Vector <double> theta_t = V.DenseOfArray(new[] { -2.0, -1, 1, 2 });

            Matrix <double> X_t = M.DenseOfArray(new [, ] {
                { 1.0, 0.1, 0.6, 1.1 },
                { 1.0, 0.2, 0.7, 1.2 },
                { 1.0, 0.3, 0.8, 1.3 },
                { 1.0, 0.4, 0.9, 1.4 },
                { 1.0, 0.5, 1.0, 1.5 },
            });
            Vector <Double> y_t      = V.DenseOfArray(new [] { 1.0, 0, 1, 0, 1 });
            int             lambda_t = 3;

            LogisticRegression lr = new LogisticRegression(X_t, y_t);

            lr.Lambda = lambda_t;
            double          J    = lr.Cost(theta_t);
            Vector <double> grad = lr.Gradient(theta_t);

            System.Console.WriteLine("\nCost: {0:f5}\n", J);
            System.Console.WriteLine("Expected cost: 2.534819\n");
            System.Console.WriteLine("Gradients:\n");
            System.Console.WriteLine(" {0:f5} \n", grad);
            System.Console.WriteLine("Expected gradients:\n");
            System.Console.WriteLine(" 0.146561\n -0.548558\n 0.724722\n 1.398003\n");

            Pause();

            //// ============ Part 2b: One-vs-All Training ============
            System.Console.WriteLine("\nTraining One-vs-All Logistic Regression...\n");

            double          lambda     = 0.1;
            int             num_labels = 10;
            Matrix <double> all_theta  = OneVsAll(X, y, num_labels, lambda);

            Pause();

            // ================ Part 3: Predict for One-Vs-All ================

            Vector <double> pred = PredictOneVsAll(all_theta, X);
            Vector <double> comp = V.Dense(y.Count);

            for (int i = 0; i < y.Count; i++)
            {
                if (pred[i] == y[i])
                {
                    comp[i] = 1;
                }
                else
                {
                    comp[i] = 0;
                }
            }


            double accuracy = comp.Mean() * 100;

            System.Console.WriteLine("\nTraining Set Accuracy: {0:f5}\n", accuracy);

            Pause();
        }
Exemplo n.º 19
0
        static void Main(string[] args)
        {
            if (!System.Console.IsOutputRedirected)
            {
                System.Console.Clear();
            }

            CultureInfo.CurrentCulture = CultureInfo.CreateSpecificCulture("en-US");

            System.Console.WriteLine("Regularized Linear Regression and Bias v.s. Variance ex.5");
            System.Console.WriteLine("=========================================================\n");

            var M = Matrix <double> .Build;
            var V = Vector <double> .Build;

            // =========== Part 1: Loading and Visualizing Data =============
            // We start the exercise by first loading and visualizing the dataset.
            // The following code will load the dataset into your environment and plot
            // the data.


            // Load Training Data
            System.Console.WriteLine("Loading and Visualizing Data ...\n");

            // Load from ex5data1:
            // You will have X, y, Xval, yval, Xtest, ytest in your environment
            Dictionary <string, Matrix <double> > ms = MatlabReader.ReadAll <double>("data\\ex5data1.mat");

            Matrix <double> X     = ms["X"];
            Vector <double> y     = ms["y"].Column(0);
            Matrix <double> Xval  = ms["Xval"];
            Vector <double> yval  = ms["yval"].Column(0);
            Matrix <double> Xtest = ms["Xtest"];
            Vector <double> ytest = ms["ytest"].Column(0);

            // m = Number of examples
            int m = X.RowCount;

            GnuPlot.HoldOn();
            PlotData(X.Column(0).ToArray(), y.ToArray());

            Pause();

            // =========== Part 2: Regularized Linear Regression Cost =============
            //  You should now implement the cost function for regularized linear
            //  regression.
            Vector <double>  theta = V.Dense(2, 1.0);
            LinearRegression lr    = new LinearRegression(X.InsertColumn(0, V.Dense(m, 1)), y, 1);
            double           J     = lr.Cost(theta);
            Vector <double>  grad  = lr.Gradient(theta);

            System.Console.WriteLine("Cost at theta = [1 ; 1]: {0:f6} \n(this value should be about 303.993192)\n", J);

            Pause();

            // =========== Part 3: Regularized Linear Regression Gradient =============
            //  You should now implement the gradient for regularized linear
            //  regression.

            System.Console.WriteLine("Gradient at theta = [1 ; 1]:  [{0:f6}; {1:f6}] \n(this value should be about [-15.303016; 598.250744])\n", grad[0], grad[1]);

            Pause();

            // =========== Part 4: Train Linear Regression =============
            //  Once you have implemented the cost and gradient correctly, the
            //  trainLinearReg function will use your cost function to train
            //  regularized linear regression.
            //
            //  Write Up Note: The data is non-linear, so this will not give a great
            //                 fit.
            //

            //  Train linear regression with lambda = 0
            lr = new LinearRegression(X.InsertColumn(0, V.Dense(m, 1)), y, 0);
            var result = lr.Train();

            Vector <double> h = X.InsertColumn(0, V.Dense(m, 1)) * result.MinimizingPoint;  // hypothesys

            PlotLinearFit(X.Column(0).ToArray(), h.ToArray());
            GnuPlot.HoldOff();

            Pause();

            // =========== Part 5: Learning Curve for Linear Regression =============
            //  Next, you should implement the learningCurve function.
            //
            //  Write Up Note: Since the model is underfitting the data, we expect to
            //                 see a graph with "high bias" -- Figure 3 in ex5.pdf
            //
            (Vector <double> error_train, Vector <double> error_val)res;
            res = LearningCurve(X.InsertColumn(0, V.Dense(m, 1)), y, Xval.InsertColumn(0, V.Dense(Xval.RowCount, 1)), yval, 0);
            PlotLinearLearningCurve(
                Generate.LinearRange(1, 1, m),
                res.error_train.ToArray(),
                res.error_val.ToArray()
                );

            System.Console.WriteLine("# Training Examples\tTrain Error\tCross Validation Error\n");

            for (int i = 0; i < m; i++)
            {
                System.Console.WriteLine("\t{0,2}\t\t{1:f6}\t{2:f6}", i, res.error_train[i], res.error_val[i]);
            }
            System.Console.WriteLine();

            Pause();

            // =========== Part 6: Feature Mapping for Polynomial Regression =============
            //  One solution to this is to use polynomial regression. You should now
            //  complete polyFeatures to map each example into its powers
            //

            int p = 8;

            // Map X onto Polynomial Features and Normalize
            Matrix <double> X_poly = MapPolyFeatures(X, p);

            // normalize
            var norm = FeatureNormalize(X_poly);

            X_poly = norm.X_norm;

            // add one's
            X_poly = X_poly.InsertColumn(0, V.Dense(X_poly.RowCount, 1));

            // Map X_poly_test and normalize (using mu and sigma)
            Matrix <double> X_poly_test = MapPolyFeatures(Xtest, p);

            for (int i = 0; i < X_poly_test.ColumnCount; i++)
            {
                Vector <double> v = X_poly_test.Column(i);
                v = v - norm.mu[0, i];
                v = v / norm.sigma[0, i];
                X_poly_test.SetColumn(i, v);
            }

            // add one's
            X_poly_test = X_poly_test.InsertColumn(0, V.Dense(X_poly_test.RowCount, 1));

            // Map X_poly_val and normalize (using mu and sigma)
            Matrix <double> X_poly_val = MapPolyFeatures(Xval, p);

            for (int i = 0; i < X_poly_val.ColumnCount; i++)
            {
                Vector <double> v = X_poly_val.Column(i);
                v = v - norm.mu[0, i];
                v = v / norm.sigma[0, i];
                X_poly_val.SetColumn(i, v);
            }

            // add one's
            X_poly_val = X_poly_val.InsertColumn(0, V.Dense(X_poly_val.RowCount, 1));

            System.Console.WriteLine("Normalized Training Example 1:\n");
            System.Console.WriteLine(X_poly.Row(0));

            Pause();

            // =========== Part 7: Learning Curve for Polynomial Regression =============
            //  Now, you will get to experiment with polynomial regression with multiple
            //  values of lambda. The code below runs polynomial regression with
            //  lambda = 0. You should try running the code with different values of
            //  lambda to see how the fit and learning curve change.
            //

            double lambda = 0;

            lr = new LinearRegression(X_poly, y, lambda);
            var minRes = lr.Train();

            GnuPlot.HoldOn();
            GnuPlot.Set("terminal wxt 1");
            PlotData(X.Column(0).ToArray(), y.ToArray());
            PlotFit(X.Column(0).Minimum(), X.Column(0).Maximum(), norm.mu.Row(0), norm.sigma.Row(0), minRes.MinimizingPoint, p, lambda);
            GnuPlot.HoldOff();

            // learning curve
            GnuPlot.Set("terminal wxt 2");

            res = LearningCurve(X_poly, y, X_poly_val, yval, lambda);
            PlotLinearLearningCurve(
                Generate.LinearRange(1, 1, m),
                res.error_train.ToArray(),
                res.error_val.ToArray()
                );

            System.Console.WriteLine("# Training Examples\tTrain Error\tCross Validation Error\n");

            for (int i = 0; i < m; i++)
            {
                System.Console.WriteLine("\t{0,2}\t\t{1:f6}\t{2:f6}", i, res.error_train[i], res.error_val[i]);
            }
            System.Console.WriteLine();

            Pause();

            // =========== Part 8: Validation for Selecting Lambda =============
            //  You will now implement validationCurve to test various values of
            //  lambda on a validation set. You will then use this to select the
            //  "best" lambda value.
            //
            var resVal = ValidationCurve(X_poly, y, X_poly_val, yval);

            PlotValidationCurve(resVal.Lamda_vec.ToArray(), resVal.error_train.ToArray(), resVal.error_val.ToArray());

            System.Console.WriteLine("# Lambda\tTrain Error\tCross Validation Error\n");

            for (int i = 0; i < resVal.error_train.Count; i++)
            {
                System.Console.WriteLine("\t{0:f4}\t\t{1:f6}\t{2:f6}", resVal.Lamda_vec[i], resVal.error_train[i], resVal.error_val[i]);
            }
            System.Console.WriteLine();


            Pause();

            // =========== Part 9: Compute test set error =============
            // Compute the test error using the best value of λ you
            // found
            lambda = 3.0;
            lr     = new LinearRegression(X_poly, y, lambda);
            minRes = lr.Train();
            theta  = minRes.MinimizingPoint;

            h = X_poly_test * theta;
            m = X_poly.RowCount;

            System.Console.WriteLine("Evaluating test-set:\n");
            System.Console.WriteLine("# \tHypothesis\tExpected\tError\n");
            for (int i = 0; i < m; i++)
            {
                System.Console.WriteLine("{0,3}\t{1:f6}\t{2:f6}\t{3:f6}", i + 1, h[i], ytest[i], h[i] - ytest[i]);
            }

            double mae  = (h - ytest).L1Norm();     // Mean Absolute Error
            double mse  = (h - ytest).L2Norm();     // Mean Squared Error
            double rmse = Math.Sqrt(mse);           // Root Mean Squared Error

            System.Console.WriteLine("\nMAE on test set: {0:F6}", mae);
            System.Console.WriteLine("MSE on test set: {0:F6}", mse);
            System.Console.WriteLine("RMSE on test set: {0:F6}\n", rmse);

            Pause();

            GnuPlot.HoldOn();
            GnuPlot.Set("terminal wxt 3");
            PlotData(Xtest.Column(0).ToArray(), ytest.ToArray());
            PlotFit(Xtest.Column(0).Minimum(), Xtest.Column(0).Maximum(), norm.mu.Row(0), norm.sigma.Row(0), theta, p, lambda);
            GnuPlot.HoldOff();

            Pause();
        }
Exemplo n.º 20
0
        static void Main(string[] args)
        {
            if (!System.Console.IsOutputRedirected)
            {
                System.Console.Clear();
            }

            CultureInfo.CurrentCulture = CultureInfo.CreateSpecificCulture("en-US");

            System.Console.WriteLine("Multi-class Classification and Neural Networks ex.4");
            System.Console.WriteLine("===================================================\n");

            var M = Matrix <double> .Build;
            var V = Vector <double> .Build;

            // Setup the parameters you will use for this exercise
            int input_layer_size  = 400;  // 20x20 Input Images of Digits
            int hidden_layer_size = 25;   // 25 hidden units
            int num_labels        = 10;   // 10 labels, from 1 to 10
                                          // (note that we have mapped "0" to label 10)

            //  =========== Part 1: Loading and Visualizing Data =============
            //  We start the exercise by first loading and visualizing the dataset.
            //  You will be working with a dataset that contains handwritten digits.
            //

            // read all matrices of a file by name into a dictionary
            Dictionary <string, Matrix <double> > ms = MatlabReader.ReadAll <double>("data\\ex3data1.mat");

            Matrix <double> X = ms["X"];
            Vector <double> y = ms["y"].Column(0);

            // get a casual sequence of 100 int numbers
            var srs = new MathNet.Numerics.Random.SystemRandomSource();
            var seq = srs.NextInt32Sequence(0, 5000).Take(100).ToList();

            // Randomly select 100 data points to display
            Vector <double>[] sel = new Vector <double> [100];
            int             idx   = 0;
            Vector <double> v     = V.Dense(400);

            foreach (int i in seq)
            {
                sel[idx++] = X.Row(i);
            }

            // display
            DisplayData(sel);

            Pause();

            // ================ Part 2: Loading Parameters ================
            // In this part of the exercise, we load some pre-initialized
            // neural network parameters.

            System.Console.WriteLine("\nLoading Saved Neural Network Parameters ...\n");

            // read all matrices of a file by name into a dictionary
            Dictionary <string, Matrix <double> > mr = MatlabReader.ReadAll <double>("data\\ex3weights.mat");

            Matrix <double> theta1 = mr["Theta1"];      // 25 X 401
            Matrix <double> theta2 = mr["Theta2"];      // 10 X 26

            // Unroll parameters
            Vector <double> nn_params = NeuralNetwork.UnrollParameters(theta1, theta2);

            Pause();

            //  ================ Part 3: Compute Cost (Feedforward) ================
            //  To the neural network, you should first start by implementing the
            //  feedforward part of the neural network that returns the cost only. You
            //  should complete the code in nnCostFunction.m to return cost. After
            //  implementing the feedforward to compute the cost, you can verify that
            //  your implementation is correct by verifying that you get the same cost
            //  as us for the fixed debugging parameters.
            //
            //  We suggest implementing the feedforward cost *without* regularization
            //  first so that it will be easier for you to debug. Later, in part 4, you
            //  will get to implement the regularized cost.


            System.Console.WriteLine("\nFeedforward Using Neural Network ...\n");

            // Weight regularization parameter (we set this to 0 here).

            NeuralNetwork nn = new NeuralNetwork(X, y, input_layer_size, hidden_layer_size, num_labels);

            nn.Lambda = 0.0;
            double J = nn.Cost(nn_params);

            System.Console.WriteLine("Cost at parameters (loaded from ex4weights): {0:f6}\n(this value should be about 0.287629)\n", J);
            Pause();

            // =============== Part 4: Implement Regularization ===============
            // Once your cost function implementation is correct, you should now
            // continue to implement the regularization with the cost.
            //

            System.Console.WriteLine("\nChecking Cost Function (w/ Regularization) ... \n");

            // Weight regularization parameter (we set this to 1 here).
            nn.Lambda = 1.0;

            J = nn.Cost(nn_params);

            System.Console.WriteLine("Cost at parameters (loaded from ex4weights): {0:f6} \n(this value should be about 0.383770)\n", J);
            Pause();

            // ================ Part 5: Sigmoid Gradient  ================
            //  Before you start implementing the neural network, you will first
            //  implement the gradient for the sigmoid function. You should complete the
            //  code in the sigmoidGradient.m file.

            System.Console.WriteLine("\nEvaluating sigmoid gradient...\n");

            var g = nn.SigmoidGradient(V.DenseOfArray(new[] { -1.0, -0.5, 0, 0.5, 1 }));

            System.Console.WriteLine("Sigmoid gradient evaluated at [-1 -0.5 0 0.5 1]:\n  ");
            System.Console.WriteLine("{0:f5} ", g);
            System.Console.WriteLine("\n\n");

            Pause();


            // ================ Part 6: Initializing Pameters ================
            // In this part of the exercise, you will be starting to implment a two
            // layer neural network that classifies digits. You will start by
            // implementing a function to initialize the weights of the neural network
            // (randInitializeWeights.m)

            System.Console.WriteLine("\nInitializing Neural Network Parameters ...\n");

            Matrix <double> initial_Theta1 = RandInitializeWeights(input_layer_size + 1, hidden_layer_size);
            Matrix <double> initial_Theta2 = RandInitializeWeights(hidden_layer_size + 1, num_labels);

            // Unroll parameters
            Vector <double> initial_nn_params = NeuralNetwork.UnrollParameters(initial_Theta1, initial_Theta2);

            Pause();
            // =============== Part 7: Implement Backpropagation ===============
            // Once your cost matches up with ours, you should proceed to implement the
            // backpropagation algorithm for the neural network. You should add to the
            // code you've written in nnCostFunction.m to return the partial
            // derivatives of the parameters.

            System.Console.WriteLine("\nChecking Backpropagation... \n");
            CheckGradient(0);

            Pause();

            // =============== Part 8: Implement Regularization ===============
            //  Once your backpropagation implementation is correct, you should now
            //  continue to implement the regularization with the cost and gradient.

            System.Console.WriteLine("\nChecking Backpropagation (w/ Regularization) ... \n");

            //  Check gradients by running checkNNGradients
            double lambda = 3;

            CheckGradient(lambda);

            // Also output the costFunction debugging values
            nn.Lambda = lambda;
            double debug_J = nn.Cost(nn_params);

            System.Console.WriteLine("\n\nCost at (fixed) debugging parameters (w/ lambda = {0:f1}): {1:f6} " +
                                     "\n(for lambda = 3, this value should be about 0.576051)\n\n", lambda, debug_J);

            Pause();

            // =================== Part 8: Training NN ===================
            //  You have now implemented all the code necessary to train a neural
            //  network. To train your neural network, we will now use "fmincg", which
            //  is a function which works similarly to "fminunc". Recall that these
            //  advanced optimizers are able to train our cost functions efficiently as
            //  long as we provide them with the gradient computations.

            System.Console.WriteLine("\nTraining Neural Network... \n");

            //  After you have completed the assignment, change the MaxIter to a larger
            //  value to see how more training helps.
            int maxIter = 40;

            //  You should also try different values of lambda
            lambda    = 1;
            nn.Lambda = lambda;


            var obj    = ObjectiveFunction.Gradient(nn.Cost, nn.Gradient);
            var solver = new LimitedMemoryBfgsMinimizer(1e-5, 1e-5, 1e-5, 5, maxIter);
            var result = solver.FindMinimum(obj, initial_nn_params);

            System.Console.WriteLine("Reason For Exit: {0}", result.ReasonForExit);
            System.Console.WriteLine("Iterations: {0}", result.Iterations);
            System.Console.WriteLine("Cost: {0:e}", result.FunctionInfoAtMinimum.Value);


            Pause();

            // ================= Part 10: Implement Predict =================
            //  After training the neural network, we would like to use it to predict
            //  the labels. You will now implement the "predict" function to use the
            //  neural network to predict the labels of the training set. This lets
            //  you compute the training set accuracy.

            Vector <double> pred = nn.Predict(result.MinimizingPoint, X);
            Vector <double> comp = V.Dense(y.Count);

            for (int i = 0; i < y.Count; i++)
            {
                if (pred[i] == y[i])
                {
                    comp[i] = 1;
                }
                else
                {
                    comp[i] = 0;
                }
            }


            double accuracy = comp.Mean() * 100;

            System.Console.WriteLine("\nTraining Set Accuracy: {0:f5}\n", accuracy);

            Pause();
        }
Exemplo n.º 21
0
        static void Main(string[] args)
        {
            if (!System.Console.IsOutputRedirected)
            {
                System.Console.Clear();
            }

            CultureInfo.CurrentCulture = CultureInfo.CreateSpecificCulture("en-US");

            System.Console.WriteLine("Neural Networks ex.3_nn");
            System.Console.WriteLine("==================================================\n");

            var M = Matrix <double> .Build;
            var V = Vector <double> .Build;

            //// =========== Part 1: Loading and Visualizing Data =============
            //   We start the exercise by first loading and visualizing the dataset.
            //   You will be working with a dataset that contains handwritten digits.
            //

            // Load Training Data
            System.Console.WriteLine("Loading and Visualizing Data ...\n");

            // read all matrices of a file by name into a dictionary
            Dictionary <string, Matrix <double> > mr = MatlabReader.ReadAll <double>("data\\ex3data1.mat");

            Matrix <double> X = mr["X"];
            Vector <double> y = mr["y"].Column(0);

            Double m = X.RowCount;

            // get a casual sequence of 100 int numbers
            var srs = new MathNet.Numerics.Random.SystemRandomSource();
            var seq = srs.NextInt32Sequence(0, 5000).Take(100).ToList();

            // Randomly select 100 data points to display
            Vector <double>[] sel = new Vector <double> [100];
            int             idx   = 0;
            Vector <double> v     = V.Dense(400);

            foreach (int i in seq)
            {
                sel[idx++] = X.Row(i);
            }

            // display
            DisplayData(sel);

            Pause();

            // ================ Part 2: Loading Pameters ================
            // In this part of the exercise, we load some pre-initialized
            // neural network parameters.

            System.Console.WriteLine("\nLoading Saved Neural Network Parameters ...\n");

            // read all matrices of a file by name into a dictionary
            mr = MatlabReader.ReadAll <double>("data\\ex3weights.mat");

            Matrix <double> theta1 = mr["Theta1"];      // 25 X 401
            Matrix <double> theta2 = mr["Theta2"];      // 10 X 26

            Pause();

            //// ================= Part 3: Implement Predict =================
            //  After training the neural network, we would like to use it to predict
            //  the labels. You will now implement the "predict" function to use the
            //  neural network to predict the labels of the training set. This lets
            //  you compute the training set accuracy.

            Vector <double> pred = Predict(theta1, theta2, X);

            Vector <double> comp = V.Dense(y.Count);

            for (int i = 0; i < y.Count; i++)
            {
                if (pred[i] == y[i])
                {
                    comp[i] = 1;
                }
                else
                {
                    comp[i] = 0;
                }
            }


            double accuracy = comp.Mean() * 100;

            System.Console.WriteLine("\nTraining Set Accuracy: {0:f5}\n", accuracy);

            //  Randomly permute examples
            seq = srs.NextInt32Sequence(0, 5000).Take(5000).ToList();

            for (int i = 0; i < m; i++)
            {
                // display
                DisplayData(new[] { X.Row(seq[i]) });

                Matrix <double> x = M.DenseOfRowVectors(new[] { X.Row(seq[i]) });
                pred = Predict(theta1, theta2, x);
                double p = pred[0];
                System.Console.WriteLine("\nNeural Network Prediction: {0:N0} (digit {1:N0})\n", p, p % 10);

                // Pause with quit option
                System.Console.WriteLine("Paused - press enter to continue, q to exit:");
                string s = Console.ReadLine();
                if (s.ToLower() == "q")
                {
                    break;
                }
            }

            Pause();
        }
Exemplo n.º 22
0
        static void Main(string[] args)
        {
            if (!System.Console.IsOutputRedirected)
            {
                System.Console.Clear();
            }

            CultureInfo.CurrentCulture = CultureInfo.CreateSpecificCulture("en-US");

            var M = Matrix <double> .Build;
            var V = Vector <double> .Build;

            //// ================= Part 1: Find Closest Centroids ====================
            //  To help you implement K-Means, we have divided the learning algorithm
            //  into two functions -- findClosestCentroids and computeCentroids. In this
            //  part, you should complete the code in the findClosestCentroids function.
            //

            System.Console.WriteLine("Finding closest centroids.\n\n");

            // Load an example dataset that we will be using
            Dictionary <string, Matrix <double> > ms = MatlabReader.ReadAll <double>("data\\ex7data2.mat");
            Matrix <double> X = ms["X"];                 // 300 X 2

            System.Console.WriteLine(X);

            // Select an initial set of centroids
            int             K = 3;                      // 3 Centroids
            Matrix <double> initial_centroids = M.DenseOfArray(new [, ] {
                { 3.0, 3.0 },
                { 6.0, 2.0 },
                { 8.0, 5.0 }
            });

            System.Console.Write("Initial centroids: ");
            System.Console.WriteLine(initial_centroids);

            Vector <double> idx = FindClosestCentroids(X, initial_centroids);


            System.Console.WriteLine("Closest centroids for the first 3 examples: \n");
            System.Console.WriteLine(idx.SubVector(0, 3));
            System.Console.WriteLine("\n(the closest centroids should be 0, 2, 1 respectively)\n");

            Pause();

            //// ===================== Part 2: Compute Means =========================
            //  After implementing the closest centroids function, you should now
            //  complete the computeCentroids function.
            //
            System.Console.WriteLine("\nComputing centroids means.\n\n");

            //  Compute means based on the closest centroids found in the previous part.
            Matrix <double> centroids;

            centroids = ComputeCentroids(X, idx, K);

            System.Console.WriteLine("Centroids computed after initial finding of closest centroids: \n");
            System.Console.WriteLine(centroids);
            System.Console.WriteLine("\nthe centroids should be");
            System.Console.WriteLine("   [ 2.428301 3.157924 ]");
            System.Console.WriteLine("   [ 5.813503 2.633656 ]");
            System.Console.WriteLine("   [ 7.119387 3.616684 ]\n");

            Pause();

            //// =================== Part 3: K-Means Clustering ======================
            //  After you have completed the two functions computeCentroids and
            //  findClosestCentroids, you have all the necessary pieces to run the
            //  kMeans algorithm. In this part, you will run the K-Means algorithm on
            //  the example dataset we have provided.
            //
            System.Console.WriteLine("\nRunning K-Means clustering on example dataset.\n\n");

            // Settings for running K-Means
            K = 3;
            int max_iters = 10;

            // For consistency, here we set centroids to specific values
            // but in practice you want to generate them automatically, such as by
            // settings them to be random examples (as can be seen in
            // kMeansInitCentroids).
            initial_centroids = M.DenseOfArray(new [, ] {
                { 3.0, 3.0 },
                { 6.0, 2.0 },
                { 8.0, 5.0 }
            });

            // Run K-Means algorithm. The 'true' at the end tells our function to plot
            // the progress of K-Means
            (Matrix <double> centroids, Vector <double> idx)kMeans = RunkMeans(X, initial_centroids, max_iters, true);
            System.Console.WriteLine("\nK-Means Done.\n\n");
            Pause();
        }