Ejemplo n.º 1
0
 def _getProbabilities(self, ProbOf1):
     """Get the orange like output probabilities for the current predicted example"""
     #This is only valid for binary classifiers opencv limitation!
     # From openCv we know that the probability returned for this example represents the fraction of tree votes for class 1
     #Find the classValue string that is represented by the scalar 1 in opencvRF
     class1 = dataUtilities.CvMat2orangeResponse(1, self.classVar).value
     dist = orange.DiscDistribution(self.classVar)
     dist[self.classVar.values.index(class1)] = ProbOf1
     dist[not self.classVar.values.index(class1)] = 1 - ProbOf1
     return dist
Ejemplo n.º 2
0
    def _singlePredict(self, origExamples = None, resultType = orange.GetValue, returnDFV = False):
        """
        orange.GetBoth -          <type 'tuple'>                     ->    (<orange.Value 'Act'='3.44158792'>, <3.442: 1.000>)
        orange.GetValue -         <type 'orange.Value'>              ->    <orange.Value 'Act'='3.44158792'>
        orange.GetProbabilities - <type 'orange.DiscDistribution'>   ->    <0.000, 0.000>
        returnDFV - Flag indicating to return the Decision Function Value. If set to True, it will encapsulate the original result asked by the keyword resultType and the DFV into a tuple:
                ((<orange.Value 'Act'='3.44158792'>, <3.442: 1.000>), 2.34443)
                (<orange.Value 'Act'='3.44158792'>, 2.34443) 
                (<0.000, 0.000>, 2.34443)
                If it is not a binary classifier, DFV will be equal to None
                DFV will be a value from greater or equal to 0  
        """
        res = None
        #dataUtilities.rmAllMeta(examples)
        if len(origExamples.domain.getmetas()) == 0:
            examples = origExamples
        else:
            examples = dataUtilities.getCopyWithoutMeta(origExamples)

        #Check if the examples are compatible with the classifier (attributes order and varType compatibility)
        if self.imputer:
            dataUtilities.verbose = self.verbose
            if not self.ExFix.ready:
                self.ExFix.set_domain(self.imputer.defaults.domain)
                self.ExFix.set_examplesFixedLog(self.examplesFixedLog)
            inExamples = self.ExFix.fixExample(examples)

            if not inExamples:
                if self.verbose > 0: print "Warning no example. Returning None prediction"
                return None

            #Imput the examples if there are missing values     
            examplesImp = self.imputer(inExamples)
            # There is a problem with using the imputer when examples contain meta attributes.
            # Unable to remove meta attributes from the examples. OK to rm meta from ExampleTables, but not from Example objects.
            if not examplesImp:
                if self.verbose > 0: print "Unable to predict with the SVM model."
                if self.verbose > 0: print "Perhaps you need to remove meta attributes from your examples."
                return None
        else:
            if self.verbose > 0: print "Warning: No Imputer in SVM Classifier"
            examplesImp = examples

        if self.classifier.get_support_vector_count() ==0:
            if self.verbose > 0: print "WARNING:  Support Vectors count is 0 (zero)" 
        DFV = None
        if examplesImp: 
            if self.scalizer:
                exToPredict = dataUtilities.Example2CvMat(self.scalizer.scaleEx(examplesImp,True), self.varNames)
                res = self.classifier.predict(exToPredict)
                res = self.scalizer.convertClass(res)
                if self.classVar.varType != orange.VarTypes.Continuous and len(self.classVar.values) == 2 and returnDFV:
                    DFV = self.classifier.predict(exToPredict, True)
                else:
                    #On Regression models assume the DVF as the value predicted
                    DFV = res 
                self._updateDFVExtremes(DFV)
                res = dataUtilities.CvMat2orangeResponse(res,self.classVar)
            else:
                exToPredict = dataUtilities.Example2CvMat(examplesImp,self.varNames)
                res = self.classifier.predict(exToPredict)
                if self.classVar.varType != orange.VarTypes.Continuous and len(self.classVar.values) == 2 and returnDFV:
                    DFV = self.classifier.predict(exToPredict, True)
                else:
                    #On Regression models assume the DVF as the value predicted
                    DFV = res
                self._updateDFVExtremes(DFV)
                res = dataUtilities.CvMat2orangeResponse(res,self.classVar)
             
            if resultType!=orange.GetValue:
                if examplesImp.domain.classVar.varType != orange.VarTypes.Continuous:
                    dist = orange.DiscDistribution(examplesImp.domain.classVar)
                    dist[res]=1
                else:
                    y_hat = self.classVar(res)
                    dist = Orange.statistics.distribution.Continuous(self.classVar)
                    dist[y_hat] = 1.0
                if resultType==orange.GetProbabilities:
                    res = dist
                else:
                    res = (res,dist)
                    
            if returnDFV:
                res = (res,DFV)
                
        self.nPredictions += 1
        return res
