Esempio n. 1
0
        // Called when the user tries to open a file to test the classifier.
        private void testingFile_button_Click(object sender, EventArgs e)
        {
            // Scroll to the end of the text box.
            classificationLog_richTextBox.SelectionStart = classificationLog_richTextBox.Text.Length;
            classificationLog_richTextBox.ScrollToCaret();

            if (testing_openFileDialog.ShowDialog() == DialogResult.OK)
            {
                testingData = new ClassificationData();

                // Return if file was not open and parsed correctly.
                if (!testingData.OpenAndParseFile(testing_openFileDialog.FileName, true))
                {
                    classificationLog_richTextBox.SelectionColor = Color.Red;
                    classificationLog_richTextBox.SelectedText   =
                        "=====> Testing file: " +
                        testing_openFileDialog.FileName +
                        " was not opened or parsed correctly." +
                        Environment.NewLine;
                    return;
                }
                testingFileLoaded = true;
                classificationLog_richTextBox.SelectionColor = Color.Green;
                classificationLog_richTextBox.SelectedText   =
                    "=====> Testing file: " +
                    testing_openFileDialog.FileName +
                    " was opened successfully." +
                    Environment.NewLine;

                reset_button.Enabled          = true;
                testClassifier_button.Enabled = trainingFileLoaded && testingFileLoaded && dataTrained;
            }
        }
        /// <summary>
        /// Test the classifier with some data.
        /// </summary>
        /// <param name="testingData">Data used to test the classifier.</param>
        /// <returns>Array of predicted values.</returns>
        public override int[] TestClassifier(ClassificationData testingData)
        {
            List <int> results = new List <int>();

            // Predict the results for a series of inputs.
            foreach (double[] input in testingData.InputData)
            {
                results.Add(ClassificationDecisionTree.Compute(input));
            }

            return(results.ToArray());
        }
Esempio n. 3
0
 // Reset all controls.
 private void reset_button_Click(object sender, EventArgs e)
 {
     if (treeVisualizer != null)
     {
         treeVisualizer.Close();
     }
     if (matrixVisualizer != null)
     {
         matrixVisualizer.Close();
     }
     trainingFileLoaded                     = false;
     testingFileLoaded                      = false;
     dataTrained                            = false;
     attributeChosen                        = false;
     classifierChosen                       = false;
     classifierError                        = 0;
     predictionError                        = 0;
     classifier                             = null;
     trainingData                           = null;
     testingData                            = null;
     parameters_groupBox.Enabled            = false;
     treeParameters_panel.Visible           = false;
     bayesianParameters_panel.Visible       = false;
     svmParameters_panel.Visible            = false;
     treeJoin_numericUpDown.Value           = 0;
     treeJoin_numericUpDown.Enabled         = true;
     treeHeight_numericUpDown.Value         = 0;
     treeHeight_numericUpDown.Enabled       = true;
     svmKernel_comboBox.SelectedIndex       = 0;
     svmKernel_comboBox.Enabled             = true;
     svmAlgorithm_comboBox.SelectedIndex    = 0;
     svmAlgorithm_comboBox.Enabled          = true;
     decisionTree_checkBox.Enabled          = true;
     attributeToPredict_comboBox.DataSource = null;
     attributeToPredict_comboBox.Enabled    = false;
     classifierToUse_comboBox.DataSource    = null;
     classifierToUse_comboBox.Enabled       = false;
     trainingFile_button.Enabled            = true;
     trainClassifier_button.Enabled         = false;
     testClassifier_button.Enabled          = false;
     dataset_dataGridView.DataSource        = null;
     dataset_dataGridView.Enabled           = false;
     trainingTimeValue_label.ForeColor      = SystemColors.ControlText;
     testingTimeValue_label.ForeColor       = SystemColors.ControlText;
     classifierErrorValue_label.ForeColor   = SystemColors.ControlText;
     predictionErrorValue_label.ForeColor   = SystemColors.ControlText;
     trainingTimeValue_label.Text           = "-";
     testingTimeValue_label.Text            = "-";
     classifierErrorValue_label.Text        = "-";
     predictionErrorValue_label.Text        = "-";
     classificationLog_richTextBox.Clear();
     classificationLog_richTextBox.Enabled = false;
 }
