コード例 #1
0
        private void ButtonClassify_Click(object sender, EventArgs e)
        {
            var start = DateTime.Now;

            if (CBRandomOrder.Checked)
            {
                SetDataRandomOrder();
            }
            classifier.SetClass(SelectClass.SelectedItem.ToString());
            classifier.LoadData(data);
            classifier.TrainModel();
            TextOutput.ResetText();
            foreach (var row in TextTestData.Text.Split('\n'))
            {
                TextOutput.AppendText($"{row}\t=> {classifier.TrainedModel.GetEstimatedClass(row)}\n");
            }
            TextOutput.AppendText($"\nExecute time:\t{new TimeSpan(DateTime.Now.Ticks - start.Ticks).TotalSeconds.ToString()}s\n");
        }
コード例 #2
0
        private void ButtonGenerateModel_Click(object sender, EventArgs e)
        {
            var start = DateTime.Now;

            if (CBRandomOrder.Checked)
            {
                SetDataRandomOrder();
            }
            classifier.SetClass(SelectClass.SelectedItem.ToString());
            int[][] confusionMatrix = null;
            int     dataCount       = data.Count();

            switch (SelectTestOption.SelectedIndex)
            {
            //Use Test Set
            case 0:
                classifier.LoadData(data);
                classifier.TrainModel();
                confusionMatrix = classifier.GetConfusionMatrix(TextTestData.Text);
                break;

            //Cross-Validation
            case 1:
                int fold      = int.Parse(TextFold.Text);
                int foldCount = dataCount / fold;
                for (int j = 0; j < fold; j++)
                {
                    int c = foldCount * j;
                    classifier.LoadData(data.Take(c).Concat(data.Skip(c + foldCount)));
                    classifier.TrainModel();
                    confusionMatrix = AddMatrix(confusionMatrix,
                                                classifier.GetConfusionMatrix(data.Skip(c).Take(foldCount)));
                }
                break;

            //Percentage Split
            case 2:
                dataCount = dataCount * int.Parse(TextPercentage.Text) / 100;
                classifier.LoadData(data.Take(dataCount));
                classifier.TrainModel();
                confusionMatrix = classifier.GetConfusionMatrix(data.Skip(dataCount));
                break;

            default:
                confusionMatrix = new int[0][];
                break;
            }
            TextOutput.ResetText();
            if (CBConfusionMatrix.Checked)
            {
                TextOutput.AppendText("Confusion Matrix:\n");
                TextOutput.AppendText($"\t{string.Join("\t", classifier.ClassInfo.NominalOption)}\n");
                var classOption = classifier.ClassInfo.NominalOption.GetEnumerator();
                for (int j = 0; j < classifier.ClassInfo.NominalOptionCount; j++)
                {
                    classOption.MoveNext();
                    TextOutput.AppendText($"{classOption.Current}\t{string.Join("\t", confusionMatrix[j])}\n");
                }
                TextOutput.AppendText("\n");
            }
            var     itor      = Enumerable.Range(0, confusionMatrix.Length);
            decimal testCount = confusionMatrix.SelectMany(x => x).Sum();

            TextOutput.AppendText($"Accuracy:\t{(itor.Select(x => confusionMatrix[x][x]).Sum() / testCount).ToString()}\n\n");
            int     i = 0;
            decimal d;

            foreach (var o in classifier.ClassInfo.NominalOption)
            {
                TextOutput.AppendText($"{o}:\n");
                TextOutput.AppendText("Precision:\t");
                d = itor.Select(x => confusionMatrix[x][i]).Sum();
                TextOutput.AppendText(d == 0m ? "∞" : (confusionMatrix[i][i] / d).ToString());
                TextOutput.AppendText("\n");
                d = itor.Select(x => confusionMatrix[i][x]).Sum();
                TextOutput.AppendText("Recall:\t");
                TextOutput.AppendText(d == 0m ? "∞" : (confusionMatrix[i][i] / d).ToString());
                TextOutput.AppendText("\n");
                d = itor.Select(x => confusionMatrix[i][x]).Concat(
                    itor.Select(x => confusionMatrix[x][i])).Sum();
                TextOutput.AppendText("F1-measure:\t");
                TextOutput.AppendText(d == 0m ? "∞" : (confusionMatrix[i][i] * 2 / d).ToString());
                TextOutput.AppendText("\n\n");
                i++;
            }
            TextOutput.AppendText($"Execute time:\t{new TimeSpan(DateTime.Now.Ticks - start.Ticks).TotalSeconds.ToString()}s\n");
        }