LeaveOneOutSplitter.java :  » Natural-Language-Processing » MinorThird » edu » cmu » minorthird » classify » experiments » Java Open Source

Java Open Source » Natural Language Processing » MinorThird 
MinorThird » edu » cmu » minorthird » classify » experiments » LeaveOneOutSplitter.java
package edu.cmu.minorthird.classify.experiments;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Random;
import java.util.Set;

import edu.cmu.minorthird.classify.HasSubpopulationId;
import edu.cmu.minorthird.classify.Splitter;

/** 
 * Do N-fold cross-validation, where N is the number of different
 * subpopulations.
 *
 * @author William Cohen
 */

public class LeaveOneOutSplitter<T> implements Splitter<T>{

  private Random random;

  private Splitter<T> crossValSplitter;
  
  public LeaveOneOutSplitter(Random random){
    this.random=random;
  }

  public LeaveOneOutSplitter(){
    this(new Random());
  }

  public void split(Iterator<T> i){
    List<T> buf=new ArrayList<T>();
    Set<String> subpops=new HashSet<String>();
    while(i.hasNext()){
      T t=i.next();
      buf.add(t);
      // find subpop id, and record it
      String id;
      if(t instanceof HasSubpopulationId){
        id=((HasSubpopulationId)t).getSubpopulationId();
      }
      else{
        id="youNeeekID#"+subpops.size();
      }
      subpops.add(id);
    }
    crossValSplitter=new CrossValSplitter<T>(random,subpops.size());
    crossValSplitter.split(buf.iterator());
  }

  public int getNumPartitions(){
    return crossValSplitter.getNumPartitions();
  }

  public Iterator<T> getTrain(int k){
    return crossValSplitter.getTrain(k);
  }

  public Iterator<T> getTest(int k){
    return crossValSplitter.getTest(k);
  }

  public String toString(){
    return "[LeaveOneOutSplitter]";
  }
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.