Package weka.estimators

Examples of weka.estimators.DiscreteEstimator


          if ((int)width > maxWidth) {
            maxWidth = (int)width;
          }
        } else {
          // nominal distributions
          DiscreteEstimator d = (DiscreteEstimator)m_model[i][j];
          for (int k = 0; k < d.getNumSymbols(); k++) {
            String size = Utils.doubleToString(d.getCount(k), maxWidth, 4).trim();
            if (size.length() > maxWidth) {
              maxWidth = size.length();
            }
          }
          int sum =
            Utils.doubleToString(d.getSumOfCounts(), maxWidth, 4).trim().length();
          if (sum > maxWidth) {
            maxWidth = sum;
          }
        }
      }
    }

    if (maxAttWidth < "Attribute".length()) {
      maxAttWidth = "Attribute".length();
    }   
   
    maxAttWidth += 2;

    temp.append("\n\n");
    temp.append(pad("Cluster", " ",
                    (maxAttWidth + maxWidth + 1) - "Cluster".length(),
                    true));
   
    temp.append("\n");
    temp.append(pad("Attribute", " ", maxAttWidth - "Attribute".length(), false));

    // cluster #'s
    for (int i = 0; i < m_num_clusters; i++) {
      String classL = "" + i;
      temp.append(pad(classL, " ", maxWidth + 1 - classL.length(), true));
    }
    temp.append("\n");

    // cluster priors
    temp.append(pad("", " ", maxAttWidth, true));
    for (int i = 0; i < m_num_clusters; i++) {
      String priorP = Utils.doubleToString(m_priors[i], maxWidth, 2).trim();
      priorP = "(" + priorP + ")";
      temp.append(pad(priorP, " ", maxWidth + 1 - priorP.length(), true));
    }

    temp.append("\n");
    temp.append(pad("", "=", maxAttWidth +
                    (maxWidth * m_num_clusters)
                    + m_num_clusters + 1, true));
    temp.append("\n");

    for (int i = 0; i < m_num_attribs; i++) {
      String attName = m_theInstances.attribute(i).name();
      temp.append(attName + "\n");

      if (m_theInstances.attribute(i).isNumeric()) {
        String meanL = "  mean";
        temp.append(pad(meanL, " ", maxAttWidth + 1 - meanL.length(), false));
        for (int j = 0; j < m_num_clusters; j++) {
          // means
          String mean =
            Utils.doubleToString(m_modelNormal[j][i][0], maxWidth, 4).trim();
          temp.append(pad(mean, " ", maxWidth + 1 - mean.length(), true));
        }
        temp.append("\n");           
        // now do std deviations
        String stdDevL = "  std. dev.";
        temp.append(pad(stdDevL, " ", maxAttWidth + 1 - stdDevL.length(), false));
        for (int j = 0; j < m_num_clusters; j++) {
          String stdDev =
            Utils.doubleToString(m_modelNormal[j][i][1], maxWidth, 4).trim();
          temp.append(pad(stdDev, " ", maxWidth + 1 - stdDev.length(), true));
        }
        temp.append("\n\n");
      } else {
        Attribute a = m_theInstances.attribute(i);
        for (int j = 0; j < a.numValues(); j++) {
          String val = "  " + a.value(j);
          temp.append(pad(val, " ", maxAttWidth + 1 - val.length(), false));
          for (int k = 0; k < m_num_clusters; k++) {
            DiscreteEstimator d = (DiscreteEstimator)m_model[k][i];
            String count = Utils.doubleToString(d.getCount(j), maxWidth, 4).trim();
            temp.append(pad(count, " ", maxWidth + 1 - count.length(), true));
          }
          temp.append("\n");
        }
        // do the totals
        String total = "  [total]";
        temp.append(pad(total, " ", maxAttWidth + 1 - total.length(), false));
        for (int k = 0; k < m_num_clusters; k++) {
          DiscreteEstimator d = (DiscreteEstimator)m_model[k][i];
          String count =
            Utils.doubleToString(d.getSumOfCounts(), maxWidth, 4).trim();
            temp.append(pad(count, " ", maxWidth + 1 - count.length(), true));
        }
        temp.append("\n");       
      }
    }
