- Timestamp:
- 10/20/08 09:47:32 (16 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.GP.StructureIdentification.Classification/ROCAnalyzer.cs
r678 r696 36 36 37 37 public override string Description { 38 get { return @"Calculate TPR & FPR for various t resholds on dataset"; }38 get { return @"Calculate TPR & FPR for various thresholds on dataset"; } 39 39 } 40 40 41 41 public ROCAnalyzer() 42 42 : base() { 43 AddVariableInfo(new VariableInfo("Values", "Item list holding the estimated and orig nial values for the ROCAnalyzer", typeof(ItemList), VariableKind.In));43 AddVariableInfo(new VariableInfo("Values", "Item list holding the estimated and original values for the ROCAnalyzer", typeof(ItemList), VariableKind.In)); 44 44 AddVariableInfo(new VariableInfo("ROCValues", "The values of the ROCAnalyzer, namely TPR & FPR", typeof(ItemList), VariableKind.New | VariableKind.Out)); 45 45 AddVariableInfo(new VariableInfo("AUCValues", "The AUC Values for each ROC", typeof(ItemList<DoubleData>), VariableKind.New | VariableKind.Out)); … … 89 89 foreach (double key in classes.Keys) 90 90 classes[key].Sort(); 91 92 //check for 2 classes classification problem 93 //if (classes.Keys.Count != 2) 94 // throw new Exception("ROCAnalyser only handles 2 class classification problems"); 95 91 96 92 //calculate ROC Curve 97 93 foreach (double key in classes.Keys) { … … 103 99 104 100 protected void CalculateBestROC(double positiveClassKey, SortedDictionary<double, List<double>> classes) { 105 106 int rocIndex = myRocValues.Count - 1;107 101 List<KeyValuePair<double, double>> rocCharacteristics; 108 102 List<KeyValuePair<double, double>> bestROC; … … 132 126 rocCharacteristics = null; 133 127 bestROC = new List<KeyValuePair<double, double>>(); 134 foreach (double minT reshold in classes[positiveClassKey].Distinct<double>()) {135 CalculateROCValuesAndAUC(classes[positiveClassKey], actNegatives, negatives.Count, minT reshold, ref rocCharacteristics, out actROC, out actAUC);128 foreach (double minThreshold in classes[positiveClassKey].Distinct<double>()) { 129 CalculateROCValuesAndAUC(classes[positiveClassKey], actNegatives, negatives.Count, minThreshold, ref rocCharacteristics, out actROC, out actAUC); 136 130 if (actAUC > bestAUC) { 137 131 bestAUC = actAUC; … … 154 148 } 155 149 156 protected void CalculateROCValuesAndAUC(List<double> positives, List<double> negatives, int negativesCount, double minT reshold,150 protected void CalculateROCValuesAndAUC(List<double> positives, List<double> negatives, int negativesCount, double minThreshold, 157 151 ref List<KeyValuePair<double, double>> rocCharacteristics, out List<KeyValuePair<double, double>> roc, out double auc) { 158 152 double actTP = -1; … … 163 157 roc = new List<KeyValuePair<double, double>>(); 164 158 165 actTP = positives.Count<double>(value => minT reshold <= value && value <= negatives.Max<double>());166 actFP = negatives.Count<double>(value => minT reshold <= value && value <= negatives.Max<double>());159 actTP = positives.Count<double>(value => minThreshold <= value && value <= negatives.Max<double>()); 160 actFP = negatives.Count<double>(value => minThreshold <= value ); 167 161 //add point (1,TPR) for AUC 'correct' calculation 168 162 roc.Add(new KeyValuePair<double, double>(1, actTP / positives.Count)); … … 173 167 if (rocCharacteristics == null) { 174 168 rocCharacteristics = new List<KeyValuePair<double, double>>(); 175 foreach (double maxT reshold in negatives.Distinct<double>()) {169 foreach (double maxThreshold in negatives.Distinct<double>()) { 176 170 auc += ((oldTP + actTP) / positives.Count) * ((oldFP - actFP) / negativesCount) / 2; 177 171 oldTP = actTP; 178 172 oldFP = actFP; 179 actTP = positives.Count<double>(value => minT reshold <= value && value < maxTreshold);180 actFP = negatives.Count<double>(value => minT reshold <= value && value < maxTreshold);173 actTP = positives.Count<double>(value => minThreshold <= value && value < maxThreshold); 174 actFP = negatives.Count<double>(value => minThreshold <= value && value < maxThreshold); 181 175 rocCharacteristics.Add(new KeyValuePair<double, double>(oldTP - actTP, oldFP - actFP)); 182 176 roc.Add(new KeyValuePair<double, double>(actFP / negativesCount, actTP / positives.Count)); 183 177 184 178 //stop calculation if truePositiveRate == 0 => straight line with y=0 & save runtime 185 if ((actTP / positives.Count == 0) || (actFP / negatives.Count== 0))179 if ((actTP == 0) || (actFP == 0)) 186 180 break; 187 181 } … … 195 189 actFP = oldFP - rocCharac.Value; 196 190 roc.Add(new KeyValuePair<double, double>(actFP / negativesCount, actTP / positives.Count)); 197 if ( actTP / positives.Count == 0)191 if ((actTP == 0) || (actFP == 0)) 198 192 break; 199 193 } … … 219 213 roc.Add(new KeyValuePair<double, double>(actFP / negativesCount, actTP / positives.Count)); 220 214 221 foreach (double minT reshold in negatives.Distinct<double>()) {215 foreach (double minThreshold in negatives.Distinct<double>()) { 222 216 auc += ((oldTP + actTP) / positives.Count) * ((oldFP - actFP) / negativesCount) / 2; 223 217 oldTP = actTP; 224 218 oldFP = actFP; 225 actTP = positives.Count<double>(value => minT reshold <=value);226 actFP = negatives.Count<double>(value => minT reshold <=value);219 actTP = positives.Count<double>(value => minThreshold < value); 220 actFP = negatives.Count<double>(value => minThreshold < value); 227 221 roc.Add(new KeyValuePair<double, double>(actFP / negativesCount, actTP / positives.Count)); 228 222 229 223 //stop calculation if truePositiveRate == 0 => straight line with y=0 & save runtime 230 if ( (actTP / positives.Count == 0) || (actFP / negatives.Count == 0))224 if (actTP == 0 || actFP==0) 231 225 break; 232 226 }
Note: See TracChangeset
for help on using the changeset viewer.