Java 类weka.core.Randomizable 实例源码

项目:meka    文件:BaggingMLdup.java   
@Override
public void buildClassifier(Instances train) throws Exception {
    testCapabilities(train);

    if (getDebug()) System.out.print("-: Models: ");

    //m_Classifiers = (MultilabelClassifier[]) AbstractClassifier.makeCopies(m_Classifier, m_NumIterations);
    m_Classifiers = MultilabelClassifier.makeCopies((MultilabelClassifier)m_Classifier, m_NumIterations);

    for(int i = 0; i < m_NumIterations; i++) {
        Random r = new Random(m_Seed+i);
        Instances bag = new Instances(train,0);
        if (m_Classifiers[i] instanceof Randomizable) ((Randomizable)m_Classifiers[i]).setSeed(m_Seed+i);
        if(getDebug()) System.out.print(""+i+" ");

        int bag_no = (m_BagSizePercent*train.numInstances()/100);
        //System.out.println(" bag no: "+bag_no);
        while(bag.numInstances() < bag_no) {
            bag.add(train.instance(r.nextInt(train.numInstances())));
        }
        m_Classifiers[i].buildClassifier(bag);
    }
    if (getDebug()) System.out.println(":-");
}
项目:meka    文件:EnsembleML.java   
@Override
public void buildClassifier(Instances train) throws Exception {
    testCapabilities(train);

    if (getDebug()) System.out.print("-: Models: ");

    train = new Instances(train);
    m_Classifiers = MultilabelClassifier.makeCopies((MultilabelClassifier)m_Classifier, m_NumIterations);
    int sub_size = (train.numInstances()*m_BagSizePercent/100);
    for(int i = 0; i < m_NumIterations; i++) {
        if(getDebug()) System.out.print(""+i+" ");
        if (m_Classifiers[i] instanceof Randomizable) ((Randomizable)m_Classifiers[i]).setSeed(i);
        train.randomize(new Random(m_Seed+i));
        Instances sub_train = new Instances(train,0,sub_size);
        m_Classifiers[i].buildClassifier(sub_train);
    }

    if (getDebug()) System.out.println(":-");
}
项目:jbossBA    文件:RandomCommittee.java   
/**
 * Builds the committee of randomizable classifiers.
 *
 * @param data the training data to be used for generating the
 * bagged classifier.
 * @exception Exception if the classifier could not be built successfully
 */
public void buildClassifier(Instances data) throws Exception {

  // can classifier handle the data?
  getCapabilities().testWithFail(data);

  // remove instances with missing class
  data = new Instances(data);
  data.deleteWithMissingClass();

  if (!(m_Classifier instanceof Randomizable)) {
    throw new IllegalArgumentException("Base learner must implement Randomizable!");
  }

  m_Classifiers = Classifier.makeCopies(m_Classifier, m_NumIterations);

  Random random = data.getRandomNumberGenerator(m_Seed);
  for (int j = 0; j < m_Classifiers.length; j++) {

    // Set the random number seed for the current classifier.
    ((Randomizable) m_Classifiers[j]).setSeed(random.nextInt());

    // Build the classifier.
    m_Classifiers[j].buildClassifier(data);
  }
}
项目:repo.kmeanspp.silhouette_score    文件:RandomCommittee.java   
/**
   * Builds the committee of randomizable classifiers.
   *
   * @param data the training data to be used for generating the
   * bagged classifier.
   * @exception Exception if the classifier could not be built successfully
   */
  public void buildClassifier(Instances data) throws Exception {

    // can classifier handle the data?
    getCapabilities().testWithFail(data);

    // get fresh instances
    m_data = new Instances(data);
    super.buildClassifier(m_data);

    if (!(m_Classifier instanceof Randomizable)) {
      throw new IllegalArgumentException("Base learner must implement Randomizable!");
    }

    m_Classifiers = AbstractClassifier.makeCopies(m_Classifier, m_NumIterations);

    Random random = m_data.getRandomNumberGenerator(m_Seed);

    // Resample data based on weights if base learner can't handle weights
    if (!(m_Classifier instanceof WeightedInstancesHandler)) {
      m_data = m_data.resampleWithWeights(random);
    }

    for (int j = 0; j < m_Classifiers.length; j++) {

      // Set the random number seed for the current classifier.
      ((Randomizable) m_Classifiers[j]).setSeed(random.nextInt());

      // Build the classifier.
//      m_Classifiers[j].buildClassifier(m_data);
    }

    buildClassifiers();

    // save memory
    m_data = null;
  }
