Beispiel #1
0
        /// <summary>
        ///   Creates a Support Vector Machine and teaches it to recognize
        ///   the previously loaded dataset using the current UI settings.
        /// </summary>
        ///
        private void btnCreate_Click(object sender, EventArgs e)
        {
            if (dgvLearningSource.DataSource == null)
            {
                MessageBox.Show("Please load some data first.");
                return;
            }

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



            // Creates a matrix from the entire source data table
            double[,] table = (dgvLearningSource.DataSource as DataTable).ToMatrix(out columnNames);

            // Get only the input vector values (first two columns)
            double[][] inputs = table.GetColumns(0, 1).ToArray();

            // Get only the output labels (last column)
            int[] outputs = table.GetColumn(2).ToInt32();

            // Create a sparse logistic learning algorithm
            var pcd = new ProbabilisticCoordinateDescent()
            {
                // Set learning parameters
                Complexity     = (double)numC.Value,
                Tolerance      = (double)numT.Value,
                PositiveWeight = (double)numPositiveWeight.Value,
                NegativeWeight = (double)numNegativeWeight.Value,
            };

            try
            {
                // Run
                svm = pcd.Learn(inputs, outputs);

                lbStatus.Text = "Training complete!";
            }
            catch (ConvergenceException)
            {
                lbStatus.Text = "Convergence could not be attained. " +
                                "The learned machine might still be usable.";
            }

            svm.Compress(); // reduce support vectors to a single weight vector
            Trace.Assert(svm.SupportVectors.Length == 1);
            Trace.Assert(svm.Weights.Length == 1);

            createSurface(table);

            // Show feature weight importance
            double[] weights = svm.SupportVectors[0].Abs();

            string[] featureNames = columnNames.RemoveAt(columnNames.Length - 1);
            dgvSupportVectors.DataSource = new ArrayDataView(weights, featureNames);

            CreateBarGraph(weights, featureNames);
        }