Ejemplo n.º 3
0
    def _singlePredict(self,
                       origExamples=None,
                       resultType=orange.GetValue,
                       returnDFV=False):
        res = None
        """
        orange.GetBoth -          <type 'tuple'>                     ->    (<orange.Value 'Act'='3.44158792'>, <3.442: 1.000>)
        orange.GetValue -         <type 'orange.Value'>              ->    <orange.Value 'Act'='3.44158792'>
        orange.GetProbabilities - <type 'orange.DiscDistribution'>   ->    <0.000, 0.000> 
        """
        #dataUtilities.rmAllMeta(examples)
        if len(origExamples.domain.getmetas()) == 0:
            examples = origExamples
        else:
            examples = dataUtilities.getCopyWithoutMeta(origExamples)
        #Check if the examples are compatible with the classifier (attributes order and varType compatibility)
        if self.verbose > 1: dataUtilities.verbose = self.verbose
        if not self.ExFix.ready:
            self.ExFix.set_domain(self.imputer.defaults.domain)
            self.ExFix.set_examplesFixedLog(self.examplesFixedLog)
        inExamples = self.ExFix.fixExample(examples)

        if not inExamples:
            return None

        #Imput the examples if there are missing values
        examplesImp = self.imputer(inExamples)
        # There is a problem with using the imputer when examples contain meta attributes.
        # Unable to remove meta attributes from the examples. OK to rm meta from ExampleTables, but not from Example objects.
        if not examplesImp:
            if self.verbose > 0: print "Unable to predict with the ANN model."
            if self.verbose > 0:
                print "Perhaps you need to remove meta attributes from your examples."
            return None

        res = None
        if self.classVar.varType == orange.VarTypes.Continuous:
            Nout = 1
        else:
            Nout = len(self.classVar.values)
        out = cv.cvCreateMat(1, Nout, cv.CV_32FC1)
        self.classifier.predict(
            dataUtilities.Example2CvMat(examplesImp, self.varNames), out)
        #print "OUT = ",out
        #print out,"->",dataUtilities.CvMat2orangeResponse(out,self.classVar,True),":",origExamples[self.classVar.name].value
        res = dataUtilities.CvMat2orangeResponse(out, self.classVar, True)
        #print "RES=",res
        DFV = None
        if out.cols > 1:
            fannOutVector = dataUtilities.CvMat2List(out)[0]
            probabilities = self.__getProbabilities(fannOutVector)
            #Compute the DFV
            if self.classVar.varType == orange.VarTypes.Discrete and len(
                    self.classVar.values) == 2:
                DFV = probabilities[0]
                # Subtract 0.5 so that the threshold is 0 as all learners DFV
                DFV -= 0.5
                self._updateDFVExtremes(DFV)

            # Retrun the desired quantity
            if resultType == orange.GetProbabilities:
                res = probabilities
            else:
                if resultType == orange.GetBoth:
                    res = (res, probabilities)
        else:
            #On Regression models, assume the DFV as the value predicted
            DFV = res.value
            self._updateDFVExtremes(DFV)
            y_hat = self.classVar(res.value)
            dist = Orange.statistics.distribution.Continuous(self.classVar)
            dist[y_hat] = 1.0
            if resultType == orange.GetProbabilities:
                res = dist
            else:
                if resultType == orange.GetBoth:
                    res = (res, dist)
        self.nPredictions += 1

        if returnDFV:
            return (res, DFV)
        else:
            return res
