// 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; }
// 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(); } }