def updateStatsForPredictor(self,predictedValue:float,instance:Instance): if not instance.classIsMissing(): self.m_WithClass+=instance.weight() if Utils.isMissingValue(predictedValue): self.m_Unclassified+=instance.weight() return self.m_SumClass+=instance.weight()*instance.classValue() self.m_SumSqrClass+=instance.weight()*instance.classValue()*instance.classValue() self.m_SumClassPredicted+=instance.weight()*instance.classValue()*predictedValue self.m_SumPredicted+=instance.weight()*predictedValue self.m_SumSqrPredicted+=instance.weight()*predictedValue*predictedValue self.updateNumericScores(self.makeDistribution(predictedValue),self.makeDistribution(instance.classValue()),instance.weight()) else: self.m_MissingClass+=instance.weight()
def updateStatsForClassifier(self,predictedDistribution:List,instance:Instance): actualClass=instance.classValue() if not instance.classIsMissing(): self.updateMargins(predictedDistribution,actualClass,instance.weight()) predictedClass=-1 bestProb=0 for i in range(self.m_NumClasses): if predictedDistribution[i] > bestProb: predictedClass=i bestProb=predictedDistribution[i] self.m_WithClass+=instance.weight() if predictedClass < 0: self.m_Unclassified+=instance.weight() return predictedProb=max(float('-inf'),predictedDistribution[actualClass]) priorProb=max(float('-inf'),self.m_ClassPriors[actualClass]/self.m_ClassPriorsSum) if predictedProb >= priorProb: self.m_SumKBInfo+= (Utils.log2(predictedProb) - Utils.log2(priorProb)) * instance.weight() else: self.m_SumKBInfo-= (Utils.log2(1 - predictedProb) - Utils.log2(1 - priorProb)) * instance.weight() self.m_SumSchemeEntropy-= Utils.log2(predictedProb) * instance.weight() self.m_SumPriorEntropy-= Utils.log2(priorProb) * instance.weight() self.updateNumericScores(predictedDistribution,self.makeDistribution(instance.classValue()),instance.weight()) indices= Utils.stableSort(predictedDistribution) sum=sizeOfregions=0 for i in range(len(predictedDistribution)-1,-1,-1): if sum >= self.m_ConfLevel: break sum+=predictedDistribution[indices[i]] sizeOfregions+=1 if actualClass == indices[i]: self.m_TotalCoverage+=instance.weight() self.m_TotalSizeOfRegions+=instance.weight()*sizeOfregions/(self.m_MaxTarget-self.m_MinTarget) self.m_ConfusionMatrix[actualClass][predictedClass]+=instance.weight() if predictedClass != actualClass: self.m_Incorrect+=instance.weight() else: self.m_Correct+=instance.weight() else: self.m_MissingClass+=instance.weight()
def evaluationForSingleInstance(self, a0, instance:Instance, storePredictions:bool): if isinstance(a0,List): if self.m_ClassIsNominal: pred= Utils.maxIndex(a0) if a0[int(pred)] <= 0: pred= Utils.missingValue() self.updateStatsForClassifier(a0, instance) if storePredictions and not self.m_DiscardPredictions: if self.m_Predictions is None: self.m_Predictions=[] self.m_Predictions.append(NominalPrediction(instance.classValue(), a0, instance.weight())) else: pred=a0[0] self.updateStatsForPredictor(pred,instance) if storePredictions and not self.m_DiscardPredictions: if self.m_Predictions is None: self.m_Predictions=[] self.m_Predictions.append(NumericPrediction(instance.classValue(),pred,instance.weight())) return pred elif isinstance(a0,Classifier): classMissing=copy.deepcopy(instance) classMissing.setDataset(instance.dataset()) #TODO # if isinstance(a0,InputMappedClassifier) # else: classMissing.setClassMissing() # print("isMiss: ", instance.value(5)) pred=self.evaluationForSingleInstance(a0.distributionForInstance(classMissing),instance,storePredictions) if not self.m_ClassIsNominal: if not instance.classIsMissing() and not Utils.isMissingValue(pred): if isinstance(a0,IntervalEstimator): self.updateStatsForIntervalEstimator(a0,classMissing,instance.classValue()) else: self.m_CoverageStatisticsAvailable=False if isinstance(a0,ConditionalDensityEstimator): self.updateStatsForConditionalDensityEstimator(a0,classMissing,instance.classValue()) else: self.m_ComplexityStatisticsAvailable=False return pred