Changeset 15545
- Timestamp:
- 12/19/17 12:14:52 (7 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/TSNE/TSNEAlgorithm.cs
r15532 r15545 53 53 } 54 54 public new IDataAnalysisProblem Problem { 55 get { return (IDataAnalysisProblem) 55 get { return (IDataAnalysisProblem)base.Problem; } 56 56 set { base.Problem = value; } 57 57 } … … 86 86 #region Parameter properties 87 87 public IFixedValueParameter<DoubleValue> PerplexityParameter { 88 get { return (IFixedValueParameter<DoubleValue>) 88 get { return (IFixedValueParameter<DoubleValue>)Parameters[PerplexityParameterName]; } 89 89 } 90 90 public IFixedValueParameter<PercentValue> ThetaParameter { 91 get { return (IFixedValueParameter<PercentValue>) 91 get { return (IFixedValueParameter<PercentValue>)Parameters[ThetaParameterName]; } 92 92 } 93 93 public IFixedValueParameter<IntValue> NewDimensionsParameter { 94 get { return (IFixedValueParameter<IntValue>) 94 get { return (IFixedValueParameter<IntValue>)Parameters[NewDimensionsParameterName]; } 95 95 } 96 96 public IConstrainedValueParameter<IDistance<double[]>> DistanceFunctionParameter { 97 get { return (IConstrainedValueParameter<IDistance<double[]>>) 97 get { return (IConstrainedValueParameter<IDistance<double[]>>)Parameters[DistanceFunctionParameterName]; } 98 98 } 99 99 public IFixedValueParameter<IntValue> MaxIterationsParameter { 100 get { return (IFixedValueParameter<IntValue>) 100 get { return (IFixedValueParameter<IntValue>)Parameters[MaxIterationsParameterName]; } 101 101 } 102 102 public IFixedValueParameter<IntValue> StopLyingIterationParameter { 103 get { return (IFixedValueParameter<IntValue>) 103 get { return (IFixedValueParameter<IntValue>)Parameters[StopLyingIterationParameterName]; } 104 104 } 105 105 public IFixedValueParameter<IntValue> MomentumSwitchIterationParameter { 106 get { return (IFixedValueParameter<IntValue>) 106 get { return (IFixedValueParameter<IntValue>)Parameters[MomentumSwitchIterationParameterName]; } 107 107 } 108 108 public IFixedValueParameter<DoubleValue> InitialMomentumParameter { 109 get { return (IFixedValueParameter<DoubleValue>) 109 get { return (IFixedValueParameter<DoubleValue>)Parameters[InitialMomentumParameterName]; } 110 110 } 111 111 public IFixedValueParameter<DoubleValue> FinalMomentumParameter { 112 get { return (IFixedValueParameter<DoubleValue>) 112 get { return (IFixedValueParameter<DoubleValue>)Parameters[FinalMomentumParameterName]; } 113 113 } 114 114 public IFixedValueParameter<DoubleValue> EtaParameter { 115 get { return (IFixedValueParameter<DoubleValue>) 115 get { return (IFixedValueParameter<DoubleValue>)Parameters[EtaParameterName]; } 116 116 } 117 117 public IFixedValueParameter<BoolValue> SetSeedRandomlyParameter { 118 get { return (IFixedValueParameter<BoolValue>) 118 get { return (IFixedValueParameter<BoolValue>)Parameters[SetSeedRandomlyParameterName]; } 119 119 } 120 120 public IFixedValueParameter<IntValue> SeedParameter { 121 get { return (IFixedValueParameter<IntValue>) 121 get { return (IFixedValueParameter<IntValue>)Parameters[SeedParameterName]; } 122 122 } 123 123 public IConstrainedValueParameter<StringValue> ClassesNameParameter { 124 get { return (IConstrainedValueParameter<StringValue>) 124 get { return (IConstrainedValueParameter<StringValue>)Parameters[ClassesNameParameterName]; } 125 125 } 126 126 public IFixedValueParameter<BoolValue> NormalizationParameter { 127 get { return (IFixedValueParameter<BoolValue>) 127 get { return (IFixedValueParameter<BoolValue>)Parameters[NormalizationParameterName]; } 128 128 } 129 129 public IFixedValueParameter<BoolValue> RandomInitializationParameter { 130 get { return (IFixedValueParameter<BoolValue>) 130 get { return (IFixedValueParameter<BoolValue>)Parameters[RandomInitializationParameterName]; } 131 131 } 132 132 public IFixedValueParameter<IntValue> UpdateIntervalParameter { 133 get { return (IFixedValueParameter<IntValue>) 133 get { return (IFixedValueParameter<IntValue>)Parameters[UpdateIntervalParameterName]; } 134 134 } 135 135 #endregion … … 218 218 [StorableHook(HookType.AfterDeserialization)] 219 219 private void AfterDeserialization() { 220 if ( Parameters.ContainsKey(RandomInitializationParameterName))220 if (!Parameters.ContainsKey(RandomInitializationParameterName)) 221 221 Parameters.Add(new FixedValueParameter<BoolValue>(RandomInitializationParameterName, "Wether data points should be randomly initialized or according to the first 2 dimensions", new BoolValue(true))); 222 222 RegisterParameterEvents(); … … 284 284 if (state == null) { 285 285 if (SetSeedRandomly) Seed = new System.Random().Next(); 286 var random = new MersenneTwister((uint) 286 var random = new MersenneTwister((uint)Seed); 287 287 var dataset = problemData.Dataset; 288 288 var allowedInputVariables = problemData.AllowedInputVariables.ToArray(); … … 401 401 var classificationData = problemData as ClassificationProblemData; 402 402 if (classificationData != null && classificationData.TargetVariable.Equals(ClassesName)) { 403 var classNames = classificationData.ClassValues.Zip(classificationData.ClassNames, (v, n) => new { v, n}).ToDictionary(x => x.v, x => x.n);403 var classNames = classificationData.ClassValues.Zip(classificationData.ClassNames, (v, n) => new { v, n }).ToDictionary(x => x.v, x => x.n); 404 404 var classes = classificationData.Dataset.GetDoubleValues(classificationData.TargetVariable, allIndices).Select(v => classNames[v]).ToArray(); 405 405 for (var i = 0; i < classes.Length; i++) { … … 407 407 dataRowNames[classes[i]].Add(i); 408 408 } 409 } 410 else if (((Dataset) problemData.Dataset).VariableHasType<string>(ClassesName)) { 409 } else if (((Dataset)problemData.Dataset).VariableHasType<string>(ClassesName)) { 411 410 var classes = problemData.Dataset.GetStringValues(ClassesName, allIndices).ToArray(); 412 411 for (var i = 0; i < classes.Length; i++) { … … 414 413 dataRowNames[classes[i]].Add(i); 415 414 } 416 } 417 else if (((Dataset) problemData.Dataset).VariableHasType<double>(ClassesName)) { 415 } else if (((Dataset)problemData.Dataset).VariableHasType<double>(ClassesName)) { 418 416 var clusterdata = new Dataset(problemData.Dataset.DoubleVariables, problemData.Dataset.DoubleVariables.Select(v => problemData.Dataset.GetDoubleValues(v, allIndices).ToList())); 419 417 const int contours = 8; … … 422 420 double[][] borders; 423 421 CreateClusters(clusterdata, ClassesName, contours, out clusterModel, out contourMap, out borders); 424 var contourorder = borders.Select((x, i) => new { x, i}).OrderBy(x => x.x[0]).Select(x => x.i).ToArray();422 var contourorder = borders.Select((x, i) => new { x, i }).OrderBy(x => x.x[0]).Select(x => x.i).ToArray(); 425 423 for (var i = 0; i < contours; i++) { 426 424 var c = contourorder[i]; … … 432 430 var allClusters = clusterModel.GetClusterValues(clusterdata, Enumerable.Range(0, clusterdata.Rows)).ToArray(); 433 431 for (var i = 0; i < clusterdata.Rows; i++) dataRowNames[contourMap[allClusters[i] - 1]].Add(i); 434 } 435 else if (((Dataset) problemData.Dataset).VariableHasType<DateTime>(ClassesName)) { 432 } else if (((Dataset)problemData.Dataset).VariableHasType<DateTime>(ClassesName)) { 436 433 var clusterdata = new Dataset(problemData.Dataset.DateTimeVariables, problemData.Dataset.DateTimeVariables.Select(v => problemData.Dataset.GetDoubleValues(v, allIndices).ToList())); 437 434 const int contours = 8; … … 440 437 double[][] borders; 441 438 CreateClusters(clusterdata, ClassesName, contours, out clusterModel, out contourMap, out borders); 442 var contourorder = borders.Select((x, i) => new { x, i}).OrderBy(x => x.x[0]).Select(x => x.i).ToArray();439 var contourorder = borders.Select((x, i) => new { x, i }).OrderBy(x => x.x[0]).Select(x => x.i).ToArray(); 443 440 for (var i = 0; i < contours; i++) { 444 441 var c = contourorder[i]; … … 450 447 var allClusters = clusterModel.GetClusterValues(clusterdata, Enumerable.Range(0, clusterdata.Rows)).ToArray(); 451 448 for (var i = 0; i < clusterdata.Rows; i++) dataRowNames[contourMap[allClusters[i] - 1]].Add(i); 452 } 453 else { 449 } else { 454 450 dataRowNames.Add("Training", problemData.TrainingIndices.ToList()); 455 451 dataRowNames.Add("Test", problemData.TestIndices.ToList()); … … 465 461 var c = tsneState.EvaluateError(); 466 462 errors.Add(c); 467 ((IntValue) 468 ((DoubleValue) 463 ((IntValue)results[IterationResultName].Value).Value = tsneState.iter; 464 ((DoubleValue)results[ErrorResultName].Value).Value = errors.Last(); 469 465 470 466 var ndata = NormalizeProjectedData(tsneState.newData); … … 490 486 for (var i = 0; i < max.Length; i++) max[i] = min[i] = data[0, i]; 491 487 for (var i = 0; i < data.GetLength(0); i++) 492 for (var j = 0; j < data.GetLength(1); j++) {493 var v = data[i, j];494 max[j] = Math.Max(max[j], v);495 min[j] = Math.Min(min[j], v);496 }488 for (var j = 0; j < data.GetLength(1); j++) { 489 var v = data[i, j]; 490 max[j] = Math.Max(max[j], v); 491 min[j] = Math.Min(min[j], v); 492 } 497 493 for (var i = 0; i < data.GetLength(0); i++) { 498 494 for (var j = 0; j < data.GetLength(1); j++) { … … 528 524 529 525 private static void CreateClusters(IDataset data, string target, int contours, out IClusteringModel contourCluster, out Dictionary<int, string> contourNames, out double[][] borders) { 530 var cpd = new ClusteringProblemData((Dataset) data, new[] {target});526 var cpd = new ClusteringProblemData((Dataset)data, new[] { target }); 531 527 contourCluster = KMeansClustering.CreateKMeansSolution(cpd, contours, 3).Model; 532 528 533 borders = Enumerable.Range(0, contours).Select(x => new[] { double.MaxValue, double.MinValue}).ToArray();529 borders = Enumerable.Range(0, contours).Select(x => new[] { double.MaxValue, double.MinValue }).ToArray(); 534 530 var clusters = contourCluster.GetClusterValues(cpd.Dataset, cpd.AllIndices).ToArray(); 535 531 var targetvalues = cpd.Dataset.GetDoubleValues(target).ToArray(); … … 550 546 var range = high - low; 551 547 var h = Math.Min(cell / range * colorGradient.Count, colorGradient.Count - 1); 552 return colorGradient[(int) 548 return colorGradient[(int)h]; 553 549 } 554 550 #endregion
Note: See TracChangeset
for help on using the changeset viewer.