⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 bagging.java

📁 代码是一个分类器的实现,其中使用了部分weka的源代码。可以将项目导入eclipse运行
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
   * displaying in the explorer/experimenter gui   */  public String bagSizePercentTipText() {    return "Size of each bag, as a percentage of the training set size.";  }  /**   * Gets the size of each bag, as a percentage of the training set size.   *   * @return the bag size, as a percentage.   */  public int getBagSizePercent() {    return m_BagSizePercent;  }    /**   * Sets the size of each bag, as a percentage of the training set size.   *   * @param newBagSizePercent the bag size, as a percentage.   */  public void setBagSizePercent(int newBagSizePercent) {    m_BagSizePercent = newBagSizePercent;  }  /**   * Returns the tip text for this property   * @return tip text for this property suitable for   * displaying in the explorer/experimenter gui   */  public String calcOutOfBagTipText() {    return "Whether the out-of-bag error is calculated.";  }  /**   * Set whether the out of bag error is calculated.   *   * @param calcOutOfBag whether to calculate the out of bag error   */  public void setCalcOutOfBag(boolean calcOutOfBag) {    m_CalcOutOfBag = calcOutOfBag;  }  /**   * Get whether the out of bag error is calculated.   *   * @return whether the out of bag error is calculated   */  public boolean getCalcOutOfBag() {    return m_CalcOutOfBag;  }  /**   * Gets the out of bag error that was calculated as the classifier   * was built.   *   * @return the out of bag error    */  public double measureOutOfBagError() {        return m_OutOfBagError;  }    /**   * Returns an enumeration of the additional measure names.   *   * @return an enumeration of the measure names   */  public Enumeration enumerateMeasures() {        Vector newVector = new Vector(1);    newVector.addElement("measureOutOfBagError");    return newVector.elements();  }    /**   * Returns the value of the named measure.   *   * @param additionalMeasureName the name of the measure to query for its value   * @return the value of the named measure   * @throws IllegalArgumentException if the named measure is not supported   */  public double getMeasure(String additionalMeasureName) {        if (additionalMeasureName.equalsIgnoreCase("measureOutOfBagError")) {      return measureOutOfBagError();    }    else {throw new IllegalArgumentException(additionalMeasureName 					     + " not supported (Bagging)");    }  }  /**   * Creates a new dataset of the same size using random sampling   * with replacement according to the given weight vector. The   * weights of the instances in the new dataset are set to one.   * The length of the weight vector has to be the same as the   * number of instances in the dataset, and all weights have to   * be positive.   *   * @param data the data to be sampled from   * @param random a random number generator   * @param sampled indicating which instance has been sampled   * @return the new dataset   * @throws IllegalArgumentException if the weights array is of the wrong   * length or contains negative weights.   */  public final Instances resampleWithWeights(Instances data,					     Random random, 					     boolean[] sampled) {    double[] weights = new double[data.numInstances()];    for (int i = 0; i < weights.length; i++) {      weights[i] = data.instance(i).weight();    }    Instances newData = new Instances(data, data.numInstances());    if (data.numInstances() == 0) {      return newData;    }    double[] probabilities = new double[data.numInstances()];    double sumProbs = 0, sumOfWeights = Utils.sum(weights);    for (int i = 0; i < data.numInstances(); i++) {      sumProbs += random.nextDouble();      probabilities[i] = sumProbs;    }    Utils.normalize(probabilities, sumProbs / sumOfWeights);    // Make sure that rounding errors don't mess things up    probabilities[data.numInstances() - 1] = sumOfWeights;    int k = 0; int l = 0;    sumProbs = 0;    while ((k < data.numInstances() && (l < data.numInstances()))) {      if (weights[l] < 0) {	throw new IllegalArgumentException("Weights have to be positive.");      }      sumProbs += weights[l];      while ((k < data.numInstances()) &&	     (probabilities[k] <= sumProbs)) { 	newData.add(data.instance(l));	sampled[l] = true;	newData.instance(k).setWeight(1);	k++;      }      l++;    }    return newData;  }  /**   * Bagging method.   *   * @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();        super.buildClassifier(data);    if (m_CalcOutOfBag && (m_BagSizePercent != 100)) {      throw new IllegalArgumentException("Bag size needs to be 100% if " +					 "out-of-bag error is to be calculated!");    }    int bagSize = data.numInstances() * m_BagSizePercent / 100;    Random random = new Random(m_Seed);        boolean[][] inBag = null;    if (m_CalcOutOfBag)      inBag = new boolean[m_Classifiers.length][];        for (int j = 0; j < m_Classifiers.length; j++) {      Instances bagData = null;      // create the in-bag dataset      if (m_CalcOutOfBag) {	inBag[j] = new boolean[data.numInstances()];	bagData = resampleWithWeights(data, random, inBag[j]);      } else {	bagData = data.resampleWithWeights(random);	if (bagSize < data.numInstances()) {	  bagData.randomize(random);	  Instances newBagData = new Instances(bagData, 0, bagSize);	  bagData = newBagData;	}      }            if (m_Classifier instanceof Randomizable) {	((Randomizable) m_Classifiers[j]).setSeed(random.nextInt());      }            // build the classifier      m_Classifiers[j].buildClassifier(bagData);    }        // calc OOB error?    if (getCalcOutOfBag()) {      double outOfBagCount = 0.0;      double errorSum = 0.0;      boolean numeric = data.classAttribute().isNumeric();            for (int i = 0; i < data.numInstances(); i++) {	double vote;	double[] votes;	if (numeric)	  votes = new double[1];	else	  votes = new double[data.numClasses()];		// determine predictions for instance	int voteCount = 0;	for (int j = 0; j < m_Classifiers.length; j++) {	  if (!inBag[j][i])	    continue;	  	  voteCount++;	  double pred = m_Classifiers[j].classifyInstance(data.instance(i));	  if (numeric)	    votes[0] += pred;	  else	    votes[(int) pred]++;	}		// "vote"	if (numeric)	  vote = votes[0] / voteCount;    // average	else	  vote = Utils.maxIndex(votes);   // majority vote		// error for instance	outOfBagCount += data.instance(i).weight();	if (numeric) {	  errorSum += StrictMath.abs(vote - data.instance(i).classValue()) 	  * data.instance(i).weight();	}	else {	  if (vote != data.instance(i).classValue())	    errorSum += data.instance(i).weight();	}      }            m_OutOfBagError = errorSum / outOfBagCount;    }    else {      m_OutOfBagError = 0;    }  }  /**   * Calculates the class membership probabilities for the given test   * instance.   *   * @param instance the instance to be classified   * @return preedicted class probability distribution   * @throws Exception if distribution can't be computed successfully    */  public double[] distributionForInstance(Instance instance) throws Exception {    double [] sums = new double [instance.numClasses()], newProbs;         for (int i = 0; i < m_NumIterations; i++) {      if (instance.classAttribute().isNumeric() == true) {	sums[0] += m_Classifiers[i].classifyInstance(instance);      } else {	newProbs = m_Classifiers[i].distributionForInstance(instance);	for (int j = 0; j < newProbs.length; j++)	  sums[j] += newProbs[j];      }    }    if (instance.classAttribute().isNumeric() == true) {      sums[0] /= (double)m_NumIterations;      return sums;    } else if (Utils.eq(Utils.sum(sums), 0)) {      return sums;    } else {      Utils.normalize(sums);      return sums;    }  }  /**   * Returns description of the bagged classifier.   *   * @return description of the bagged classifier as a string   */  public String toString() {        if (m_Classifiers == null) {      return "Bagging: No model built yet.";    }    StringBuffer text = new StringBuffer();    text.append("All the base classifiers: \n\n");    for (int i = 0; i < m_Classifiers.length; i++)      text.append(m_Classifiers[i].toString() + "\n\n");        if (m_CalcOutOfBag) {      text.append("Out of bag error: "		  + Utils.doubleToString(m_OutOfBagError, 4)		  + "\n\n");    }    return text.toString();  }  /**   * Main method for testing this class.   *   * @param argv the options   */  public static void main(String [] argv) {    runClassifier(new Bagging(), argv);  }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -