Changeset 8660 for branches/GP-MoveOperators/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/Classification/ClassificationProblemData.cs
- Timestamp:
- 09/14/12 18:58:15 (12 years ago)
- Location:
- branches/GP-MoveOperators
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/GP-MoveOperators
- Property svn:ignore
-
old new 21 21 protoc.exe 22 22 _ReSharper.HeuristicLab 3.3 Tests 23 Google.ProtocolBuffers-2.4.1.473.dll
-
- Property svn:mergeinfo changed
- Property svn:ignore
-
branches/GP-MoveOperators/HeuristicLab.Problems.DataAnalysis
- Property svn:mergeinfo changed
-
branches/GP-MoveOperators/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/Classification/ClassificationProblemData.cs
r8206 r8660 223 223 } 224 224 225 private List<double> classValues ;226 p ublic List<double> ClassValues{225 private List<double> classValuesCache; 226 private List<double> ClassValuesCache { 227 227 get { 228 if (classValues == null) { 229 classValues = Dataset.GetDoubleValues(TargetVariableParameter.Value.Value).Distinct().ToList(); 230 classValues.Sort(); 228 if (classValuesCache == null) { 229 classValuesCache = Dataset.GetDoubleValues(TargetVariableParameter.Value.Value).Distinct().OrderBy(x => x).ToList(); 231 230 } 232 return classValues ;231 return classValuesCache; 233 232 } 234 233 } 235 IEnumerable<double> IClassificationProblemData.ClassValues { 236 get { return ClassValues; } 237 } 238 234 public IEnumerable<double> ClassValues { 235 get { return ClassValuesCache; } 236 } 239 237 public int Classes { 240 get { return ClassValues .Count; }241 } 242 243 private List<string> classNames ;244 p ublic List<string> ClassNames{238 get { return ClassValuesCache.Count; } 239 } 240 241 private List<string> classNamesCache; 242 private List<string> ClassNamesCache { 245 243 get { 246 if (classNames == null) {247 classNames = new List<string>();244 if (classNamesCache == null) { 245 classNamesCache = new List<string>(); 248 246 for (int i = 0; i < ClassNamesParameter.Value.Rows; i++) 249 classNames .Add(ClassNamesParameter.Value[i, 0]);247 classNamesCache.Add(ClassNamesParameter.Value[i, 0]); 250 248 } 251 return classNames ;249 return classNamesCache; 252 250 } 253 251 } 254 IEnumerable<string> IClassificationProblemData.ClassNames { 255 get { return ClassNames; } 256 } 257 258 private Dictionary<Tuple<double, double>, double> classificationPenaltiesCache = new Dictionary<Tuple<double, double>, double>(); 252 public IEnumerable<string> ClassNames { 253 get { return ClassNamesCache; } 254 } 259 255 #endregion 260 256 … … 277 273 278 274 public ClassificationProblemData() : this(defaultDataset, defaultAllowedInputVariables, defaultTargetVariable) { } 275 276 public ClassificationProblemData(IClassificationProblemData classificationProblemData) 277 : this(classificationProblemData.Dataset, classificationProblemData.AllowedInputVariables, classificationProblemData.TargetVariable) { 278 TrainingPartition.Start = classificationProblemData.TrainingPartition.Start; 279 TrainingPartition.End = classificationProblemData.TrainingPartition.End; 280 TestPartition.Start = classificationProblemData.TestPartition.Start; 281 TestPartition.End = classificationProblemData.TestPartition.End; 282 } 283 279 284 public ClassificationProblemData(Dataset dataset, IEnumerable<string> allowedInputVariables, string targetVariable) 280 285 : base(dataset, allowedInputVariables) { … … 310 315 DeregisterParameterEvents(); 311 316 312 classNames = null;313 317 ((IStringConvertibleMatrix)ClassNamesParameter.Value).Columns = 1; 314 ((IStringConvertibleMatrix)ClassNamesParameter.Value).Rows = ClassValues .Count;318 ((IStringConvertibleMatrix)ClassNamesParameter.Value).Rows = ClassValuesCache.Count; 315 319 for (int i = 0; i < Classes; i++) 316 ClassNamesParameter.Value[i, 0] = "Class " + ClassValues [i];320 ClassNamesParameter.Value[i, 0] = "Class " + ClassValuesCache[i]; 317 321 ClassNamesParameter.Value.ColumnNames = new List<string>() { "ClassNames" }; 318 322 ClassNamesParameter.Value.RowNames = ClassValues.Select(s => "ClassValue: " + s); 319 323 320 classificationPenaltiesCache.Clear();321 ((ValueParameter<DoubleMatrix>)ClassificationPenaltiesParameter).ReactOnValueToStringChangedAndValueItemImageChanged = false;322 324 ((IStringConvertibleMatrix)ClassificationPenaltiesParameter.Value).Rows = Classes; 323 325 ((IStringConvertibleMatrix)ClassificationPenaltiesParameter.Value).Columns = Classes; … … 330 332 } 331 333 } 332 ((ValueParameter<DoubleMatrix>)ClassificationPenaltiesParameter).ReactOnValueToStringChangedAndValueItemImageChanged = true;333 334 RegisterParameterEvents(); 334 335 } 335 336 336 337 public string GetClassName(double classValue) { 337 if (!ClassValues .Contains(classValue)) throw new ArgumentException();338 int index = ClassValues .IndexOf(classValue);339 return ClassNames [index];338 if (!ClassValuesCache.Contains(classValue)) throw new ArgumentException(); 339 int index = ClassValuesCache.IndexOf(classValue); 340 return ClassNamesCache[index]; 340 341 } 341 342 public double GetClassValue(string className) { 342 if (!ClassNames .Contains(className)) throw new ArgumentException();343 int index = ClassNames .IndexOf(className);344 return ClassValues [index];343 if (!ClassNamesCache.Contains(className)) throw new ArgumentException(); 344 int index = ClassNamesCache.IndexOf(className); 345 return ClassValuesCache[index]; 345 346 } 346 347 public void SetClassName(double classValue, string className) { 347 if (!classValues.Contains(classValue)) throw new ArgumentException(); 348 int index = ClassValues.IndexOf(classValue); 349 ClassNames[index] = className; 348 if (!ClassValuesCache.Contains(classValue)) throw new ArgumentException(); 349 int index = ClassValuesCache.IndexOf(classValue); 350 350 ClassNamesParameter.Value[index, 0] = className; 351 // updating of class names cache is not necessary here as the parameter value fires a changed event which updates the cache 351 352 } 352 353 … … 355 356 } 356 357 public double GetClassificationPenalty(double correctClassValue, double estimatedClassValue) { 357 var key = Tuple.Create(correctClassValue, estimatedClassValue); 358 if (!classificationPenaltiesCache.ContainsKey(key)) { 359 int correctClassIndex = ClassValues.IndexOf(correctClassValue); 360 int estimatedClassIndex = ClassValues.IndexOf(estimatedClassValue); 361 classificationPenaltiesCache[key] = ClassificationPenaltiesParameter.Value[correctClassIndex, estimatedClassIndex]; 362 } 363 return classificationPenaltiesCache[key]; 358 int correctClassIndex = ClassValuesCache.IndexOf(correctClassValue); 359 int estimatedClassIndex = ClassValuesCache.IndexOf(estimatedClassValue); 360 return ClassificationPenaltiesParameter.Value[correctClassIndex, estimatedClassIndex]; 364 361 } 365 362 public void SetClassificationPenalty(string correctClassName, string estimatedClassName, double penalty) { … … 367 364 } 368 365 public void SetClassificationPenalty(double correctClassValue, double estimatedClassValue, double penalty) { 369 var key = Tuple.Create(correctClassValue, estimatedClassValue); 370 int correctClassIndex = ClassValues.IndexOf(correctClassValue); 371 int estimatedClassIndex = ClassValues.IndexOf(estimatedClassValue); 366 int correctClassIndex = ClassValuesCache.IndexOf(correctClassValue); 367 int estimatedClassIndex = ClassValuesCache.IndexOf(estimatedClassValue); 372 368 373 369 ClassificationPenaltiesParameter.Value[correctClassIndex, estimatedClassIndex] = penalty; … … 379 375 ClassNamesParameter.Value.Reset += new EventHandler(Parameter_ValueChanged); 380 376 ClassNamesParameter.Value.ItemChanged += new EventHandler<EventArgs<int, int>>(MatrixParameter_ItemChanged); 381 ClassificationPenaltiesParameter.Value.Reset += new EventHandler(Parameter_ValueChanged);382 ClassificationPenaltiesParameter.Value.ItemChanged += new EventHandler<EventArgs<int, int>>(MatrixParameter_ItemChanged);383 377 } 384 378 private void DeregisterParameterEvents() { … … 386 380 ClassNamesParameter.Value.Reset -= new EventHandler(Parameter_ValueChanged); 387 381 ClassNamesParameter.Value.ItemChanged -= new EventHandler<EventArgs<int, int>>(MatrixParameter_ItemChanged); 388 ClassificationPenaltiesParameter.Value.Reset -= new EventHandler(Parameter_ValueChanged);389 ClassificationPenaltiesParameter.Value.ItemChanged -= new EventHandler<EventArgs<int, int>>(MatrixParameter_ItemChanged);390 382 } 391 383 392 384 private void TargetVariableParameter_ValueChanged(object sender, EventArgs e) { 393 classValues = null; 385 classValuesCache = null; 386 classNamesCache = null; 394 387 ResetTargetVariableDependentMembers(); 395 388 OnChanged(); 396 389 } 397 390 private void Parameter_ValueChanged(object sender, EventArgs e) { 391 classNamesCache = null; 398 392 OnChanged(); 399 393 } 400 394 private void MatrixParameter_ItemChanged(object sender, EventArgs<int, int> e) { 395 classNamesCache = null; 401 396 OnChanged(); 402 397 }
Note: See TracChangeset
for help on using the changeset viewer.