Ejemplo n.º 4
0
    def _singlePredict(self, origExample = None, resultType = orange.GetValue, returnDFV = False):
        """
        orange.GetBoth -          <type 'tuple'>                     ->    (<orange.Value 'Act'='3.44158792'>, <3.442: 1.000>)
        orange.GetValue -         <type 'orange.Value'>              ->    <orange.Value 'Act'='3.44158792'>
        orange.GetProbabilities - <type 'orange.DiscDistribution'>   ->    <0.000, 0.000>
        returnDFV - Flag indicating to return the Decision Function Value. If set to True, it will encapsulate the original result asked by the keyword resultType and the DFV into a tuple:
                ((<orange.Value 'Act'='3.44158792'>, <3.442: 1.000>), 0.34443)
                (<orange.Value 'Act'='3.44158792'>, 0.34443) 
                (<0.000, 0.000>, 0.34443)
                If it is not a binary classifier, DFV will be equal to None
                DFV will be a value from -0.5 to 0.5
        """
        if origExample == None:
            return self.classifier(None, resultType)
        else:
##scPA
            # Remove Meta attributes from example
            #dataUtilities.rmAllMeta(example)
            if len(origExample.domain.getmetas()) == 0:
                example = origExample
            else:
                example = dataUtilities.getCopyWithoutMeta(origExample)

            if not self.ExFix.ready:
                self.ExFix.set_domain(self.domain)
                self.ExFix.set_examplesFixedLog(self.examplesFixedLog)
            inExample = self.ExFix.fixExample(example) 
            if inExample: ##only procceds if the example was fixed or already ok, i.e.   inExample !=  None
##ecPA          
                ##scPA
                if  self.useBuiltInMissValHandling:
                    #compute the missing _mask
                    (exampleCvMat, missing_mask) = dataUtilities.Example2CvMat(inExample,self.varNames,self.thisVer,True) 
                else:
                    missing_mask = None
                    if self.imputer:
                         examplesImp = self.imputer(inExample)
                         # There is a problem with using the imputer when examples contain meta attributes.
                         # Unable to remove meta attributes from the examples. OK to rm meta from ExampleTables, but not from Example objects.
                         if not examplesImp:
                             if self.verbose > 0: print "Unable to predict with the RF model."
                             if self.verbose > 0: print "Perhaps you need to remove meta attributes from your examples."
                             return None
                    else:
                         examplesImp = inExample
                    ##ecPA
                    # Remove the response variable from the example to be predicted and transfrom the example to a tab sep string 
                    exampleCvMat = dataUtilities.Example2CvMat(examplesImp,self.varNames,self.thisVer)
                    del examplesImp
                if not exampleCvMat:
                    if self.verbose > 0: print "Could not convert the example to a valid CvMat objct for prediction"
                    return none
                # Predict using the RFmodel object
                prediction = self.classifier.predict(exampleCvMat,missing_mask)
	        probabilities = None
                DFV = None
                # Back transform the prediction to the original classes and calc probabilities
                prediction = dataUtilities.CvMat2orangeResponse(prediction, self.classVar)
                # Calculate artificial probabilities - not returned by the OpenCV RF algorithm
                if self.classVar.varType == orange.VarTypes.Discrete:
                    if resultType != orange.GetValue:
                        if len(self.classVar.values) == 2:
                            probOf1 = self.classifier.predict_prob(exampleCvMat,missing_mask)
                            probabilities = self.__getProbabilities(probOf1)
                            DFV = self.convert2DFV(probOf1)
                            self._isRealProb = True 
                        else:
                            #Need to make sure to return meanful probabilities to the cases where opencvRF does not support probabilities
                            # to be compatible with possible callers asking for probabilities. 
                            probabilities = self.__generateProbabilities(prediction)
                            self._isRealProb = False
                    elif len(self.classVar.values) == 2 and returnDFV:
                        DFV = self.convert2DFV(self.classifier.predict_prob(exampleCvMat,missing_mask))
                else:
                    #On Regression models assume the DVF as the value predicted
                    if not prediction.isSpecial():
                        DFV = float(prediction.value)
                        self._updateDFVExtremes(DFV)
                    y_hat = self.classVar(prediction)
                    probabilities = Orange.statistics.distribution.Continuous(self.classVar)
                    probabilities[y_hat] = 1.0
                del exampleCvMat
                del inExample
            else:
                if self.verbose > 0:
                    print "No prediction made for example:"
                    print example
                    print "The example does not have the same variables as the model."
                prediction = None
                probabilities = None
                DFV = None
	    ##scPA
	    del example
	    ##ecPA
            if resultType == orange.GetBoth:
                if prediction: 
                    orangePrediction = orange.Value(self.classVar, prediction)
                else: 
                    orangePrediction = None
                res = orangePrediction, probabilities
            elif resultType == orange.GetProbabilities:
                res = probabilities
            else: 
                if prediction: 
                    orangePrediction = orange.Value(self.classVar, prediction)
                else: 
                    orangePrediction = None
                res = orangePrediction 

            self.nPredictions += 1
            if returnDFV:
                return (res,DFV)
            else:
                return res