项目:autoweka    文件:RandomCommittee.java   
/**
   * Builds the committee of randomizable classifiers.
   *
   * @param data the training data to be used for generating the
   * bagged classifier.
   * @exception Exception if the classifier could not be built successfully
   */
  public void buildClassifier(Instances data) throws Exception {

    // can classifier handle the data?
    getCapabilities().testWithFail(data);

    // remove instances with missing class
    m_data = new Instances(data);
    m_data.deleteWithMissingClass();
    super.buildClassifier(m_data);

    if (!(m_Classifier instanceof Randomizable)) {
      throw new IllegalArgumentException("Base learner must implement Randomizable!");
    }

    m_Classifiers = FilteredClassifier.makeCopies(this, m_NumIterations);

    Random random = m_data.getRandomNumberGenerator(m_Seed);
    for (int j = 0; j < m_Classifiers.length; j++) {

      // Set the random number seed for the current classifier.
      ((Randomizable) m_Classifiers[j]).setSeed(random.nextInt());

      // Build the classifier.
//      m_Classifiers[j].buildClassifier(m_data);
    }

    buildClassifiers();

    // save memory
    m_data = null;
  }
项目:umple    文件:RandomCommittee.java   
/**
   * Builds the committee of randomizable classifiers.
   *
   * @param data the training data to be used for generating the
   * bagged classifier.
   * @exception Exception if the classifier could not be built successfully
   */
  public void buildClassifier(Instances data) throws Exception {

    // can classifier handle the data?
    getCapabilities().testWithFail(data);

    // remove instances with missing class
    m_data = new Instances(data);
    m_data.deleteWithMissingClass();
    super.buildClassifier(m_data);

    if (!(m_Classifier instanceof Randomizable)) {
      throw new IllegalArgumentException("Base learner must implement Randomizable!");
    }

    m_Classifiers = AbstractClassifier.makeCopies(m_Classifier, m_NumIterations);

    Random random = m_data.getRandomNumberGenerator(m_Seed);

    // Resample data based on weights if base learner can't handle weights
    if (!(m_Classifier instanceof WeightedInstancesHandler)) {
      m_data = m_data.resampleWithWeights(random);
    }

    for (int j = 0; j < m_Classifiers.length; j++) {

      // Set the random number seed for the current classifier.
      ((Randomizable) m_Classifiers[j]).setSeed(random.nextInt());

      // Build the classifier.
//      m_Classifiers[j].buildClassifier(m_data);
    }

    buildClassifiers();

    // save memory
    m_data = null;
  }
项目:meka    文件:BaggingML.java   
@Override
public void buildClassifier(Instances train) throws Exception {
    testCapabilities(train);

    if (getDebug()) System.out.print("-: Models: ");

    train = new Instances(train);
    m_Classifiers = MultilabelClassifier.makeCopies((MultilabelClassifier)m_Classifier, m_NumIterations);

    for(int i = 0; i < m_NumIterations; i++) {
        Random r = new Random(m_Seed+i);
        Instances bag = new Instances(train,0);
        if (m_Classifiers[i] instanceof Randomizable) ((Randomizable)m_Classifiers[i]).setSeed(m_Seed+i);
        if(getDebug()) System.out.print(""+i+" ");

        int ixs[] = new int[train.numInstances()];
        for(int j = 0; j < ixs.length; j++) {
            ixs[r.nextInt(ixs.length)]++;
        }
        for(int j = 0; j < ixs.length; j++) {
            if (ixs[j] > 0) {
                Instance instance = train.instance(j);
                instance.setWeight(ixs[j]);
                bag.add(instance);
            }
        }

        m_Classifiers[i].buildClassifier(bag);
    }
    if (getDebug()) System.out.println(":-");
}
项目:jbossBA    文件:END.java   
/**
  * Builds the committee of randomizable classifiers.
  *
  * @param data the training data to be used for generating the
  * bagged classifier.
  * @throws Exception if the classifier could not be built successfully
  */
 public void buildClassifier(Instances data) throws Exception {

   // can classifier handle the data?
   getCapabilities().testWithFail(data);

   // remove instances with missing class
   data = new Instances(data);
   data.deleteWithMissingClass();

   if (!(m_Classifier instanceof weka.classifiers.meta.nestedDichotomies.ND) && 
!(m_Classifier instanceof weka.classifiers.meta.nestedDichotomies.ClassBalancedND) &&  
!(m_Classifier instanceof weka.classifiers.meta.nestedDichotomies.DataNearBalancedND)) {
     throw new IllegalArgumentException("END only works with ND, ClassBalancedND " +
                 "or DataNearBalancedND classifier");
   }

   m_hashtable = new Hashtable();

   m_Classifiers = Classifier.makeCopies(m_Classifier, m_NumIterations);

   Random random = data.getRandomNumberGenerator(m_Seed);
   for (int j = 0; j < m_Classifiers.length; j++) {

     // Set the random number seed for the current classifier.
     ((Randomizable) m_Classifiers[j]).setSeed(random.nextInt());

     // Set the hashtable
     if (m_Classifier instanceof weka.classifiers.meta.nestedDichotomies.ND) 
((weka.classifiers.meta.nestedDichotomies.ND)m_Classifiers[j]).setHashtable(m_hashtable);
     else if (m_Classifier instanceof weka.classifiers.meta.nestedDichotomies.ClassBalancedND) 
((weka.classifiers.meta.nestedDichotomies.ClassBalancedND)m_Classifiers[j]).setHashtable(m_hashtable);
     else if (m_Classifier instanceof weka.classifiers.meta.nestedDichotomies.DataNearBalancedND) 
((weka.classifiers.meta.nestedDichotomies.DataNearBalancedND)m_Classifiers[j]).
  setHashtable(m_hashtable);

     // Build the classifier.
     m_Classifiers[j].buildClassifier(data);
   }
 }
项目:repo.kmeanspp.silhouette_score    文件:RandomSubSpace.java   
/**
  * builds the classifier.
  *
  * @param data     the training data to be used for generating the
  *             classifier.
  * @throws Exception   if the classifier could not be built successfully
  */
 public void buildClassifier(Instances data) throws Exception {

   // can classifier handle the data?
   getCapabilities().testWithFail(data);

   // get fresh Instances object
   m_data = new Instances(data);

   // only class? -> build ZeroR model
   if (m_data.numAttributes() == 1) {
     System.err.println(
  "Cannot build model (only class attribute present in data!), "
  + "using ZeroR model instead!");
     m_ZeroR = new weka.classifiers.rules.ZeroR();
     m_ZeroR.buildClassifier(m_data);
     return;
   }
   else {
     m_ZeroR = null;
   }

   super.buildClassifier(data);

   Integer[] indices = new Integer[data.numAttributes()-1];
   int classIndex = data.classIndex();
   int offset = 0;
   for(int i = 0; i < indices.length+1; i++) {
     if (i != classIndex) {
indices[offset++] = i+1;
     }
   }
   int subSpaceSize = numberOfAttributes(indices.length, getSubSpaceSize());
   Random random = data.getRandomNumberGenerator(m_Seed);

   for (int j = 0; j < m_Classifiers.length; j++) {
     if (m_Classifier instanceof Randomizable) {
((Randomizable) m_Classifiers[j]).setSeed(random.nextInt());
     }
     FilteredClassifier fc = new FilteredClassifier();
     fc.setClassifier(m_Classifiers[j]);
     m_Classifiers[j] = fc;
     Remove rm = new Remove();
     rm.setOptions(new String[]{"-V", "-R", randomSubSpace(indices,subSpaceSize,classIndex+1,random)});
     fc.setFilter(rm);

     // build the classifier
     //m_Classifiers[j].buildClassifier(m_data);
   }

   buildClassifiers();

   // save memory
   m_data = null;
 }
