示例#1
0
  /**
   * @param ex the given test exemplar
   * @return the classification
   * @throws Exception if the exemplar could not be classified successfully
   */
  public double classifyInstance(Instance ex) throws Exception {
    // Instance ex = new Exemplar(e);
    Instances exi = ex.relationalValue(1);
    double[] n = new double[m_Dimension];
    double[] xBar = new double[m_Dimension];
    for (int i = 0; i < exi.numAttributes(); i++) xBar[i] = exi.meanOrMode(i);

    for (int w = 0, t = 0; w < m_Dimension; w++, t++) {
      // if((t==m_ClassIndex) || (t==m_IdIndex))
      // t++;
      for (int u = 0; u < exi.numInstances(); u++)
        if (!exi.instance(u).isMissing(t)) n[w] += exi.instance(u).weight();
    }

    double logOdds = likelihoodRatio(n, xBar);
    return (logOdds > m_Cutoff) ? 1 : 0;
  }
示例#2
0
  /**
   * Computes the distribution for a given exemplar
   *
   * @param ex the exemplar for which distribution is computed
   * @return the distribution
   * @throws Exception if the distribution can't be computed successfully
   */
  public double[] distributionForInstance(Instance ex) throws Exception {

    double[] distribution = new double[2];
    Instances exi = ex.relationalValue(1);
    double[] n = new double[m_Dimension];
    double[] xBar = new double[m_Dimension];
    for (int i = 0; i < exi.numAttributes(); i++) xBar[i] = exi.meanOrMode(i);

    for (int w = 0, t = 0; w < m_Dimension; w++, t++) {
      for (int u = 0; u < exi.numInstances(); u++)
        if (!exi.instance(u).isMissing(t)) n[w] += exi.instance(u).weight();
    }

    double logOdds = likelihoodRatio(n, xBar);

    // returned logOdds value has been divided by m_Dimension to avoid
    // Math.exp(logOdds) getting too large or too small,
    // that may result in two fixed distribution value (1 or 0).
    distribution[0] = 1 / (1 + Math.exp(logOdds)); // Prob. for class 0 (negative)
    distribution[1] = 1 - distribution[0];

    return distribution;
  }