View Full Code Here


      m_Disc = null;
    }

    // Reserve space for the distributions
    m_Distributions = new Estimator[m_Instances.numAttributes() - 1][m_Instances.numClasses()];
    m_ClassDistribution = new DiscreteEstimator( m_Instances.numClasses(),
        true );
    int attIndex = 0;
    Enumeration enu = m_Instances.enumerateAttributes();
    while( enu.hasMoreElements() ) {
      Attribute attribute = (Attribute) enu.nextElement();

      // If the attribute is numeric, determine the estimator
      // numeric precision from differences between adjacent values
      double numPrecision = DEFAULT_NUM_PRECISION;
      if( attribute.type() == Attribute.NUMERIC ) {
        m_Instances.sort( attribute );
        if( (m_Instances.numInstances() > 0) && !m_Instances.instance( 0 ).isMissing( attribute ) ) {
          double lastVal = m_Instances.instance( 0 ).value( attribute );
          double currentVal, deltaSum = 0;
          int distinct = 0;
          for( int i = 1; i < m_Instances.numInstances(); i++ ) {
            Instance currentInst = m_Instances.instance( i );
            if( currentInst.isMissing( attribute ) ) {
              break;
            }
            currentVal = currentInst.value( attribute );
            if( currentVal != lastVal ) {
              deltaSum += currentVal - lastVal;
              lastVal = currentVal;
              distinct++;
            }
          }
          if( distinct > 0 ) {
            numPrecision = deltaSum / distinct;
          }
        }
      }


      for( int j = 0; j < m_Instances.numClasses(); j++ ) {
        switch( attribute.type() ) {
          case Attribute.NUMERIC:
            if( m_UseKernelEstimator ) {
              m_Distributions[attIndex][j] =
                  new KernelEstimator( numPrecision );
            } else {
              m_Distributions[attIndex][j] =
                  new NormalEstimator( numPrecision );
            }
            break;
          case Attribute.NOMINAL:
            m_Distributions[attIndex][j] =
                new DiscreteEstimator( attribute.numValues(), true );
            break;
          default:
            throw new Exception( "Attribute type unknown to NaiveBayes" );
        }
      }
View Full Code Here

                  maxWidth = m.length();
                }
              }
            }
          } else if( m_Distributions[i][0] instanceof DiscreteEstimator ) {
            DiscreteEstimator d = (DiscreteEstimator) m_Distributions[i][j];
            for( int k = 0; k < d.getNumSymbols(); k++ ) {
              String size = "" + d.getCount( k );
              if( size.length() > maxWidth ) {
                maxWidth = size.length();
              }
            }
            int sum = ("" + d.getSumOfCounts()).length();
            if( sum > maxWidth ) {
              maxWidth = sum;
            }
          }
        }
      }

      // Check width of class labels
      for( int i = 0; i < m_Instances.numClasses(); i++ ) {
        String cSize = m_Instances.classAttribute().value( i );
        if( cSize.length() > maxWidth ) {
          maxWidth = cSize.length();
        }
      }

      // Check width of class priors
      for( int i = 0; i < m_Instances.numClasses(); i++ ) {
        String priorP =
            Utils.doubleToString( ((DiscreteEstimator) m_ClassDistribution).getProbability( i ),
            maxWidth, 2 ).trim();
        priorP = "(" + priorP + ")";
        if( priorP.length() > maxWidth ) {
          maxWidth = priorP.length();
        }
      }

      if( maxAttWidth < "Attribute".length() ) {
        maxAttWidth = "Attribute".length();
      }

      if( maxAttWidth < "  weight sum".length() ) {
        maxAttWidth = "  weight sum".length();
      }

      if( containsKernel ) {
        if( maxAttWidth < "  [precision]".length() ) {
          maxAttWidth = "  [precision]".length();
        }
      }

      maxAttWidth += 2;



      temp.append( "\n\n" );
      temp.append( pad( "Class", " ",
          (maxAttWidth + maxWidth + 1) - "Class".length(),
          true ) );

      temp.append( "\n" );
      temp.append( pad( "Attribute", " ", maxAttWidth - "Attribute".length(), false ) );
      // class labels
      for( int i = 0; i < m_Instances.numClasses(); i++ ) {
        String classL = m_Instances.classAttribute().value( i );
        temp.append( pad( classL, " ", maxWidth + 1 - classL.length(), true ) );
      }
      temp.append( "\n" );
      // class priors
      temp.append( pad( "", " ", maxAttWidth, true ) );
      for( int i = 0; i < m_Instances.numClasses(); i++ ) {
        String priorP =
            Utils.doubleToString( ((DiscreteEstimator) m_ClassDistribution).getProbability( i ),
            maxWidth, 2 ).trim();
        priorP = "(" + priorP + ")";
        temp.append( pad( priorP, " ", maxWidth + 1 - priorP.length(), true ) );
      }
      temp.append( "\n" );
      temp.append( pad( "", "=", maxAttWidth +
          (maxWidth * m_Instances.numClasses()) + m_Instances.numClasses() + 1, true ) );
      temp.append( "\n" );

      // loop over the attributes
      int counter = 0;
      for( int i = 0; i < m_Instances.numAttributes(); i++ ) {
        if( i == m_Instances.classIndex() ) {
          continue;
        }
        String attName = m_Instances.attribute( i ).name();
        temp.append( attName + "\n" );

        if( m_Distributions[counter][0] instanceof NormalEstimator ) {
          String meanL = "  mean";
          temp.append( pad( meanL, " ", maxAttWidth + 1 - meanL.length(), false ) );
          for( int j = 0; j < m_Instances.numClasses(); j++ ) {
            // means
            NormalEstimator n = (NormalEstimator) m_Distributions[counter][j];
            String mean =
                Utils.doubleToString( n.getMean(), maxWidth, 4 ).trim();
            temp.append( pad( mean, " ", maxWidth + 1 - mean.length(), true ) );
          }
          temp.append( "\n" );
          // now do std deviations
          String stdDevL = "  std. dev.";
          temp.append( pad( stdDevL, " ", maxAttWidth + 1 - stdDevL.length(), false ) );
          for( int j = 0; j < m_Instances.numClasses(); j++ ) {
            NormalEstimator n = (NormalEstimator) m_Distributions[counter][j];
            String stdDev =
                Utils.doubleToString( n.getStdDev(), maxWidth, 4 ).trim();
            temp.append( pad( stdDev, " ", maxWidth + 1 - stdDev.length(), true ) );
          }
          temp.append( "\n" );
          // now the weight sums
          String weightL = "  weight sum";
          temp.append( pad( weightL, " ", maxAttWidth + 1 - weightL.length(), false ) );
          for( int j = 0; j < m_Instances.numClasses(); j++ ) {
            NormalEstimator n = (NormalEstimator) m_Distributions[counter][j];
            String weight =
                Utils.doubleToString( n.getSumOfWeights(), maxWidth, 4 ).trim();
            temp.append( pad( weight, " ", maxWidth + 1 - weight.length(), true ) );
          }
          temp.append( "\n" );
          // now the precisions
          String precisionL = "  precision";
          temp.append( pad( precisionL, " ", maxAttWidth + 1 - precisionL.length(), false ) );
          for( int j = 0; j < m_Instances.numClasses(); j++ ) {
            NormalEstimator n = (NormalEstimator) m_Distributions[counter][j];
            String precision =
                Utils.doubleToString( n.getPrecision(), maxWidth, 4 ).trim();
            temp.append( pad( precision, " ", maxWidth + 1 - precision.length(), true ) );
          }
          temp.append( "\n\n" );

        } else if( m_Distributions[counter][0] instanceof DiscreteEstimator ) {
          Attribute a = m_Instances.attribute( i );
          for( int j = 0; j < a.numValues(); j++ ) {
            String val = "  " + a.value( j );
            temp.append( pad( val, " ", maxAttWidth + 1 - val.length(), false ) );
            for( int k = 0; k < m_Instances.numClasses(); k++ ) {
              DiscreteEstimator d = (DiscreteEstimator) m_Distributions[counter][k];
              String count = "" + d.getCount( j );
              temp.append( pad( count, " ", maxWidth + 1 - count.length(), true ) );
            }
            temp.append( "\n" );
          }
          // do the totals
          String total = "  [total]";
          temp.append( pad( total, " ", maxAttWidth + 1 - total.length(), false ) );
          for( int k = 0; k < m_Instances.numClasses(); k++ ) {
            DiscreteEstimator d = (DiscreteEstimator) m_Distributions[counter][k];
            String count = "" + d.getSumOfCounts();
            temp.append( pad( count, " ", maxWidth + 1 - count.length(), true ) );
          }
          temp.append( "\n\n" );
        } else if( m_Distributions[counter][0] instanceof KernelEstimator ) {
          String kL = "  [# kernels]";
View Full Code Here

    while (i.hasNext()) {
      Instance instance = (Instance) i.next();
      Coordinates c = new Coordinates(instance);

      // get DiscreteEstimator from the map
      DiscreteEstimator df =
  (DiscreteEstimator) distributions.get(c);

      // if no DiscreteEstimator is present in the map, create one
      if (df == null) {
  df = new DiscreteEstimator(instances.numClasses(), 0);
      }
      df.addValue(instance.classValue(),instance.weight()); // update
      distributions.put(c,df); // put back in map
    }


    // build the map of cumulative distribution functions
    Map cumulativeDistributions =
      new HashMap(distributions.size());

    // Cycle trough the map of discrete distributions, and create a new
    // one containing cumulative discrete distributions
    for (Iterator it=distributions.keySet().iterator();
    it.hasNext();) {
      Coordinates c = (Coordinates) it.next();
      DiscreteEstimator df =
  (DiscreteEstimator) distributions.get(c);
      cumulativeDistributions.put
      (c, new CumulativeDiscreteDistribution(df));
    }
    int[] revPref = new int[5];
View Full Code Here

    if (numValues == 0) {
      throw new IllegalArgumentException
      ("Can't create histogram for numeric attribute");
    }

    DiscreteEstimator de = new DiscreteEstimator(numValues, false);
    Iterator it = new EnumerationIterator(instances.enumerateInstances());
    while (it.hasNext()) {
      Instance instance = (Instance) it.next();
      if (!instance.isMissing(attributeIndex)) {
  de.addValue(instance.value(attributeIndex), instance.weight());
      }
    }
    return de;
  }
View Full Code Here

  if (current > max) {
    max = current;
  }
      }

      DiscreteEstimator newEst = new DiscreteEstimator(max + 1, true);

      for (int i = 0; i < argv.length; i++) {
  current = Integer.parseInt(argv[i]);

  System.out.println(newEst);
  System.out.println("Prediction for " + current + " = "
         + newEst.getProbability(current));
  newEst.addValue(current, 1);
      }
    } catch (Exception e) {
      System.out.println(e.getMessage());
    }
  }    // main
