Exemplo n.º 1
0
        // Plot J cost function
        private static void PlotJ(Matrix <double> X, Matrix <double> y, Matrix <double> theta)
        {
            // Grid over which we will calculate J
            double[] theta0_vals = MathNet.Numerics.Generate.LinearSpaced(100, -10, 10);
            double[] theta1_vals = MathNet.Numerics.Generate.LinearSpaced(100, -1, 4);

            // initialize J_vals to a matrix of 0's
            int size = theta0_vals.Length * theta1_vals.Length;

            double[] sx = new double[size];
            double[] sy = new double[size];
            double[] sz = new double[size];


            // Fill out J_vals
            int idx = 0;

            for (int i = 0; i < theta0_vals.Length; i++)
            {
                for (int k = 0; k < theta1_vals.Length; k++)
                {
                    Matrix <double> t = Matrix <double> .Build.Dense(2, 1);

                    t[0, 0] = theta0_vals[i];
                    t[1, 0] = theta1_vals[k];

                    sx[idx] = theta0_vals[i];
                    sy[idx] = theta1_vals[k];
                    sz[idx] = ComputeCost(X, y, t);
                    idx++;
                }
            }

            GnuPlot.HoldOn();
            GnuPlot.Set("terminal wxt 1");
            GnuPlot.Set("title \"Cost function J\"");
            GnuPlot.Set("key bottom right");
            GnuPlot.Set("xlabel \"{/Symbol q}_0\"");
            GnuPlot.Set("ylabel \"{/Symbol q}_1\"");

            // surface plot
            GnuPlot.SPlot(sx, sy, sz, "palette title \"J({/Symbol q}_0,{/Symbol q}_1)\"");

            // Contour plot
            GnuPlot.Set("terminal wxt 2");
            GnuPlot.Set("cntrparam levels auto 10", "logscale z", "xr[-10:10]", "yr[-1:4]");
            GnuPlot.Unset("key", "label");
            GnuPlot.Contour(sx, sy, sz);

            GnuPlot.Plot(new double[] { theta[0, 0] }, new double[] { theta[1, 0] }, "pt 2 ps 1 lc rgb \"red\"");
        }
Exemplo n.º 2
0
        private static void PlotBoundary(Matrix <double> x, C_SVC svc)
        {
            double min = x.Column(0).Min();
            double max = x.Column(0).Max();

            double[] x0 = MathNet.Numerics.Generate.LinearSpaced(100, min, max);

            min = x.Column(1).Min();
            max = x.Column(1).Max();

            double[] x1 = MathNet.Numerics.Generate.LinearSpaced(100, min, max);

            int size = x0.Length * x1.Length;

            double[] sx = new double[size];
            double[] sy = new double[size];
            double[] sz = new double[size];

            int idx = 0;

            for (int i = 0; i < x0.Length; i++)
            {
                for (int j = 0; j < x1.Length; j++)
                {
                    sx[idx] = x0[i];
                    sy[idx] = x1[j];

                    svm_node n1 = new svm_node();
                    n1.index = 1;
                    n1.value = x0[i];

                    svm_node n2 = new svm_node();
                    n2.index = 2;
                    n2.value = x1[j];

                    double z = svc.Predict(new [] { n1, n2 });
                    sz[idx] = z;
                    idx++;
                }
            }

            GnuPlot.Set("cntrparam levels discrete 0.5");
            GnuPlot.Contour(sx, sy, sz, "title \"Decision Boundary\"");
        }
Exemplo n.º 3
0
        public void MakeContour(double[] xBound, double[] yBound)
        {
            int id = x.Count() - 1;

            double[] x1plot = new double[id + 1];
            double[] x2plot = new double[id + 1];
            for (int i = 0; i <= id; i++)
            {
                x1plot[i] = x[i][0];
                x2plot[i] = x[i][1];
            }
            string xr = "xr[" + xBound[0] + ":" + xBound[1] + "]";
            string yr = "yr[" + yBound[0] + ":" + yBound[1] + "]";

            GnuPlot.Unset("key");                                                      //hide the key or legend
            GnuPlot.HoldOn();

            GnuPlot.Set(xr, yr, "cntrparam levels 20", "isosamples 50", xr, yr, "decimalsign locale"); //notice cntrparam levels (# height levels)
            GnuPlot.Plot(x1plot, x2plot, "with linespoints linestyle 1");
            GnuPlot.Contour(filename, "lc rgb 'blue'");
        }
Exemplo n.º 4
0
        private static void PlotDecisionBoundary(Vector <double> theta)
        {
            // Grid over which we will calculate J
            double[] x1 = MathNet.Numerics.Generate.LinearSpaced(50, -1, 1.5);
            double[] x2 = MathNet.Numerics.Generate.LinearSpaced(50, -1, 1.5);

            // initialize J_vals to a matrix of 0's
            int size = x1.Length * x2.Length;

            double[] sx = new double[size];
            double[] sy = new double[size];
            double[] sz = new double[size];

            int idx = 0;

            for (int i = 0; i < x1.Length; i++)
            {
                for (int j = 0; j < x2.Length; j++)
                {
                    sx[idx] = x1[i];
                    sy[idx] = x2[j];

                    Vector <double> v1 = Vector <double> .Build.DenseOfArray(new [] { x1[i] });

                    Vector <double> v2 = Vector <double> .Build.DenseOfArray(new [] { x2[j] });

                    Matrix <double> X = MapFeature(v1, v2);
                    Vector <double> z = X * theta;

                    sz[idx] = z[0];
                    idx++;
                }
            }
            GnuPlot.Set("cntrparam levels discrete 0");
            GnuPlot.Contour(sx, sy, sz, "title \"Decision Boundary\"");
        }