Пример #1
0
        public VisualizationForm(ReceiverOperatingCharacteristic roc, String windowTitle)
        {
            InitializeComponent();
            ScatterPlotForm sp = new ScatterPlotForm(roc.GetScatterplot(true));

            sp.Show();
        }
Пример #2
0
        static void Main(string[] args)
        {
            double[][] inputs =
            {
                // Those are from class -1
                new double[] { 2, 4, 0 },
                new double[] { 5, 5, 1 },
                new double[] { 4, 5, 0 },
                new double[] { 2, 5, 5 },
                new double[] { 4, 5, 1 },
                new double[] { 4, 5, 0 },
                new double[] { 6, 2, 0 },
                new double[] { 4, 1, 0 },

                // Those are from class +1
                new double[] { 1, 4, 5 },
                new double[] { 7, 5, 1 },
                new double[] { 2, 6, 0 },
                new double[] { 7, 4, 7 },
                new double[] { 4, 5, 0 },
                new double[] { 6, 2, 9 },
                new double[] { 4, 1, 6 },
                new double[] { 7, 2, 9 },
            };

            int[] outputs =
            {
                -1, -1, -1, -1, -1, -1, -1, -1, // fist eight from class -1
                +1, +1, +1, +1, +1, +1, +1, +1  // last eight from class +1
            };

            // Next, we create a linear Support Vector Machine with 4 inputs
            SupportVectorMachine machine = new SupportVectorMachine(inputs: 3);

            // Create the sequential minimal optimization learning algorithm
            var smo = new SequentialMinimalOptimization(machine, inputs, outputs);

            // We learn the machine
            double error = smo.Run();

            // And then extract its predicted labels
            double[] predicted = new double[inputs.Length];
            for (int i = 0; i < predicted.Length; i++)
            {
                predicted[i] = machine.Compute(inputs[i]);
            }

            // At this point, the output vector contains the labels which
            // should have been assigned by the machine, and the predicted
            // vector contains the labels which have been actually assigned.

            // Create a new ROC curve to assess the performance of the model
            var roc = new ReceiverOperatingCharacteristic(outputs, predicted);

            roc.Compute(100); // Compute a ROC curve with 100 cut-off points
            roc.GetScatterplot(true);
            Console.WriteLine(roc.Area.ToString());
            Console.Write(roc.StandardError.ToString());
        }
Пример #3
0
        private void visualize()
        {
            switch (_visualizationType)
            {
            case VisualizationType.COMPONENTS_CUMULATIVE:
            {
                if (_visualizationSource != null)
                {
                    if (_visualizationSource.GetType() == typeof(PrincipalComponentAnalysis))
                    {
                        PrincipalComponentAnalysis pca = (PrincipalComponentAnalysis)_visualizationSource;
                        VisualizationForm          f   = new VisualizationForm(pca.Components, true, "Cumulative Component Distribution");
                        f.Show();
                    }
                }

                break;
            }

            case VisualizationType.COMPONENTS_DISTRIBUTION:
            {
                if (_visualizationSource != null)
                {
                    if (_visualizationSource.GetType() == typeof(PrincipalComponentAnalysis))
                    {
                        PrincipalComponentAnalysis pca = (PrincipalComponentAnalysis)_visualizationSource;
                        VisualizationForm          f   = new VisualizationForm(pca.Components, false, "Component Distribution");
                        f.Show();
                    }
                }
                break;
            }

            case VisualizationType.ROC_PLOT_POINTS:
            {
                if (_visualizationSource != null)
                {
                    if (_visualizationSource.GetType() == typeof(ReceiverOperatingCharacteristic))
                    {
                        ReceiverOperatingCharacteristic roc = (ReceiverOperatingCharacteristic)_visualizationSource;
                        ScatterPlotForm sp = new ScatterPlotForm(roc.GetScatterplot(true));
                        sp.Show();
                    }
                }
                break;
            }

            default:
            {
                break;
            }
            }
        }
Пример #4
0
        private void btnRunAnalysis_Click(object sender, EventArgs e)
        {
            if (sourceTable == null)
            {
                MessageBox.Show("Please load some data before attempting to plot a curve.");
                return;
            }


            // Finishes and save any pending changes to the given data
            dgvSource.EndEdit();

            // Creates a matrix from the source data table
            int n = sourceTable.Rows.Count;

            double[] realData = new double[n];
            double[] testData = new double[n];
            for (int i = 0; i < n; i++)
            {
                realData[i] = (double)sourceTable.Rows[i][0];
                testData[i] = (double)sourceTable.Rows[i][1];
            }

            // Creates the Receiver Operating Curve of the given source
            rocCurve = new ReceiverOperatingCharacteristic(realData, testData);

            // Compute the ROC curve
            if (rbNumPoints.Checked)
            {
                rocCurve.Compute((int)numPoints.Value);
            }
            else
            {
                rocCurve.Compute((float)numIncrement.Value);
            }

            scatterplotView1.Scatterplot = rocCurve.GetScatterplot(true);

            // Show point details
            dgvPointDetails.DataSource = new SortableBindingList <ReceiverOperatingCharacteristicPoint>(rocCurve.Points);

            // Show area and error
            tbArea.Text  = rocCurve.Area.ToString();
            tbError.Text = rocCurve.StandardError.ToString();
        }
