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

📄 votedperceptron.java

📁 Weka
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
   * @param insts the data to train the classifier with   * @throws Exception if something goes wrong during building   */  public void buildClassifier(Instances insts) throws Exception {     // can classifier handle the data?    getCapabilities().testWithFail(insts);    // remove instances with missing class    insts = new Instances(insts);    insts.deleteWithMissingClass();        // Filter data    m_Train = new Instances(insts);    m_ReplaceMissingValues = new ReplaceMissingValues();    m_ReplaceMissingValues.setInputFormat(m_Train);    m_Train = Filter.useFilter(m_Train, m_ReplaceMissingValues);        m_NominalToBinary = new NominalToBinary();    m_NominalToBinary.setInputFormat(m_Train);    m_Train = Filter.useFilter(m_Train, m_NominalToBinary);    /** Randomize training data */    m_Train.randomize(new Random(m_Seed));    /** Make space to store perceptrons */    m_Additions = new int[m_MaxK + 1];    m_IsAddition = new boolean[m_MaxK + 1];    m_Weights = new int[m_MaxK + 1];    /** Compute perceptrons */    m_K = 0;  out:    for (int it = 0; it < m_NumIterations; it++) {      for (int i = 0; i < m_Train.numInstances(); i++) {	Instance inst = m_Train.instance(i);	if (!inst.classIsMissing()) {	  int prediction = makePrediction(m_K, inst);	  int classValue = (int) inst.classValue();	  if (prediction == classValue) {	    m_Weights[m_K]++;	  } else {	    m_IsAddition[m_K] = (classValue == 1);	    m_Additions[m_K] = i;	    m_K++;	    m_Weights[m_K]++;	  }	  if (m_K == m_MaxK) {	    break out;	  }	}      }    }  }  /**   * Outputs the distribution for the given output.   *   * Pipes output of SVM through sigmoid function.   * @param inst the instance for which distribution is to be computed   * @return the distribution   * @throws Exception if something goes wrong   */  public double[] distributionForInstance(Instance inst) throws Exception {    // Filter instance    m_ReplaceMissingValues.input(inst);    m_ReplaceMissingValues.batchFinished();    inst = m_ReplaceMissingValues.output();    m_NominalToBinary.input(inst);    m_NominalToBinary.batchFinished();    inst = m_NominalToBinary.output();        // Get probabilities    double output = 0, sumSoFar = 0;    if (m_K > 0) {      for (int i = 0; i <= m_K; i++) {	if (sumSoFar < 0) {	  output -= m_Weights[i];	} else {	  output += m_Weights[i];	}	if (m_IsAddition[i]) {	  sumSoFar += innerProduct(m_Train.instance(m_Additions[i]), inst);	} else {	  sumSoFar -= innerProduct(m_Train.instance(m_Additions[i]), inst);	}      }    }    double[] result = new double[2];    result[1] = 1 / (1 + Math.exp(-output));    result[0] = 1 - result[1];    return result;  }  /**   * Returns textual description of classifier.   *    * @return the model as string   */  public String toString() {    return "VotedPerceptron: Number of perceptrons=" + m_K;  }   /**   * Returns the tip text for this property   * @return tip text for this property suitable for   * displaying in the explorer/experimenter gui   */  public String maxKTipText() {    return "The maximum number of alterations to the perceptron.";  }  /**   * Get the value of maxK.   *   * @return Value of maxK.   */  public int getMaxK() {        return m_MaxK;  }    /**   * Set the value of maxK.   *   * @param v  Value to assign to maxK.   */  public void setMaxK(int v) {        m_MaxK = v;  }    /**   * Returns the tip text for this property   * @return tip text for this property suitable for   * displaying in the explorer/experimenter gui   */  public String numIterationsTipText() {    return "Number of iterations to be performed.";  }  /**   * Get the value of NumIterations.   *   * @return Value of NumIterations.   */  public int getNumIterations() {        return m_NumIterations;  }    /**   * Set the value of NumIterations.   *   * @param v  Value to assign to NumIterations.   */  public void setNumIterations(int v) {        m_NumIterations = v;  }  /**   * Returns the tip text for this property   * @return tip text for this property suitable for   * displaying in the explorer/experimenter gui   */  public String exponentTipText() {    return "Exponent for the polynomial kernel.";  }  /**   * Get the value of exponent.   *   * @return Value of exponent.   */  public double getExponent() {        return m_Exponent;  }    /**   * Set the value of exponent.   *   * @param v  Value to assign to exponent.   */  public void setExponent(double v) {        m_Exponent = v;  }    /**   * Returns the tip text for this property   * @return tip text for this property suitable for   * displaying in the explorer/experimenter gui   */  public String seedTipText() {    return "Seed for the random number generator.";  }  /**   * Get the value of Seed.   *   * @return Value of Seed.   */  public int getSeed() {        return m_Seed;  }    /**   * Set the value of Seed.   *   * @param v  Value to assign to Seed.   */  public void setSeed(int v) {        m_Seed = v;  }  /**    * Computes the inner product of two instances   *    * @param i1 first instance   * @param i2 second instance   * @return the inner product   * @throws Exception if computation fails   */  private double innerProduct(Instance i1, Instance i2) throws Exception {    // we can do a fast dot product    double result = 0;    int n1 = i1.numValues(); int n2 = i2.numValues();    int classIndex = m_Train.classIndex();    for (int p1 = 0, p2 = 0; p1 < n1 && p2 < n2;) {        int ind1 = i1.index(p1);        int ind2 = i2.index(p2);        if (ind1 == ind2) {            if (ind1 != classIndex) {                result += i1.valueSparse(p1) *                          i2.valueSparse(p2);            }            p1++; p2++;        } else if (ind1 > ind2) {            p2++;        } else {            p1++;        }    }    result += 1.0;        if (m_Exponent != 1) {      return Math.pow(result, m_Exponent);    } else {      return result;    }  }  /**    * Compute a prediction from a perceptron   *    * @param k   * @param inst the instance to make a prediction for   * @return the prediction   * @throws Exception if computation fails   */  private int makePrediction(int k, Instance inst) throws Exception {    double result = 0;    for (int i = 0; i < k; i++) {      if (m_IsAddition[i]) {	result += innerProduct(m_Train.instance(m_Additions[i]), inst);      } else {	result -= innerProduct(m_Train.instance(m_Additions[i]), inst);      }    }    if (result < 0) {      return 0;    } else {      return 1;    }  }  /**   * Main method.   *    * @param argv the commandline options   */  public static void main(String[] argv) {    runClassifier(new VotedPerceptron(), argv);  }}

⌨️ 快捷键说明

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