def buildClassifier(self, instances): """ builds the ZeroR classifier with the given data Parameter(s): 'instances' -- the data to build the classifier from """ self.getCapabilities().testWithFail(instances) # remove instances with missing class instances = Instances(instances) instances.deleteWithMissingClass() sumOfWeights = 0 self.__Class = instances.classAttribute() self.__ClassValue = 0 self.__Counts = None if (instances.classAttribute().isNumeric()): self.__Counts = None elif (instances.classAttribute().isNominal()): self.__Counts = jarray.zeros(instances.numClasses(), 'd') for i in range(len(self.__Counts)): self.__Counts[i] = 1 sumOfWeights = instances.numClasses() enu = instances.enumerateInstances() while (enu.hasMoreElements()): instance = enu.nextElement() if (not instance.classIsMissing()): if (instances.classAttribute().isNominal()): self.__Counts[int( instance.classValue())] += instance.weight() else: self.__ClassValue += instance.weight( ) * instance.classValue() sumOfWeights += instance.weight() if (instances.classAttribute().isNumeric()): if (Utils.gr(sumOfWeights, 0)): self.__ClassValue /= sumOfWeights else: self.__ClassValue = Utils.maxIndex(self.__Counts) Utils.normalize(self.__Counts, sumOfWeights) return
def buildClassifier(self, instances): """ builds the ZeroR classifier with the given data Parameter(s): 'instances' -- the data to build the classifier from """ self.getCapabilities().testWithFail(instances) # remove instances with missing class instances = Instances(instances) instances.deleteWithMissingClass() sumOfWeights = 0 self.__Class = instances.classAttribute() self.__ClassValue = 0 self.__Counts = None if (instances.classAttribute().isNumeric()): self.__Counts = None elif (instances.classAttribute().isNominal()): self.__Counts = jarray.zeros(instances.numClasses(), 'd') for i in range(len(self.__Counts)): self.__Counts[i] = 1 sumOfWeights = instances.numClasses() enu = instances.enumerateInstances() while (enu.hasMoreElements()): instance = enu.nextElement() if (not instance.classIsMissing()): if (instances.classAttribute().isNominal()): self.__Counts[int(instance.classValue())] += instance.weight() else: self.__ClassValue += instance.weight() * instance.classValue() sumOfWeights += instance.weight() if (instances.classAttribute().isNumeric()): if (Utils.gr(sumOfWeights, 0)): self.__ClassValue /= sumOfWeights else: self.__ClassValue = Utils.maxIndex(self.__Counts) Utils.normalize(self.__Counts, sumOfWeights) return
def load_model(self, model, arff): try: j48 = J48() options = ('-i', '-l', model, '-T', arff) objectInputFileName = Utils.getOption('l', options); if len(objectInputFileName) == 0: print "No training file and no object input file given." raise inputStream = InputStream inputStream = FileInputStream(objectInputFileName) objectInputStream = ObjectInputStream(inputStream) j48 = objectInputStream.readObject() return j48 except: raise
class JeroR(AbstractClassifier, JythonSerializableObject): """ JeroR is a Jython implementation of the Weka classifier ZeroR 'author' -- FracPete (fracpete at waikato dot ac dot nz) 'version' -- $Revision: 10229 $ """ # the documentation can be generated with HappyDoc: # http://happydoc.sourceforge.net/ # Example command: # happydoc --title Weka -d ./doc ./src # the chosen class value __ClassValue = Utils.missingValue() # the class attribute __Class = None # the counts for each class label __Counts = None def listOptions(self): """ Returns an enumeration describing the available options. Return: an enumeration of all the available options. """ return AbstractClassifier.listOptions(self) def setOptions(self, options): """ Parses a given list of options. Parameter(s): 'options' -- the list of options as an array of strings """ AbstractClassifier.setOptions(self, options) return def getOptions(self): """ Gets the current settings of the Classifier as string array. Return: an array of strings suitable for passing to setOptions """ return AbstractClassifier.getOptions(self) def getCapabilities(self): """ returns the capabilities of this classifier Return: the capabilities of this classifier """ result = AbstractClassifier.getCapabilities(self) # attributes result.enable(Capability.NOMINAL_ATTRIBUTES) result.enable(Capability.NUMERIC_ATTRIBUTES) result.enable(Capability.DATE_ATTRIBUTES) result.enable(Capability.STRING_ATTRIBUTES) result.enable(Capability.RELATIONAL_ATTRIBUTES) result.enable(Capability.MISSING_VALUES) # class result.enable(Capability.NOMINAL_CLASS) result.enable(Capability.NUMERIC_CLASS) result.enable(Capability.DATE_CLASS) result.enable(Capability.MISSING_CLASS_VALUES) # instances result.setMinimumNumberInstances(0) return result def buildClassifier(self, instances): """ builds the ZeroR classifier with the given data Parameter(s): 'instances' -- the data to build the classifier from """ self.getCapabilities().testWithFail(instances) # remove instances with missing class instances = Instances(instances) instances.deleteWithMissingClass() sumOfWeights = 0 self.__Class = instances.classAttribute() self.__ClassValue = 0 self.__Counts = None if (instances.classAttribute().isNumeric()): self.__Counts = None elif (instances.classAttribute().isNominal()): self.__Counts = jarray.zeros(instances.numClasses(), 'd') for i in range(len(self.__Counts)): self.__Counts[i] = 1 sumOfWeights = instances.numClasses() enu = instances.enumerateInstances() while (enu.hasMoreElements()): instance = enu.nextElement() if (not instance.classIsMissing()): if (instances.classAttribute().isNominal()): self.__Counts[int( instance.classValue())] += instance.weight() else: self.__ClassValue += instance.weight( ) * instance.classValue() sumOfWeights += instance.weight() if (instances.classAttribute().isNumeric()): if (Utils.gr(sumOfWeights, 0)): self.__ClassValue /= sumOfWeights else: self.__ClassValue = Utils.maxIndex(self.__Counts) Utils.normalize(self.__Counts, sumOfWeights) return def classifyInstance(self, instance): """ returns the prediction for the given instance Parameter(s): 'instance' -- the instance to predict the class value for Return: the prediction for the given instance """ return self.__ClassValue def distributionForInstance(self, instance): """ returns the class distribution for the given instance Parameter(s): 'instance' -- the instance to calculate the class distribution for Return: the class distribution for the given instance """ result = None if (self.__Counts == None): result = jarray.zeros(1, 'd') result[0] = self.__ClassValue else: result = self.__Counts[:] return result def toString(self): """ Prints a string representation of the classifier Return: string representation of the classifier """ if (self.__Class == None): return "JeroR: No model built yet." if (self.__Counts == None): return "JeroR predicts class value: " + str(self.__ClassValue) else: return "JeroR predicts class value: " + str( self.__Class.value(int(self.__ClassValue)))