Free cookie consent management tool by TermsFeed Policy Generator

Changeset 16065


Ignore:
Timestamp:
08/07/18 16:32:12 (6 years ago)
Author:
fholzing
Message:

#2904: Added more Unit-Tests for Classification

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/2904_CalculateImpacts/HeuristicLab.Tests/HeuristicLab.Problems.DataAnalysis-3.4/ClassificationVariableImpactCalculationTest.cs

    r16061 r16065  
    33using System.Linq;
    44using HeuristicLab.Algorithms.DataAnalysis;
     5using HeuristicLab.Common;
    56using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
    67using HeuristicLab.Problems.DataAnalysis.Symbolic;
     8using HeuristicLab.Problems.DataAnalysis.Symbolic.Classification;
    79using HeuristicLab.Problems.Instances.DataAnalysis;
     10using HeuristicLab.Random;
    811using Microsoft.VisualStudio.TestTools.UnitTesting;
    912
     
    4245    [TestCategory("Problems.DataAnalysis")]
    4346    [TestProperty("Time", "short")]
     47    public void CustomModelVariableImpactTest() {
     48      IClassificationProblemData problemData = CreateDefaultProblem();
     49      ISymbolicExpressionTree tree = CreateCustomExpressionTree();
     50      var model = new SymbolicNearestNeighbourClassificationModel(problemData.TargetVariable, 3, tree, new SymbolicDataAnalysisExpressionTreeInterpreter());
     51      model.RecalculateModelParameters(problemData, problemData.TrainingIndices);
     52      IClassificationSolution solution = new ClassificationSolution(model, (IClassificationProblemData)problemData.Clone());
     53      Dictionary<string, double> expectedImpacts = GetExpectedValuesForCustomProblem();
     54
     55      CheckDefaultAsserts(solution, expectedImpacts);
     56    }
     57
     58    [TestMethod]
     59    [TestCategory("Problems.DataAnalysis")]
     60    [TestProperty("Time", "short")]
     61    public void CustomModelVariableImpactNoInfluenceTest() {
     62      IClassificationProblemData problemData = CreateDefaultProblem();
     63      ISymbolicExpressionTree tree = CreateCustomExpressionTreeNoInfluenceX1();
     64      var model = new SymbolicNearestNeighbourClassificationModel(problemData.TargetVariable, 3, tree, new SymbolicDataAnalysisExpressionTreeInterpreter());
     65      model.RecalculateModelParameters(problemData, problemData.TrainingIndices);
     66      IClassificationSolution solution = new ClassificationSolution(model, (IClassificationProblemData)problemData.Clone());
     67      Dictionary<string, double> expectedImpacts = GetExpectedValuesForCustomProblemNoInfluence();
     68
     69      CheckDefaultAsserts(solution, expectedImpacts);
     70    }
     71
     72    [TestMethod]
     73    [TestCategory("Problems.DataAnalysis")]
     74    [TestProperty("Time", "short")]
    4475    [ExpectedException(typeof(ArgumentException))]
    4576    public void WrongDataSetTest() {
     
    6495      return provider.LoadData(instance);
    6596    }
     97    private IClassificationProblemData CreateDefaultProblem() {
     98      List<string> allowedInputVariables = new List<string>() { "x1", "x2", "x3", "x4", "x5" };
     99      string targetVariable = "y";
     100      var variableNames = allowedInputVariables.Union(targetVariable.ToEnumerable());
     101      double[,] variableValues = new double[100, variableNames.Count()];
     102
     103      FastRandom random = new FastRandom(12345);
     104      int len0 = variableValues.GetLength(0);
     105      int len1 = variableValues.GetLength(1);
     106      for (int i = 0; i < len0; i++) {
     107        for (int j = 0; j < len1; j++) {
     108          if (j == len1 - 1) {
     109            variableValues[i, j] = (j + i) % 2;
     110          } else {
     111            variableValues[i, j] = random.Next(1, 100);
     112          }
     113        }
     114      }
     115
     116      Dataset dataset = new Dataset(variableNames, variableValues);
     117      var ret = new ClassificationProblemData(dataset, allowedInputVariables, targetVariable);
     118
     119      ret.SetClassName(0, "NOK");
     120      ret.SetClassName(1, "OK");
     121      return ret;
     122    }
    66123    #endregion
    67124
    68125    #region Create SymbolicExpressionTree
     126    private ISymbolicExpressionTree CreateCustomExpressionTree() {
     127      return new InfixExpressionParser().Parse("x1*x2 - x2*x2 + x3*x3 + x4*x4 - x5*x5 + 14/12");
     128    }
    69129    private ISymbolicExpressionTree CreateCustomExpressionTreeNoInfluenceX1() {
    70130      return new InfixExpressionParser().Parse("x1/x1*x2 - x2*x2 + x3*x3 + x4*x4 - x5*x5 + 14/12");
     
    91151      return expectedImpacts;
    92152    }
     153    private Dictionary<string, double> GetExpectedValuesForCustomProblem() {
     154      Dictionary<string, double> expectedImpacts = new Dictionary<string, double>();
     155      expectedImpacts.Add("x1", 0.04);
     156      expectedImpacts.Add("x2", 0.22);
     157      expectedImpacts.Add("x3", 0.26);
     158      expectedImpacts.Add("x4", 0.24);
     159      expectedImpacts.Add("x5", 0.2);
     160
     161      return expectedImpacts;
     162    }
     163    private Dictionary<string, double> GetExpectedValuesForCustomProblemNoInfluence() {
     164      Dictionary<string, double> expectedImpacts = new Dictionary<string, double>();
     165      expectedImpacts.Add("x1", 0);
     166      expectedImpacts.Add("x2", 0.22);
     167      expectedImpacts.Add("x3", 0.14);
     168      expectedImpacts.Add("x4", 0.3);
     169      expectedImpacts.Add("x5", 0.44);
     170
     171      return expectedImpacts;
     172    }
    93173    #endregion
    94174
Note: See TracChangeset for help on using the changeset viewer.