def CvBayesread(path, verbose=0): '''Read a CvBayes classifier from disk and return as a CvBayesClassifier instance. ''' NTrainEx = 0 basicStat = None thePath = str(path) try: if not os.path.isdir(thePath): if verbose > 0: print "ERROR: no such path: ", path return None if not os.path.isfile(str(os.path.join( thePath, "ImputeData.tab"))) or not os.path.isfile( str(os.path.join(thePath, "model.bayes"))): if verbose > 0: print "ERROR: Missing saved model/data files in: ", path return None impData = dataUtilities.DataTable( str(os.path.join(thePath, "ImputeData.tab")), createNewOn=orange.Variable.MakeStatus.OK) loadedbayes = ml.CvNormalBayesClassifier() loadedbayes.load(os.path.join(thePath, "model.bayes")) # Load Scaling Values if os.path.isdir(str(os.path.join(thePath, "scalingValues"))): scalizer = dataUtilities.scalizer( file=str(os.path.join(thePath, "scalingValues"))) else: scalizer = None #Load the var names oredered the way it was used when training varNamesFile = open(os.path.join(thePath, "varNames.txt"), "r") lines = varNamesFile.readlines() varNames = eval(lines[0].strip()) if len(lines) >= 3: NTrainEx = eval(lines[1].strip()) basicStat = eval(lines[2].strip()) varNamesFile.close() # Read the parameters if os.path.isfile(os.path.join(thePath, "parameters.pkl")): fileh = open(os.path.join(thePath, "parameters.pkl"), "r") parameters = pickle.load(fileh) fileh.close() else: parameters = {} return CvBayesClassifier(classifier=loadedbayes, imputeData=impData[0], classVar=impData.domain.classVar, verbose=verbose, loadedModel=True, varNames=varNames, NTrainEx=NTrainEx, basicStat=basicStat, scalizer=scalizer, parameters=parameters) except: if verbose > 0: print "ERROR: Could not read model from ", path
def __call__(self, data, weight=None): """Creates a Bayes model from the data in origTrainingData. """ if not AZBaseClasses.AZLearner.__call__(self, data, weight): return None if data.domain.classVar.varType != orange.VarTypes.Discrete: raise Exception( "AZorngCvBayes can only be used for classification.") #Remove from the domain any unused values of discrete attributes including class data = dataUtilities.getDataWithoutUnusedValues(data, True) #dataUtilities.rmAllMeta(data) if len(data.domain.getmetas()) == 0: trainingData = data else: trainingData = dataUtilities.getCopyWithoutMeta(data) # Create the imputer self.imputer = orange.ImputerConstructor_average(trainingData) # Impute the data trainingData = self.imputer(trainingData) if self.scale: self.scalizer = dataUtilities.scalizer() self.scalizer.scaleClass = False self.scalizer.nMin = -1 self.scalizer.nMax = 1 self.trainData = self.scalizer.scaleAndContinuizeData(trainingData) else: self.trainData = trainingData self.scalizer = None impData = self.imputer.defaults #Convert the ExampleTable to CvMat CvMatrices = dataUtilities.ExampleTable2CvMat(self.trainData) mat = CvMatrices["matrix"] responses = CvMatrices["responses"] varTypes = CvMatrices["varTypes"] missingDataMask = CvMatrices["missing_data_mask"] #Create the model it MUST be created with the NON DEFAULT constructor or must call create classifier = ml.CvNormalBayesClassifier() classifier.clear() #Train the model #CvNormalBayesClassifier::train(const CvMat* _train_data, const CvMat* _responses, const CvMat* _var_idx =0, const CvMat* _sample_idx=0, bool update=false) classifier.train(mat, responses, None, None, False) return CvBayesClassifier(classifier=classifier, classVar=trainingData.domain.classVar, imputeData=impData, verbose=self.verbose, varNames=CvMatrices["varNames"], nIter=None, basicStat=self.basicStat, NTrainEx=len(trainingData), scalizer=self.scalizer, parameters=self.parameters)
def __call__(self, data, weight=None): """Creates a Bayes model from the data in origTrainingData. """ if not AZBaseClasses.AZLearner.__call__(self, data, weight): return None if data.domain.classVar.varType != orange.VarTypes.Discrete: raise Exception("AZorngCvBayes can only be used for classification.") # Remove from the domain any unused values of discrete attributes including class data = dataUtilities.getDataWithoutUnusedValues(data, True) # dataUtilities.rmAllMeta(data) if len(data.domain.getmetas()) == 0: trainingData = data else: trainingData = dataUtilities.getCopyWithoutMeta(data) # Create the imputer self.imputer = orange.ImputerConstructor_average(trainingData) # Impute the data trainingData = self.imputer(trainingData) if self.scale: self.scalizer = dataUtilities.scalizer() self.scalizer.scaleClass = False self.scalizer.nMin = -1 self.scalizer.nMax = 1 self.trainData = self.scalizer.scaleAndContinuizeData(trainingData) else: self.trainData = trainingData self.scalizer = None impData = self.imputer.defaults # Convert the ExampleTable to CvMat CvMatrices = dataUtilities.ExampleTable2CvMat(self.trainData) mat = CvMatrices["matrix"] responses = CvMatrices["responses"] varTypes = CvMatrices["varTypes"] missingDataMask = CvMatrices["missing_data_mask"] # Create the model it MUST be created with the NON DEFAULT constructor or must call create classifier = ml.CvNormalBayesClassifier() classifier.clear() # Train the model # CvNormalBayesClassifier::train(const CvMat* _train_data, const CvMat* _responses, const CvMat* _var_idx =0, const CvMat* _sample_idx=0, bool update=false) classifier.train(mat, responses, None, None, False) return CvBayesClassifier( classifier=classifier, classVar=trainingData.domain.classVar, imputeData=impData, verbose=self.verbose, varNames=CvMatrices["varNames"], nIter=None, basicStat=self.basicStat, NTrainEx=len(trainingData), scalizer=self.scalizer, )
def CvBayesread(path, verbose=0): """Read a CvBayes classifier from disk and return as a CvBayesClassifier instance. """ NTrainEx = 0 basicStat = None thePath = str(path) try: if not os.path.isdir(thePath): if verbose > 0: print "ERROR: no such path: ", path return None if not os.path.isfile(str(os.path.join(thePath, "ImputeData.tab"))) or not os.path.isfile( str(os.path.join(thePath, "model.bayes")) ): if verbose > 0: print "ERROR: Missing saved model/data files in: ", path return None impData = dataUtilities.DataTable( str(os.path.join(thePath, "ImputeData.tab")), createNewOn=orange.Variable.MakeStatus.OK ) loadedbayes = ml.CvNormalBayesClassifier() loadedbayes.load(os.path.join(thePath, "model.bayes")) # Load Scaling Values if os.path.isdir(str(os.path.join(thePath, "scalingValues"))): scalizer = dataUtilities.scalizer(file=str(os.path.join(thePath, "scalingValues"))) else: scalizer = None # Load the var names oredered the way it was used when training varNamesFile = open(os.path.join(thePath, "varNames.txt"), "r") lines = varNamesFile.readlines() varNames = eval(lines[0].strip()) if len(lines) >= 3: NTrainEx = eval(lines[1].strip()) basicStat = eval(lines[2].strip()) varNamesFile.close() return CvBayesClassifier( classifier=loadedbayes, imputeData=impData[0], classVar=impData.domain.classVar, verbose=verbose, loadedModel=True, varNames=varNames, NTrainEx=NTrainEx, basicStat=basicStat, scalizer=scalizer, ) except: if verbose > 0: print "ERROR: Could not read model from ", path
def CvSVMread(path, verbose = 0): '''Read a PLS classifier from disk and return as a PLSClassifier instance. ''' thePath = str(path) NTrainEx = 0 basicStat = None try: if not os.path.isdir(thePath): if verbose > 0: print "ERROR: no such path: ", path return None if not os.path.isfile(str(os.path.join(thePath,"ImputeData.tab"))) or not os.path.isfile(str(os.path.join(thePath,"model.svm"))): if verbose > 0: print "ERROR: Missing saved model/data files in: ", path return None if os.path.isdir(str(os.path.join(thePath,"scalingValues"))): scalizer = dataUtilities.scalizer(file=str(os.path.join(thePath,"scalingValues"))) else: scalizer = None impData = dataUtilities.DataTable(str(os.path.join(thePath,"ImputeData.tab")),createNewOn=orange.Variable.MakeStatus.OK) loadedsvm = ml.CvSVM() loadedsvm.load(os.path.join(thePath,"model.svm")) #Load the var names oredered the way it was used when training if (os.path.isfile(os.path.join(thePath,"varNames.txt"))): varNamesFile = open(os.path.join(thePath,"varNames.txt"),"r") lines = varNamesFile.readlines() varNames = eval(lines[0].strip()) if len(lines) >= 3: NTrainEx = eval(lines[1].strip()) basicStat = eval(lines[2].strip()) varNamesFile.close() #thisVer = True else: if verbose > 0: print "WARNING: The model loaded was probably saved with azorange version 0.2.1 or lower" varNames = [attr.name for attr in impData.domain.attributes] #thisVer = False # Read the parameters if os.path.isfile(os.path.join(thePath,"parameters.pkl")): fileh = open(os.path.join(thePath,"parameters.pkl"),"r") parameters = pickle.load(fileh) fileh.close() else: parameters = {} return CvSVMClassifier(classifier = loadedsvm, scalizer = scalizer,imputeData=impData[0], classVar = impData.domain.classVar, verbose = verbose, loadedModel = True, varNames = varNames, NTrainEx = NTrainEx, basicStat = basicStat, parameters = parameters) except: if verbose > 0: print "ERROR: Could not read model from ", path
def __call__(self, data, weight = None): """Creates an SVM model from the data in origTrainingData. """ if not AZBaseClasses.AZLearner.__call__(self, data, weight): if self.verbose > 0: print "Could not create base class instance" return None dataUtilities.verbose = self.verbose #Remove from the domain any unused values of discrete attributes including class data = dataUtilities.getDataWithoutUnusedValues(data,True) #dataUtilities.rmAllMeta(data) if len(data.domain.getmetas()) == 0: trainingData = data else: trainingData = dataUtilities.getCopyWithoutMeta(data) # Create the imputer self.imputer = orange.ImputerConstructor_average(trainingData) # Impute the data trainingData = self.imputer(trainingData) if self.scaleData: self.scalizer = dataUtilities.scalizer() for attr in ("nMin","nMax","nClassMin","nClassMax"): setattr(self.scalizer, attr, getattr(self, attr)) #Only scale the class in regression. On classification, set scaleClass to False self.scalizer.scaleClass = self.scaleClass and trainingData.domain.classVar.varType == orange.VarTypes.Continuous or False self.scalizer.nClassMin = self.nClassMin self.scalizer.nClassMax = self.nClassMax self.trainData = self.scalizer.scaleAndContinuizeData(trainingData) else: self.trainData = trainingData self.scalizer = None impData=self.imputer.defaults #Adjust the svm type according to the problem (regression or classification) if self.svm_type != 102: if trainingData.domain.classVar.varType == orange.VarTypes.Continuous: if self.svm_type in (100,101): self.svm_type += 3 self.eps = self.epsR #Regression eps else: if self.svm_type in (103,104): self.svm_type -= 3 self.eps = self.epsC #Classification eps #Convert the ExampleTable to CvMat CvMatices = dataUtilities.ExampleTable2CvMat(self.trainData) mat = CvMatices["matrix"] responses = CvMatices["responses"] varTypes = CvMatices["varTypes"] #Configure SVM self.params self.params = ml.CvSVMParams() self.params.svm_type = self.svm_type self.params.kernel_type = self.kernel_type self.params.degree = self.degree self.params.gamma = self.gamma self.params.coef0 = self.coef0 self.params.C = self.C self.params.nu = self.nu self.params.p = self.p #Process the priors from a str, list or dict to a valid list priors = self.convertPriors(self.priors,trainingData.domain.classVar) if type(priors) == str: #If a string is returned, there was a failure, and it is the respective error mnessage. print priors return None if priors and self.params.svm_type != ml.CvSVM.C_SVC: priors = None if self.verbose > 0: print "WARNING: The priors will not have any effect. They can only be used with C_SVC SVM-Type." elif priors: priors = dataUtilities. List2CvMat(priors) self.params.class_weights = priors term_crit = cv.CvTermCriteria() term_crit.type = self.stopCrit #cv.CV_TERMCRIT_EPS # or CV_TERMCRIT_ITER term_crit.epsilon = self.eps #Or use: term_crit.max_iter = x term_crit.max_iter = self.maxIter #Or use: term_crit.max_iter = x self.params.term_crit = term_crit #Create the model classifier = ml.CvSVM() #Train the model #train(trainData, responses, varIdx, SampleIdx, Params) classifier.train(mat,responses,None,None,self.params) if classifier.get_support_vector_count() < 1: print "WARNING: The number of support vectors is 0." print "This could be becasue the margin between the hyper plane and the support vectors has become zero." print "Try to modify the parameters controlling the margin. " print "For example decrease C or p(regression only)." print "No SVM model returned!" return None else: return CvSVMClassifier(classifier = classifier, classVar = data.domain.classVar, scalizer = self.scalizer, imputeData=impData, verbose = self.verbose, varNames = CvMatices["varNames"], basicStat = self.basicStat, NTrainEx = len(trainingData))
def __call__(self, data, weight = None): """Creates an SVM model from the data in origTrainingData. """ if not AZBaseClasses.AZLearner.__call__(self, data, weight): if self.verbose > 0: print "Could not create base class instance" return None dataUtilities.verbose = self.verbose #Remove from the domain any unused values of discrete attributes including class data = dataUtilities.getDataWithoutUnusedValues(data,True) #dataUtilities.rmAllMeta(data) if len(data.domain.getmetas()) == 0: trainingData = data else: trainingData = dataUtilities.getCopyWithoutMeta(data) # Create the imputer self.imputer = orange.ImputerConstructor_average(trainingData) # Impute the data trainingData = self.imputer(trainingData) if self.scaleData: self.scalizer = dataUtilities.scalizer() for attr in ("nMin","nMax","nClassMin","nClassMax"): setattr(self.scalizer, attr, getattr(self, attr)) #Only scale the class in regression. On classification, set scaleClass to False self.scalizer.scaleClass = self.scaleClass and trainingData.domain.classVar.varType == orange.VarTypes.Continuous or False self.scalizer.nClassMin = self.nClassMin self.scalizer.nClassMax = self.nClassMax self.trainData = self.scalizer.scaleAndContinuizeData(trainingData) else: self.trainData = trainingData self.scalizer = None impData=self.imputer.defaults #Adjust the svm type according to the problem (regression or classification) if self.svm_type != 102: if trainingData.domain.classVar.varType == orange.VarTypes.Continuous: if self.svm_type in (100,101): self.svm_type += 3 self.eps = self.epsR #Regression eps else: if self.svm_type in (103,104): self.svm_type -= 3 self.eps = self.epsC #Classification eps #Convert the ExampleTable to CvMat CvMatices = dataUtilities.ExampleTable2CvMat(self.trainData) mat = CvMatices["matrix"] responses = CvMatices["responses"] varTypes = CvMatices["varTypes"] #Configure SVM self.params self.params = ml.CvSVMParams() self.params.svm_type = self.svm_type self.params.kernel_type = self.kernel_type self.params.degree = self.degree self.params.gamma = self.gamma self.params.coef0 = self.coef0 self.params.C = self.C self.params.nu = self.nu self.params.p = self.p #Process the priors from a str, list or dict to a valid list priors = self.convertPriors(self.priors,trainingData.domain.classVar) if type(priors) == str: #If a string is returned, there was a failure, and it is the respective error mnessage. print priors return None if priors and self.params.svm_type != ml.CvSVM.C_SVC: priors = None if self.verbose > 0: print "WARNING: The priors will not have any effect. They can only be used with C_SVC SVM-Type." elif priors: priors = dataUtilities. List2CvMat(priors) self.params.class_weights = priors term_crit = cv.CvTermCriteria() term_crit.type = self.stopCrit #cv.CV_TERMCRIT_EPS # or CV_TERMCRIT_ITER term_crit.epsilon = self.eps #Or use: term_crit.max_iter = x term_crit.max_iter = self.maxIter #Or use: term_crit.max_iter = x self.params.term_crit = term_crit #Create the model classifier = ml.CvSVM() #Train the model #train(trainData, responses, varIdx, SampleIdx, Params) classifier.train(mat,responses,None,None,self.params) if classifier.get_support_vector_count() < 1: print "WARNING: The number of support vectors is 0." print "This could be becasue the margin between the hyper plane and the support vectors has become zero." print "Try to modify the parameters controlling the margin. " print "For example decrease C or p(regression only)." print "No SVM model returned!" return None else: return CvSVMClassifier(classifier = classifier, classVar = data.domain.classVar, scalizer = self.scalizer, imputeData=impData, verbose = self.verbose, varNames = CvMatices["varNames"], basicStat = self.basicStat, NTrainEx = len(trainingData), parameters = self.parameters)