示例#1
0
 public TrainingResultRecord(int _id, ClassifierEnum _classifier, string _bestParamters, float _bestScore, float _accuracy, float _precision, float _recall, float _f1_score)
 {
     Id            = _id;
     Classifier    = _classifier;
     BestParamters = _bestParamters;
     BestScore     = _bestScore;
     Accuracy      = _accuracy;
     Precision     = _precision;
     Recall        = _recall;
     F1_Score      = _f1_score;
 }
示例#2
0
        public void BindResultToView(string output)
        {
            var results = new Dictionary <ClassifierEnum, TrainingResultRecord>();
            var lines   = output.Split('\n');

            var bestParamsSignal  = "best parameters: ";
            var bestScoreSignal   = "best score: ";
            var accuracySignal    = "accuracy";
            var weightedAvgSignal = "weighted avg";

            ClassifierEnum currentClassifier = ClassifierEnum.COUNT;

            for (int iLine = 0; iLine < lines.Length; iLine++)
            {
                var line = lines[iLine];
                if (line.Contains("full training") || line.Contains("test model"))
                {
                    for (int iClassifier = 0; iClassifier < (int)ClassifierEnum.COUNT; iClassifier++)
                    {
                        if (line.Contains(((ClassifierEnum)iClassifier).ToString()))
                        {
                            currentClassifier = (ClassifierEnum)iClassifier;
                            if (!results.ContainsKey(currentClassifier))
                            {
                                var nextId = results.Count;
                                results[currentClassifier]            = new TrainingResultRecord();
                                results[currentClassifier].Id         = nextId;
                                results[currentClassifier].Classifier = (ClassifierEnum)iClassifier;
                            }

                            break;
                        }
                    }
                }
                else if (line.Contains(bestParamsSignal))
                {
                    results[currentClassifier].BestParamters = line.Substring(line.IndexOf(bestParamsSignal) + bestParamsSignal.Length).Trim();
                }
                else if (line.Contains(bestScoreSignal))
                {
                    float.TryParse(line.Substring(line.IndexOf(bestScoreSignal) + bestScoreSignal.Length), out results[currentClassifier].BestScore);
                }
                else if (line.Contains(accuracySignal))
                {
                    var parts = line.Substring(line.IndexOf(accuracySignal) + accuracySignal.Length).Split(' ')
                                .Select(x => x.Trim())
                                .Where(x => !string.IsNullOrWhiteSpace(x))
                                .ToArray();
                    if (parts.Length >= 1)
                    {
                        float.TryParse(parts[0], out results[currentClassifier].Accuracy);
                    }
                }
                else if (line.Contains(weightedAvgSignal))
                {
                    var parts = line.Substring(line.IndexOf(weightedAvgSignal) + weightedAvgSignal.Length).Split(' ')
                                .Select(x => x.Trim())
                                .Where(x => !string.IsNullOrWhiteSpace(x))
                                .ToArray();

                    if (parts.Length >= 3)
                    {
                        float.TryParse(parts[0], out results[currentClassifier].Precision);
                        float.TryParse(parts[1], out results[currentClassifier].Recall);
                        float.TryParse(parts[2], out results[currentClassifier].F1_Score);
                    }
                }
            }

            var resultDataGridView = _userControl.resultDataGridView;

            resultDataGridView.Invoke((MethodInvoker) delegate
            {
                var table = new DataTable();
                table.Columns.Add("Id", typeof(int));
                table.Columns.Add("Classifier", typeof(ClassifierEnum));
                table.Columns.Add("BestParamters", typeof(string));
                table.Columns.Add("BestScore", typeof(float));
                table.Columns.Add("Accuracy", typeof(float));
                table.Columns.Add("Precision", typeof(float));
                table.Columns.Add("Recall", typeof(float));
                table.Columns.Add("F1_Score", typeof(float));

                foreach (var result in results)
                {
                    var dataRow              = table.NewRow();
                    dataRow["Id"]            = result.Value.Id;
                    dataRow["Classifier"]    = result.Value.Classifier;
                    dataRow["BestParamters"] = result.Value.BestParamters;
                    dataRow["BestScore"]     = result.Value.BestScore;
                    dataRow["Accuracy"]      = result.Value.Accuracy;
                    dataRow["Precision"]     = result.Value.Precision;
                    dataRow["Recall"]        = result.Value.Recall;
                    dataRow["F1_Score"]      = result.Value.F1_Score;

                    table.Rows.Add(dataRow);
                }

                resultDataGridView.DataSource = new BindingSource(table, null);
            });
        }