Exemple #1
0
        // Called when the user tries to train the classifier with the data previously loaded.
        private void trainClassifier_button_Click(object sender, EventArgs e)
        {
            // Scroll to the end of the text box.
            classificationLog_richTextBox.SelectionStart = classificationLog_richTextBox.Text.Length;
            classificationLog_richTextBox.ScrollToCaret();

            // Return if there was an error when processing the training dataset.
            if (!trainingData.ProcessDataset(attributeToPredict_comboBox.SelectedItem.ToString()))
            {
                classificationLog_richTextBox.SelectionColor = Color.Red;
                classificationLog_richTextBox.SelectedText   =
                    "=====> There was a problem with processing the training data." +
                    Environment.NewLine;
                return;
            }
            classificationLog_richTextBox.SelectionColor = Color.Green;
            classificationLog_richTextBox.SelectedText   =
                "=====> Training data was processed correctly." +
                Environment.NewLine;

            // Choose classifier to use.
            switch (classifierToUse_comboBox.SelectedItem.ToString())
            {
            case "Decision Tree":
                classifier = new DecisionTreeClassifier();
                // Give the inputs' names to the classifier,
                // otherwise they will be generated automatically.
                ((DecisionTreeClassifier)classifier).DecisionVariableNames =
                    trainingData.InputColumnNames;
                break;

            case "Naive Bayesian":
                classifier = new NaiveBayesianClassifier();
                break;

            case "SVM":
                classifier = new SVMClassifier();
                break;

            default:
                // Return if no valid classifier is selected.
                return;
            }

            // Measure and display training time and error.
            chronometer.Reset();
            chronometer.Start();
            try
            {
                if (classifier is DecisionTreeClassifier)
                {
                    classifierError = ((DecisionTreeClassifier)classifier).TrainClassifierWithParameters(
                        trainingData,
                        (int)treeJoin_numericUpDown.Value,
                        (int)treeHeight_numericUpDown.Value);
                }
                else if (classifier is NaiveBayesianClassifier)
                {
                    classifierError = ((NaiveBayesianClassifier)classifier).TrainClassifier(
                        trainingData);
                }
                else if (classifier is SVMClassifier)
                {
                    classifierError = ((SVMClassifier)classifier).TrainClassifierWithParameters(
                        trainingData,
                        kernelList[svmKernel_comboBox.SelectedValue.ToString()],
                        /*svmAlgorithm_comboBox*/ null);
                }
            }
            catch
            {
                classificationLog_richTextBox.SelectionColor = Color.Red;
                classificationLog_richTextBox.SelectedText   =
                    "=====> There was a problem with training the classifier." +
                    Environment.NewLine;
                return;
            }
            chronometer.Stop();
            classificationLog_richTextBox.SelectionColor = Color.Green;
            classificationLog_richTextBox.SelectedText   =
                "=====> Classifier was trained successfully." +
                Environment.NewLine;
            trainingTimeValue_label.ForeColor    = Color.Blue;
            trainingTimeValue_label.Text         = chronometer.ElapsedMilliseconds + " ms";
            classifierErrorValue_label.ForeColor = (classifierError == 0) ? Color.Green : Color.Red;
            classifierErrorValue_label.Text      =
                string.Format("{0:0.00}", classifierError * 100) + "%";
            dataTrained = true;

            // Deactivate some window's controls if
            // classifier's training was successful.
            attributeToPredict_comboBox.Enabled = false;
            classifierToUse_comboBox.Enabled    = false;
            trainingFile_button.Enabled         = false;
            trainClassifier_button.Enabled      = false;
            treeJoin_numericUpDown.Enabled      = false;
            treeHeight_numericUpDown.Enabled    = false;
            svmKernel_comboBox.Enabled          = false;
            svmAlgorithm_comboBox.Enabled       = false;
            decisionTree_checkBox.Enabled       = false;
            testClassifier_button.Enabled       = trainingFileLoaded && testingFileLoaded && dataTrained;
            this.Refresh();

            // Show a visual tree if decision tree classifier
            // was chosen and the user checked the check box.
            if ((classifier is DecisionTreeClassifier) && decisionTree_checkBox.Checked)
            {
                try
                {
                    VisualDecisionTree visualTree = new VisualDecisionTree(
                        ((DecisionTreeClassifier)classifier).ClassificationDecisionTree);
                    Bitmap image = visualTree.drawHorizontalTree(
                        trainingData.OutputColumnName,
                        trainingData.CodeBook);
                    treeVisualizer = new TreeView(image);
                }
                catch
                {
                    classificationLog_richTextBox.SelectionColor = Color.Red;
                    classificationLog_richTextBox.SelectedText   =
                        "=====> There was a problem with drawing the decision tree." +
                        Environment.NewLine;
                    return;
                }
                treeVisualizer.Show();
            }
        }