Пример #5
0
        private void btnRunAnalysis_Click(object sender, EventArgs e)
        {
            if (sourceTable == null)
            {
                MessageBox.Show("Please load some data before attempting to plot a curve.");
                return;
            }


            // Finishes and save any pending changes to the given data
            dgvSource.EndEdit();

            // Creates a matrix from the source data table
            int n = sourceTable.Rows.Count;

            double[] realData = new double[n];
            double[] testData = new double[n];
            for (int i = 0; i < n; i++)
            {
                realData[i] = (double)sourceTable.Rows[i][0];
                testData[i] = (double)sourceTable.Rows[i][1];
            }

            // Creates the Receiver Operating Curve of the given source
            rocCurve = new ReceiverOperatingCharacteristic(realData, testData);

            // Compute the ROC curve
            if (rbNumPoints.Checked)
                rocCurve.Compute((int)numPoints.Value);
            else
                rocCurve.Compute((float)numIncrement.Value);

            scatterplotView1.Scatterplot = rocCurve.GetScatterplot(true);

            // Show point details
            dgvPointDetails.DataSource = new SortableBindingList<ReceiverOperatingCharacteristicPoint>(rocCurve.Points);

            // Show area and error
            tbArea.Text = rocCurve.Area.ToString();
            tbError.Text = rocCurve.StandardError.ToString();
        }
Пример #6
0
        /// <summary>
        /// Run the lesson.
        /// </summary>
        public static void Run()
        {
            // get data
            Console.WriteLine("Loading data....");
            var path    = Path.GetFullPath(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"..\..\..\..\california_housing.csv"));
            var housing = Frame.ReadCsv(path, separators: ",");

            housing = housing.Where(kv => ((decimal)kv.Value["median_house_value"]) < 500000);

            // create the median_high_house_value feature
            housing.AddColumn("median_high_house_value",
                              housing["median_house_value"].Select(v => v.Value >= 265000 ? 1.0 : 0.0));

            // shuffle the frame
            var rnd     = new Random();
            var indices = Enumerable.Range(0, housing.Rows.KeyCount).OrderBy(v => rnd.NextDouble());

            housing = housing.IndexRowsWith(indices).SortRowsByKey();

            // create training, validation, and test frames
            var training   = housing.Rows[Enumerable.Range(0, 12000)];
            var validation = housing.Rows[Enumerable.Range(12000, 2500)];
            var test       = housing.Rows[Enumerable.Range(14500, 2500)];

            // build the list of features we're going to use
            var columns = new string[] {
                "latitude",
                "longitude",
                "housing_median_age",
                "total_rooms",
                "total_bedrooms",
                "population",
                "households",
                "median_income"
            };

            // train the model using a logistic regressor
            var learner = new IterativeReweightedLeastSquares <LogisticRegression>()
            {
                MaxIterations = 100
            };
            var regression = learner.Learn(
                training.Columns[columns].ToArray2D <double>().ToJagged(),
                training["median_high_house_value"].Values.ToArray());

            // get probabilities
            var features_validation = validation.Columns[columns].ToArray2D <double>().ToJagged();
            var label_validation    = validation["median_high_house_value"].Values.ToArray();
            var probabilities       = regression.Probability(features_validation);

            // calculate the histogram of probabilities
            var histogram = new Histogram();

            histogram.Compute(probabilities, 0.05);

            // draw the histogram
            Plot(histogram, "Probability histogram", "prediction", "count");

            // get predictions and actuals
            var predictions = regression.Decide(features_validation);
            var actuals     = label_validation.Select(v => v == 1.0 ? true : false).ToArray();

            // create confusion matrix
            var confusion = new ConfusionMatrix(predictions, actuals);

            // display classification scores
            Console.WriteLine($"True Positives:  {confusion.TruePositives}");
            Console.WriteLine($"True Negatives:  {confusion.TrueNegatives}");
            Console.WriteLine($"False Positives: {confusion.FalsePositives}");
            Console.WriteLine($"False Negatives: {confusion.FalseNegatives}");
            Console.WriteLine();

            // display accuracy, precision, and recall
            Console.WriteLine($"Accuracy:        {confusion.Accuracy}");
            Console.WriteLine($"Precision:       {confusion.Precision}");
            Console.WriteLine($"Recall:          {confusion.Recall}");
            Console.WriteLine();

            // display TPR and FPR
            Console.WriteLine($"TPR:             {confusion.Sensitivity}");
            Console.WriteLine($"FPR:             {confusion.FalsePositiveRate}");
            Console.WriteLine();

            // calculate roc curve
            var roc = new ReceiverOperatingCharacteristic(
                actuals,
                predictions.Select(v => v ? 1 : 0).ToArray());

            roc.Compute(100);

            // generate the scatter plot
            var rocPlot = roc.GetScatterplot(true);

            // show roc curve
            Plot(rocPlot);

            // show the auc
            Console.WriteLine($"AUC:             {roc.Area}");
        }