View Full Code Here

  if (current > max) {
    max = current;
  }
      }

      DiscreteEstimator newEst = new DiscreteEstimator(max + 1, true);

      for (int i = 0; i < argv.length; i++) {
  current = Integer.parseInt(argv[i]);

  System.out.println(newEst);
  System.out.println("Prediction for " + current + " = "
         + newEst.getProbability(current));
  newEst.addValue(current, 1);
      }
    } catch (Exception e) {
      System.out.println(e.getMessage());
    }
  }    // main
View Full Code Here

  (CumulativeDiscreteDistribution)
  m_estimatedCumulativeDistributions.get(yc);

      if (yc.equals(xc)) {
  // update n_m and n_M
  DiscreteEstimator df =
    (DiscreteEstimator) m_estimatedDistributions.get(yc);
  updateN_m(n_m,df);
  updateN_M(n_M,df);

  fMin = DistributionUtils.takeMin(fMin,cdf);
  fMax = DistributionUtils.takeMax(fMax,cdf);
      } else if (yc.strictlySmaller(xc)) {
  // update n_m
  DiscreteEstimator df =
    (DiscreteEstimator) m_estimatedDistributions.get(yc);
  updateN_m(n_m, df);
  fMin = DistributionUtils.takeMin(fMin,cdf);
      }
      else if (xc.strictlySmaller(yc)) {
  // update n_M
  DiscreteEstimator df =
    (DiscreteEstimator) m_estimatedDistributions.get(yc);
  updateN_M(n_M, df);
  fMax = DistributionUtils.takeMax(fMax,cdf);
      }
    }