Esempio n. 4
0
        /// <summary>
        /// Test the classifier with some data.
        /// </summary>
        /// <param name="testingData">Data used to test the classifier.</param>
        /// <returns>Array of predicted values.</returns>
        public override int[] TestClassifier(ClassificationData testingData)
        {
            List <int> results = new List <int>();

            // Predict the results for a series of inputs.
            foreach (double[] input in testingData.InputData)
            {
                results.Add(SVMachine.Compute(input, MulticlassComputeMethod.Voting));
            }

            return(results.ToArray());
        }
        /// <summary>
        /// Train the classifier with some data, using parameters.
        /// </summary>
        /// <param name="trainingData">Data used to train the classifier.</param>
        /// <param name="maxJoin">How many times a variable can join
        /// the decision process.</param>
        /// <param name="maxHeight">Maximum height when learning the tree.</param>
        /// <returns>Classifier prediction error.</returns>
        public double TrainClassifierWithParameters(
            ClassificationData trainingData,
            int maxJoin   = 0,
            int maxHeight = 0)
        {
            double classifierError = 0;
            List <DecisionVariable> decisionVariables = new List <DecisionVariable>();

            if (DecisionVariableNames != null)
            {
                for (int n = 0; n < trainingData.InputAttributeNumber; ++n)
                {
                    decisionVariables.Add(
                        new DecisionVariable(DecisionVariableNames[n], DecisionVariableKind.Continuous)
                        );
                }
            }
            // Generate automatic names for the variables if no names are provided.
            else
            {
                for (int n = 0; n < trainingData.InputAttributeNumber; ++n)
                {
                    decisionVariables.Add(
                        new DecisionVariable("variable_" + (n + 1).ToString(),
                                             DecisionVariableKind.Continuous));
                }
            }

            // Create a new Decision Tree classifier.
            ClassificationDecisionTree = new DecisionTree(decisionVariables, trainingData.OutputPossibleValues);

            // Create a new instance of the C45 algorithm to be learned by the tree.
            C45LearningTree = new C45Learning(ClassificationDecisionTree);

            // Change some classifier's parameters if valid new
            // values are provided.
            if (maxJoin > 0)
            {
                C45LearningTree.Join = maxJoin;
            }
            if (maxHeight > 0)
            {
                C45LearningTree.MaxHeight = maxHeight;
            }

            // Use data to train the tree.
            classifierError = C45LearningTree.Run(trainingData.InputData, trainingData.OutputData);

            return(classifierError);
        }
Esempio n. 6
0
        /// <summary>
        /// Default constructor.
        /// </summary>
        /// <param name="testingData">Source data containing the actual values.</param>
        /// <param name="predictedValues">Predicted results for the source data.</param>
        public ConfusionMatrixView(ClassificationData testingData, int[] predictedValues)
        {
            InitializeComponent();

            // Create a confusion matrix using source and predicted data.
            GeneralConfusionMatrix confusionMatrix =
                new GeneralConfusionMatrix(testingData.OutputPossibleValues, predictedValues, testingData.OutputData);

            // The confusion matrix will be shown as a DataTable,
            // in which columns represent the actual values while
            // rows represent the predicted values. The first column
            // holds the names of the values.

            // Create the structure of the table.
            for (int n = 0; n <= testingData.OutputPossibleValues; ++n)
            {
                if (n == 0)
                {
                    confusionMatrixTable.Columns.Add("Confusion matrix", typeof(string));
                }
                else
                {
                    confusionMatrixTable.Columns.Add(
                        testingData.CodeBook.Translate(testingData.OutputColumnName, n - 1),
                        typeof(int));
                }
            }

            // Populate the table rows with data.
            for (int i = 0; i < testingData.OutputPossibleValues; ++i)
            {
                DataRow newRow = confusionMatrixTable.NewRow();
                for (int j = 0; j <= testingData.OutputPossibleValues; ++j)
                {
                    if (j == 0)
                    {
                        newRow[j] = testingData.CodeBook.Translate(testingData.OutputColumnName, i);
                    }
                    else
                    {
                        newRow[j] = confusionMatrix.Matrix[i, j - 1];
                    }
                }
                confusionMatrixTable.Rows.Add(newRow);
            }

            confusionMatrix_dataGridView.DataSource = confusionMatrixTable;
        }
