private OrdinalAttribute getMajorityClassification ( String property , Iterable < State > examples ) throws LearningFailureException { /*log.fine("private OrdinalAttribute getMajorityClassification(String property, Collection<State> examples): called");*/ /*log.fine("property = " + property);*/ // Flag used to indicate that the map to hold the value counts in has been initialized. Map < OrdinalAttribute , Integer > countMap = null ; // Used to hold the biggest count found so far. int biggestCount = 0 ; // Used to hold the value with the biggest count found so far. OrdinalAttribute biggestAttribute = null ; // Loop over all the examples counting the number of occurences of each possible classification by the // named property. for ( State example : examples ) { OrdinalAttribute nextAttribute = ( OrdinalAttribute ) example . getProperty ( property ) ; /*log.fine("nextAttribute = " + nextAttribute);*/ // If this is the first attribute then find out how many possible values it can take on. if ( countMap == null ) { // A check has already been performed at the start of the learning method to ensure that the output // property only takes on a finite number of values. countMap = new HashMap < OrdinalAttribute , Integer > ( ) ; } int count ; // Increment the count for the number of occurences of this classification. if ( ! countMap . containsKey ( nextAttribute ) ) { count = 1 ; countMap . put ( nextAttribute , count ) ; } else { count = countMap . get ( nextAttribute ) ; countMap . put ( nextAttribute , count ++ ) ; } // Compare it to the biggest score found so far to see if it is bigger. if ( count > biggestCount ) { // Update the biggest score. biggestCount = count ; // Update the value of the majority classification. biggestAttribute = nextAttribute ; } } // Return the majority classification found. return biggestAttribute ; }
This helper method works out how the majority of the specified examples are classified by the named property . The property should always be the goal property that the algorithm is learning and must always take on a finite number of different values .