项目:repo.kmeanspp.silhouette_score    文件:RandomizableFilteredClassifier.java   
/**
 * Build the classifier on the filtered data.
 *
 * @param data the training data
 * @throws Exception if the classifier could not be built successfully
 */
public void buildClassifier(Instances data) throws Exception {

  if (m_Classifier == null) {
    throw new Exception("No base classifiers have been set!");
  }

  if (!(m_Classifier instanceof Randomizable) &&
      !(m_Filter instanceof Randomizable)) {
    throw new Exception("Either the classifier or the filter must implement " +
                        "the Randomizable interface.");
  }

  getCapabilities().testWithFail(data);

  // get fresh instances object
  data = new Instances(data);

  if (data.numInstances() == 0) {
    throw new Exception("No training instances.");
  }

  try {

  // get a random number generator
  Random r = data.getRandomNumberGenerator(m_Seed);

  if (m_Filter instanceof Randomizable) {
    ((Randomizable)m_Filter).setSeed(r.nextInt());
  }

  m_Filter.setInputFormat(data);  // filter capabilities are checked here
  data = Filter.useFilter(data, m_Filter);

  // can classifier handle the data?
  getClassifier().getCapabilities().testWithFail(data);

  m_FilteredInstances = data.stringFreeStructure();

  if (m_Classifier instanceof Randomizable) {
    ((Randomizable)m_Classifier).setSeed(r.nextInt());
  }
  m_Classifier.buildClassifier(data);

  } catch (Exception e) {
    e.printStackTrace();
    System.exit(1);
  }
}
项目:autoweka    文件:RandomSubSpace.java   
/**
  * builds the classifier.
  *
  * @param data     the training data to be used for generating the
  *             classifier.
  * @throws Exception   if the classifier could not be built successfully
  */
 public void buildClassifier(Instances data) throws Exception {

   // can classifier handle the data?
   getCapabilities().testWithFail(data);

   // remove instances with missing class
   m_data = new Instances(data);
   m_data.deleteWithMissingClass();

   // only class? -> build ZeroR model
   if (m_data.numAttributes() == 1) {
     System.err.println(
  "Cannot build model (only class attribute present in data!), "
  + "using ZeroR model instead!");
     m_ZeroR = new weka.classifiers.rules.ZeroR();
     m_ZeroR.buildClassifier(m_data);
     return;
   }
   else {
     m_ZeroR = null;
   }

   super.buildClassifier(data);

   Integer[] indices = new Integer[data.numAttributes()-1];
   int classIndex = data.classIndex();
   int offset = 0;
   for(int i = 0; i < indices.length+1; i++) {
     if (i != classIndex) {
indices[offset++] = i+1;
     }
   }
   int subSpaceSize = numberOfAttributes(indices.length, getSubSpaceSize());
   Random random = data.getRandomNumberGenerator(m_Seed);

   for (int j = 0; j < m_Classifiers.length; j++) {
     if (m_Classifier instanceof Randomizable) {
((Randomizable) m_Classifiers[j]).setSeed(random.nextInt());
     }
     FilteredClassifier fc = new FilteredClassifier();
     fc.setClassifier(m_Classifiers[j]);
     m_Classifiers[j] = fc;
     Remove rm = new Remove();
     rm.setOptions(new String[]{"-V", "-R", randomSubSpace(indices,subSpaceSize,classIndex+1,random)});
     fc.setFilter(rm);

     // build the classifier
     //m_Classifiers[j].buildClassifier(m_data);
   }

   buildClassifiers();

   // save memory
   m_data = null;
 }