View Full Code Here

    it.hasNext();) {
      Instance instance = (Instance) it.next();
      Coordinates c = new Coordinates(instance);

      // get DiscreteEstimator from the map
      DiscreteEstimator df =
  (DiscreteEstimator) m_estimatedDistributions.get(c);

      // if no DiscreteEstimator is present in the map, create one
      if (df == null) {
  df = new DiscreteEstimator(instances.numClasses(),0);
      }
      df.addValue(instance.classValue(),instance.weight()); // update
      m_estimatedDistributions.put(c,df); // put back in map
    }


    // build the map of cumulative distribution functions
    m_estimatedCumulativeDistributions =
      new HashMap(m_estimatedDistributions.size()/2);

    // Cycle trough the map of discrete distributions, and create a new
    // one containing cumulative discrete distributions
    for (Iterator it=m_estimatedDistributions.keySet().iterator();
    it.hasNext();) {
      Coordinates c = (Coordinates) it.next();
      DiscreteEstimator df =
  (DiscreteEstimator) m_estimatedDistributions.get(c);
      m_estimatedCumulativeDistributions.put
      (c, new CumulativeDiscreteDistribution(df));
    }
View Full Code Here

   */
  private void removeInstance(Instance instance) {
    Coordinates c = new Coordinates(instance);

    // Remove instance temporarily from the Maps with the distributions
    DiscreteEstimator df =
      (DiscreteEstimator) m_estimatedDistributions.get(c);

    // remove from df
    df.addValue(instance.classValue(),-instance.weight());

    if (Math.abs(df.getSumOfCounts() - 0) < Utils.SMALL) {

      /* There was apparently only one example with coordinates c
       * in the training set, and now we removed it.
       * Remove the key c from both maps.
       */
 
View Full Code Here

TOP

Related Classes of weka.estimators.DiscreteEstimator

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.