Changeset 16065 for branches/2904_CalculateImpacts/HeuristicLab.Tests/HeuristicLab.Problems.DataAnalysis-3.4/ClassificationVariableImpactCalculationTest.cs
- Timestamp:
- 08/07/18 16:32:12 (6 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/2904_CalculateImpacts/HeuristicLab.Tests/HeuristicLab.Problems.DataAnalysis-3.4/ClassificationVariableImpactCalculationTest.cs
r16061 r16065 3 3 using System.Linq; 4 4 using HeuristicLab.Algorithms.DataAnalysis; 5 using HeuristicLab.Common; 5 6 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding; 6 7 using HeuristicLab.Problems.DataAnalysis.Symbolic; 8 using HeuristicLab.Problems.DataAnalysis.Symbolic.Classification; 7 9 using HeuristicLab.Problems.Instances.DataAnalysis; 10 using HeuristicLab.Random; 8 11 using Microsoft.VisualStudio.TestTools.UnitTesting; 9 12 … … 42 45 [TestCategory("Problems.DataAnalysis")] 43 46 [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")] 44 75 [ExpectedException(typeof(ArgumentException))] 45 76 public void WrongDataSetTest() { … … 64 95 return provider.LoadData(instance); 65 96 } 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 } 66 123 #endregion 67 124 68 125 #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 } 69 129 private ISymbolicExpressionTree CreateCustomExpressionTreeNoInfluenceX1() { 70 130 return new InfixExpressionParser().Parse("x1/x1*x2 - x2*x2 + x3*x3 + x4*x4 - x5*x5 + 14/12"); … … 91 151 return expectedImpacts; 92 152 } 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 } 93 173 #endregion 94 174
Note: See TracChangeset
for help on using the changeset viewer.