Ejemplo n.º 5
0
    def _singlePredict(self,
                       origExamples=None,
                       resultType=orange.GetValue,
                       returnDFV=False):
        res = None
        """
        orange.GetBoth -          <type 'tuple'>                     ->    (<orange.Value 'Act'='3.44158792'>, <3.442: 1.000>)
        orange.GetValue -         <type 'orange.Value'>              ->    <orange.Value 'Act'='3.44158792'>
        orange.GetProbabilities - <type 'orange.DiscDistribution'>   ->    <0.000, 0.000> 
        """
        #dataUtilities.rmAllMeta(examples)
        if len(origExamples.domain.getmetas()) == 0:
            examples = origExamples
        else:
            examples = dataUtilities.getCopyWithoutMeta(origExamples)
        #Check if the examples are compatible with the classifier (attributes order and varType compatibility)
        dataUtilities.verbose = self.verbose
        if not self.ExFix.ready:
            self.ExFix.set_domain(self.imputer.defaults.domain)
            self.ExFix.set_examplesFixedLog(self.examplesFixedLog)
        inExamples = self.ExFix.fixExample(examples)

        if not inExamples:
            return None

        #Imput the examples if there are missing values
        examplesImp = self.imputer(inExamples)
        # There is a problem with using the imputer when examples contain meta attributes.
        # Unable to remove meta attributes from the examples. OK to rm meta from ExampleTables, but not from Example objects.
        if not examplesImp:
            if self.verbose > 0:
                print "Unable to predict with the Bayes model."
            if self.verbose > 0:
                print "Perhaps you need to remove meta attributes from your examples."
            return None

        if self.scalizer:
            ex = self.scalizer.scaleEx(examplesImp)
        else:
            ex = examplesImp
        out = self.classifier.predict(
            dataUtilities.Example2CvMat(ex, self.varNames))
        #print "OUT:",out
        probabilities = None
        DFV = None
        # Back transform the prediction to the original classes and calc probabilities
        prediction = dataUtilities.CvMat2orangeResponse(out, self.classVar)
        #print "Prediction:",prediction
        # Calculate artificial probabilities - not returned by the OpenCV RF algorithm
        if self.classVar.varType == orange.VarTypes.Discrete:
            if resultType != orange.GetValue:
                #Need to make sure to return meanful probabilities to the cases where opencvRF does not support probabilities
                # to be compatible with possible callers asking for probabilities.
                probabilities = self.__generateProbabilities(prediction)
                self._isRealProb = False
        else:
            #On Regression models assume the DVF as the value predicted
            DFV = prediction
            self._updateDFVExtremes(DFV)

        if resultType == orange.GetBoth:
            if prediction:
                orangePrediction = orange.Value(self.classVar, prediction)
            else:
                orangePrediction = None
            res = orangePrediction, probabilities
        elif resultType == orange.GetProbabilities:
            res = probabilities
        else:
            if prediction:
                orangePrediction = orange.Value(self.classVar, prediction)
            else:
                orangePrediction = None
            res = orangePrediction

        self.nPredictions += 1
        if returnDFV:
            return (res, DFV)
        else:
            return res