Anonymous avatar Anonymous committed 348a668

Adding fix for a Labels-related bug where if you provide a classLabels argument to Labels, an exception occurs.

Comments (0)

Files changed (1)

PyML/containers/labels.py

    	A class that holds the labels of a dataset.
 
     Attributes::
-    
+
       L - labels provided by the user (strings)
       Y - internal label representation - an integer from 0 to numClasses - 1
       for multilabel problems each pattern has a list of integer labels
       numClasses - the number of classes in the data
     """
 
-    def __init__(self, arg = None, **args) : 
+    def __init__(self, arg = None, **args) :
 
         """
         :Parameters:
           - `positiveClass` - for a two class problem, the identity of the
             positive class.  If the labels are '+1' and '-1' or  '1' and '-1',
             the positive class is detected automatically.
-          - `classLabels` - 
+          - `classLabels` -
           - `forgetClassLabels` - when using copy construction the default behavior
             is to return a Labels object that remembers the set of classes that the
             original object had, even if some classes are no longer represented.
             self.positiveClass = args['positiveClass']
         if 'classLabels' in args :
             self.classLabels = args['classLabels']
+            self.numClasses = len(self.classLabels)
 
         if type(arg) == type('') :
             args['hint'] = 'csv'
 
     def extend(self, other, patterns = None) :
         """add to a dataset a list of patterns from another dataset"""
-        
+
         if patterns is None :
             patterns = range(len(other))
         # retrieve labels from other
                 self.patternID.append(other.patternID[p])
         if self.L is not None :
             self.processLabels(self.L)
-            
-        
+
+
     def processLabels(self, L, **args) :
 
         forgetClassLabels = False
             return False
         else :
             return True
-    
+
     def save(self, fileName, delim = '\t') :
 
         fileHandle = open(fileName, 'w')
         for classLabel in classList :
             for p in self.classes[self.classDict[classLabel]] :
                 self.L[p] = newLabel
-        
+
         self.processLabels(self.L, forgetClassLabels = True)
 
     def oneAgainstRest(self, classLabels, className = None) :
     data.attachLabels(data.labels)
 
     return data
-        
-    
+
+
 def oneAgainstRest(data, classLabels, className = None) :
 
     """
     creates a one-against-the-rest dataset/labels object
 
     :Parameters:
-    
+
       - `data` - a dataset
       - `classLabels` - a single class name, or a list of class names (string
         or a list of strings)
       - `className` - if given, the new name given to the class
-      
+
     Return value::
-    
+
       returns a dataset object where all class labels that are different
       from the given class label/s are converted to a single class
       """
-	
+
     data.labels.oneAgainstRest(classLabels, className)
     data.attachLabels(data.labels)
 
 def eliminateSmallClasses(data, size) :
     """returns a dataset that contains the classes of d that contain
     at least size patterns"""
-    
+
     patterns = []
     for i in range(len(data)) :
         if data.labels.classSize[data.labels.Y[i]] >= size :
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.