项目:umple    文件:RandomSubSpace.java   
/**
  * builds the classifier.
  *
  * @param data     the training data to be used for generating the
  *             classifier.
  * @throws Exception   if the classifier could not be built successfully
  */
 public void buildClassifier(Instances data) throws Exception {

   // can classifier handle the data?
   getCapabilities().testWithFail(data);

   // remove instances with missing class
   m_data = new Instances(data);
   m_data.deleteWithMissingClass();

   // only class? -> build ZeroR model
   if (m_data.numAttributes() == 1) {
     System.err.println(
  "Cannot build model (only class attribute present in data!), "
  + "using ZeroR model instead!");
     m_ZeroR = new weka.classifiers.rules.ZeroR();
     m_ZeroR.buildClassifier(m_data);
     return;
   }
   else {
     m_ZeroR = null;
   }

   super.buildClassifier(data);

   Integer[] indices = new Integer[data.numAttributes()-1];
   int classIndex = data.classIndex();
   int offset = 0;
   for(int i = 0; i < indices.length+1; i++) {
     if (i != classIndex) {
indices[offset++] = i+1;
     }
   }
   int subSpaceSize = numberOfAttributes(indices.length, getSubSpaceSize());
   Random random = data.getRandomNumberGenerator(m_Seed);

   for (int j = 0; j < m_Classifiers.length; j++) {
     if (m_Classifier instanceof Randomizable) {
((Randomizable) m_Classifiers[j]).setSeed(random.nextInt());
     }
     FilteredClassifier fc = new FilteredClassifier();
     fc.setClassifier(m_Classifiers[j]);
     m_Classifiers[j] = fc;
     Remove rm = new Remove();
     rm.setOptions(new String[]{"-V", "-R", randomSubSpace(indices,subSpaceSize,classIndex+1,random)});
     fc.setFilter(rm);

     // build the classifier
     //m_Classifiers[j].buildClassifier(m_data);
   }

   buildClassifiers();

   // save memory
   m_data = null;
 }
项目:umple    文件:RandomizableFilteredClassifier.java   
/**
 * Build the classifier on the filtered data.
 *
 * @param data the training data
 * @throws Exception if the classifier could not be built successfully
 */