Esempio n. 7
0
        // Called when the user tries to open a file to train the classifier.
        private void trainingFile_button_Click(object sender, EventArgs e)
        {
            // Scroll to the end of the text box.
            classificationLog_richTextBox.SelectionStart = classificationLog_richTextBox.Text.Length;
            classificationLog_richTextBox.ScrollToCaret();

            if (training_openFileDialog.ShowDialog() == DialogResult.OK)
            {
                trainingData = new ClassificationData();

                // Return if the file was not open and parsed correctly.
                if (!trainingData.OpenAndParseFile(training_openFileDialog.FileName, true))
                {
                    classificationLog_richTextBox.SelectionColor = Color.Red;
                    classificationLog_richTextBox.SelectedText   =
                        "=====> Training file: " +
                        training_openFileDialog.FileName +
                        " was not opened or parsed correctly." +
                        Environment.NewLine;
                    return;
                }
                trainingFileLoaded = true;
                classificationLog_richTextBox.SelectionColor = Color.Green;
                classificationLog_richTextBox.SelectedText   =
                    "=====> Training file: " +
                    training_openFileDialog.FileName +
                    " was opened successfully. " +
                    Environment.NewLine;

                // Activate some window's controls if the file was loaded successfully.
                attributeToPredict_comboBox.DataSource    = trainingData.AllColumnNames;
                attributeToPredict_comboBox.SelectedIndex = -1;
                attributeToPredict_comboBox.Enabled       = true;
                classifierToUse_comboBox.DataSource       = classificationMethods;
                classifierToUse_comboBox.SelectedIndex    = -1;
                classifierToUse_comboBox.Enabled          = true;
                dataset_dataGridView.DataSource           = trainingData.ExtractedDataset;
                dataset_dataGridView.Enabled          = true;
                classificationLog_richTextBox.Enabled = true;
                trainClassifier_button.Enabled        = attributeChosen && classifierChosen;
                reset_button.Enabled          = true;
                testClassifier_button.Enabled = trainingFileLoaded && testingFileLoaded && dataTrained;
            }
        }
Esempio n. 8
0
        public double TrainClassifierWithParameters(
            ClassificationData trainingData,
            IKernel kernel = null,
            SupportVectorMachineLearningConfigurationFunction algorithm = null)
        {
            double classifierError = 0;

            // Use default parameters if no valid new
            // values are provided.
            if (kernel == null)
            {
                kernel = new Linear();
            }
            if (algorithm == null)
            {
                algorithm = (SVM, inputData, outputData, i, j) =>
                            new SequentialMinimalOptimization(SVM, inputData, outputData)
                {
                    // Avoid OutOfMemory exception.
                    CacheSize = 1000
                }
            }
            ;

            // Create a new SVM classifier.
            SVMachine = new MulticlassSupportVectorMachine(
                trainingData.InputAttributeNumber,
                kernel,
                trainingData.OutputPossibleValues);

            // Create an algorithm to be learned by the SVM.
            SVMachineLearning = new MulticlassSupportVectorLearning(
                SVMachine,
                trainingData.InputData,
                trainingData.OutputData);
            SVMachineLearning.Algorithm = algorithm;

            // Run the learning algorithm.
            classifierError = SVMachineLearning.Run(true);

            return(classifierError);
        }
        /// <summary>
        /// Train the classifier with some data.
        /// </summary>
        /// <param name="trainingData">Data used to train the classifier.</param>
        /// <returns>Classifier prediction error.</returns>
        public override double TrainClassifier(ClassificationData trainingData)
        {
            double classifierError = 0;

            // Create a new Naive Bayes classifier.
            BayesianModel = new NaiveBayes <NormalDistribution>(
                trainingData.OutputPossibleValues,
                trainingData.InputAttributeNumber,
                NormalDistribution.Standard);

            // Compute the Naive Bayes model.
            classifierError = BayesianModel.Estimate(
                trainingData.InputData,
                trainingData.OutputData,
                true,
                new NormalOptions {
                Regularization = 1e-5                     /* To avoid zero variances. */
            });

            return(classifierError);
        }
 /// <summary>
 /// Train the classifier with some data and default parameters.
 /// </summary>
 /// <param name="trainingData">Data used to train the classifier.</param>
 /// <returns>Classifier prediction error.</returns>
 public override double TrainClassifier(ClassificationData trainingData)
 {
     // Train the classifier using default parameters.
     return(TrainClassifierWithParameters(trainingData));
 }