Exemple #2
0
        // Called when the user tries to test the classifier with some testing data.
        private void testClassifier_button_Click(object sender, EventArgs e)
        {
            // Scroll to the end of the text box.
            classificationLog_richTextBox.SelectionStart = classificationLog_richTextBox.Text.Length;
            classificationLog_richTextBox.ScrollToCaret();

            // Return if there was an error when processing the testing dataset.
            if (!testingData.ProcessDataset(trainingData.OutputColumnName, trainingData.CodeBook))
            {
                classificationLog_richTextBox.SelectionColor = Color.Red;
                classificationLog_richTextBox.SelectedText   =
                    "=====> There was a problem with processing the testing data." +
                    Environment.NewLine;
                return;
            }
            classificationLog_richTextBox.SelectionColor = Color.Green;
            classificationLog_richTextBox.SelectedText   =
                "=====> Testing data was processed correctly. Testing results:" +
                Environment.NewLine;

            // Reset prediction error.
            predictionError = 0;

            // Measure testing time.
            chronometer.Reset();
            chronometer.Start();
            int[] predictedValues = classifier.TestClassifier(testingData);
            chronometer.Stop();

            // Log results and calculate prediction error.
            for (int n = 0; n < predictedValues.Length; ++n)
            {
                // Verify if predicted results are equal to the actual results.
                if (predictedValues[n] == testingData.OutputData[n])
                {
                    classificationLog_richTextBox.SelectionColor = Color.Green;
                    classificationLog_richTextBox.SelectedText   =
                        "MATCHING: expected " +
                        testingData.CodeBook.Translate(
                            testingData.OutputColumnName,
                            testingData.OutputData[n]) +
                        " and predicted " +
                        testingData.CodeBook.Translate(
                            testingData.OutputColumnName,
                            predictedValues[n]) +
                        Environment.NewLine;
                }
                else
                {
                    ++predictionError;
                    classificationLog_richTextBox.SelectionColor = Color.Red;
                    classificationLog_richTextBox.SelectedText   =
                        "NOT MATCHNG: expected " +
                        testingData.CodeBook.Translate(
                            testingData.OutputColumnName,
                            testingData.OutputData[n]) +
                        " but predicted " +
                        testingData.CodeBook.Translate(
                            testingData.OutputColumnName,
                            predictedValues[n]) +
                        Environment.NewLine;
                }
            }

            // Scroll to the end of the text box.
            classificationLog_richTextBox.SelectionStart = classificationLog_richTextBox.Text.Length;
            classificationLog_richTextBox.ScrollToCaret();

            // Close the old window showing the confusion matrix
            // (if present) and show a new window if the user
            // checked the check box.
            if (matrixVisualizer != null)
            {
                matrixVisualizer.Close();
            }
            if (confusionMatrix_checkBox.Checked)
            {
                matrixVisualizer = new ConfusionMatrixView(testingData, predictedValues);
                matrixVisualizer.Show();
            }

            // Display testing time and prediction error.
            testingTimeValue_label.ForeColor     = Color.Blue;
            testingTimeValue_label.Text          = chronometer.ElapsedMilliseconds + " ms";
            predictionErrorValue_label.ForeColor = (predictionError == 0) ? Color.Green : Color.Red;
            predictionErrorValue_label.Text      =
                string.Format("{0:0.00}", predictionError * 100 / predictedValues.Length) + "%";
        }