public void buildClassifier(Instances data) throws Exception {

  if (m_Classifier == null) {
    throw new Exception("No base classifiers have been set!");
  }

  if (!(m_Classifier instanceof Randomizable) &&
      !(m_Filter instanceof Randomizable)) {
    throw new Exception("Either the classifier or the filter must implement " +
                        "the Randomizable interface.");
  }

  getCapabilities().testWithFail(data);

  // remove instances with missing class
  data = new Instances(data);
  data.deleteWithMissingClass();

  if (data.numInstances() == 0) {
    throw new Exception("Not enough training instances with class labels.");
  }

  try {

  // get a random number generator
  Random r = data.getRandomNumberGenerator(m_Seed);

  if (m_Filter instanceof Randomizable) {
    ((Randomizable)m_Filter).setSeed(r.nextInt());
  }

  m_Filter.setInputFormat(data);  // filter capabilities are checked here
  data = Filter.useFilter(data, m_Filter);

  // can classifier handle the data?
  getClassifier().getCapabilities().testWithFail(data);

  m_FilteredInstances = data.stringFreeStructure();

  if (m_Classifier instanceof Randomizable) {
    ((Randomizable)m_Classifier).setSeed(r.nextInt());
  }
  m_Classifier.buildClassifier(data);

  } catch (Exception e) {
    e.printStackTrace();
    System.exit(1);
  }
}
项目:meka    文件:RandomSubspaceML.java   
@Override
public void buildClassifier(Instances D) throws Exception {
    testCapabilities(D);

    m_InstancesTemplates = new Instances[m_NumIterations];
    m_InstanceTemplates = new Instance[m_NumIterations];

    if (getDebug()) System.out.println("-: Models: ");

    m_Classifiers = MultilabelClassifier.makeCopies((MultilabelClassifier)m_Classifier, m_NumIterations);

    Random r = new Random(m_Seed);

    int N_sub = (D.numInstances()*m_BagSizePercent/100);

    int L = D.classIndex();
    int d = D.numAttributes() - L;
    int d_new = d * m_AttSizePercent / 100;
    m_IndicesCut = new int[m_NumIterations][];

    for(int i = 0; i < m_NumIterations; i++) {

        // Downsize the instance space (exactly like in EnsembleML.java)

        if (getDebug()) 
            System.out.print("\t"+(i+1)+": ");
        D.randomize(r);
        Instances D_cut = new Instances(D,0,N_sub);
        if (getDebug()) 
            System.out.print("N="+D.numInstances()+" -> N'="+D_cut.numInstances()+", ");

        // Downsize attribute space

        D_cut.setClassIndex(-1);
        int indices_a[] = A.make_sequence(L,d+L);
        A.shuffle(indices_a,r);
        indices_a = Arrays.copyOfRange(indices_a,0,d-d_new);
        Arrays.sort(indices_a);
        m_IndicesCut[i] = A.invert(indices_a,D.numAttributes());
        D_cut = F.remove(D_cut,indices_a,false);
        D_cut.setClassIndex(L);
        if (getDebug()) 
            System.out.print(" A:="+(D.numAttributes() - L)+" -> A'="+(D_cut.numAttributes() - L)+" ("+m_IndicesCut[i][L]+",...,"+m_IndicesCut[i][m_IndicesCut[i].length-1]+")");

        // Train multi-label classifier

        if (m_Classifiers[i] instanceof Randomizable) ((Randomizable)m_Classifiers[i]).setSeed(m_Seed+i);
        if(getDebug()) System.out.println(".");

        m_Classifiers[i].buildClassifier(D_cut);
        m_InstanceTemplates[i] = D_cut.instance(1);
        m_InstancesTemplates[i] = new Instances(D_cut,0);
    }
    if (getDebug()) System.out.println(":-");
}
项目:jbossBA    文件:RandomSubSpace.java   
/**
  * builds the classifier.
  *
  * @param data     the training data to be used for generating the
  *             classifier.
  * @throws Exception   if the classifier could not be built successfully
  */
 public void buildClassifier(Instances data) throws Exception {

   // can classifier handle the data?
   getCapabilities().testWithFail(data);

   // remove instances with missing class
   data = new Instances(data);
   data.deleteWithMissingClass();

   // only class? -> build ZeroR model
   if (data.numAttributes() == 1) {
     System.err.println(
  "Cannot build model (only class attribute present in data!), "
  + "using ZeroR model instead!");
     m_ZeroR = new weka.classifiers.rules.ZeroR();
     m_ZeroR.buildClassifier(data);
     return;
   }
   else {
     m_ZeroR = null;
   }

   super.buildClassifier(data);

   Integer[] indices = new Integer[data.numAttributes()-1];
   int classIndex = data.classIndex();
   int offset = 0;
   for(int i = 0; i < indices.length+1; i++) {
     if (i != classIndex) {
indices[offset++] = i+1;
     }
   }
   int subSpaceSize = numberOfAttributes(indices.length, getSubSpaceSize());
   Random random = data.getRandomNumberGenerator(m_Seed);

   for (int j = 0; j < m_Classifiers.length; j++) {
     if (m_Classifier instanceof Randomizable) {
((Randomizable) m_Classifiers[j]).setSeed(random.nextInt());
     }
     FilteredClassifier fc = new FilteredClassifier();
     fc.setClassifier(m_Classifiers[j]);
     m_Classifiers[j] = fc;
     Remove rm = new Remove();
     rm.setOptions(new String[]{"-V", "-R", randomSubSpace(indices,subSpaceSize,classIndex+1,random)});
     fc.setFilter(rm);

     // build the classifier
     m_Classifiers[j].buildClassifier(data);
   }

 }