Пример #7
0
        static void Main(string[] args)
        {
            System.Globalization.CultureInfo customCulture = (System.Globalization.CultureInfo)System.Threading.Thread.CurrentThread.CurrentCulture.Clone();
            customCulture.NumberFormat.NumberDecimalSeparator = ".";

            int nBufferWidth = Console.BufferWidth;

            Console.SetBufferSize(nBufferWidth, 1000);

            System.Threading.Thread.CurrentThread.CurrentCulture = customCulture;

            Config cfg = new Config("AnalysClassifier.config");

            log("info", "Конфиг:" + cfg.ToString());
            double[] output = File.ReadAllLines(cfg.OutputPath)
                              .Select(x => double.Parse(x)).ToArray();
            double[] target = File.ReadAllLines(cfg.TargetPath)
                              .Select(x => double.Parse(x)).ToArray();
            double[] error = File.ReadAllLines(cfg.ErrorPath)
                             .Select(x => double.Parse(x)).ToArray();

            if (cfg.Plotroc == true)
            {
                Console.WriteLine("Модуль Plotroc");
                Console.WriteLine("Расчет ROC");
                var roc = new ReceiverOperatingCharacteristic(output, target);
                roc.Compute(100); // Compute a ROC curve with 100 cut-off points
                Console.WriteLine("ROC расчитана");
                ScatterplotBox.Show(roc.GetScatterplot(includeRandom: true))
                .SetSymbolSize(0)          // do not display data points
                .SetLinesVisible(true)     // show lines connecting points
                .SetScaleTight(true);      // tighten the scale to points
            }
            if (cfg.Plothist == true)
            {
                Console.WriteLine("Модуль Plothist");
                HistoframShow(error, "ошибок");
                HistoframShow(target, "эталонных выходов");
                HistoframShow(output, "выходов модели");
            }
            if (cfg.Plotconfucion == true)
            {
                Console.WriteLine("Модуль Plotconfucion");
                Console.WriteLine("Расчет ConfusionMatrix");
                var cm = new GeneralConfusionMatrix(classes: 2,
                                                    expected: output.Select(x => x > 0.5?1:0).ToArray(),
                                                    predicted: target.Select(x => x > 0.5 ? 1 : 0).ToArray());
                Console.WriteLine("ConfusionMatrix расчитана");
                Console.WriteLine("Confusion Matrix:");
                string[][] outMat = cm.Matrix.
                                    ToJagged().
                                    Select(x => x.Select(y => IntToStringFormatted(y)).ToArray()).
                                    ToArray();

                foreach (var it in cm.ColumnTotals)
                {
                    Console.Write($"{IntToStringFormatted(it)}");
                }
                Console.WriteLine("|");
                Console.WriteLine(new string('_', 9 * cm.ColumnTotals.Length));
                int i = 0;
                foreach (var it in outMat)
                {
                    foreach (var it2 in it)
                    {
                        Console.Write(it2);
                        Console.Write(" ");
                    }
                    Console.Write($"| {cm.RowTotals[i++]}");
                    Console.WriteLine();
                }
                Console.WriteLine();

                // We can get more information about our problem as well:
                Console.WriteLine("Дополнительная информация:");
                Console.WriteLine($"Классов: {cm.NumberOfClasses}:");
                Console.WriteLine($"Примеров: {cm.NumberOfSamples}:");
                Console.WriteLine($"Точность: {cm.Accuracy}:");
                Console.WriteLine($"Ошибка: {cm.Error}:");
                Console.WriteLine($"chanceAgreement: {cm.ChanceAgreement}:");
                Console.WriteLine($"geommetricAgreement: {cm.Error}:");
                Console.WriteLine($"pearson: {cm.Pearson}:");
                Console.WriteLine($"kappa: {cm.Kappa}:");
                Console.WriteLine($"tau: {cm.Tau}:");
                Console.WriteLine($"chiSquare: {cm.ChiSquare}:");
                Console.WriteLine($"kappaStdErr: {cm.Kappa}:");
            }
        }