AttributeStats.java
Upload User: rhdiban
Upload Date: 2013-08-09
Package Size: 15085k
Code Size: 4k
Category:

Windows Develop

Development Platform:

Java

  1. /*
  2.  *    This program is free software; you can redistribute it and/or modify
  3.  *    it under the terms of the GNU General Public License as published by
  4.  *    the Free Software Foundation; either version 2 of the License, or
  5.  *    (at your option) any later version.
  6.  *
  7.  *    This program is distributed in the hope that it will be useful,
  8.  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
  9.  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  10.  *    GNU General Public License for more details.
  11.  *
  12.  *    You should have received a copy of the GNU General Public License
  13.  *    along with this program; if not, write to the Free Software
  14.  *    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15.  */
  16. /*
  17.  *    AttributeStats.java
  18.  *    Copyright (C) 1999 Len Trigg
  19.  *
  20.  */
  21. package weka.core;
  22. import java.io.Serializable;
  23. /**
  24.  * A Utility class that contains summary information on an
  25.  * the values that appear in a dataset for a particular attribute.
  26.  *
  27.  * @author <a href="mailto:len@webmind.com">Len Trigg</a>
  28.  * @version $Revision: 1.6 $
  29.  */
  30. public class AttributeStats implements Serializable {    
  31.   
  32.   /** The number of int-like values */
  33.   public int intCount = 0;
  34.   
  35.   /** The number of real-like values (i.e. have a fractional part) */
  36.   public int realCount = 0;
  37.   
  38.   /** The number of missing values */
  39.   public int missingCount = 0;
  40.   
  41.   /** The number of distinct values */
  42.   public int distinctCount = 0;
  43.   
  44.   /** The number of values that only appear once */
  45.   public int uniqueCount = 0;
  46.   
  47.   /** The total number of values (i.e. number of instances) */
  48.   public int totalCount = 0;
  49.   
  50.   /** Stats on numeric value distributions */
  51.   // perhaps Stats should be moved from weka.experiment to weka.core
  52.   public weka.experiment.Stats numericStats;
  53.   
  54.   /** Counts of each nominal value */
  55.   public int [] nominalCounts;
  56.     
  57.   /**
  58.    * Updates the counters for one more observed distinct value.
  59.    *
  60.    * @param value the value that has just been seen
  61.    * @param count the number of times the value appeared
  62.    */
  63.   protected void addDistinct(double value, int count) {
  64.     
  65.     if (count > 0) {
  66.       if (count == 1) {
  67. uniqueCount++;
  68. }
  69.       if (Utils.eq(value, (double)((int)value))) {
  70. intCount += count;
  71.       } else {
  72. realCount += count;
  73.       }
  74.       if (nominalCounts != null) {
  75. nominalCounts[(int)value] = count;
  76.       }
  77.       if (numericStats != null) {
  78.   numericStats.add(value, count);
  79.   numericStats.calculateDerived();
  80.       }
  81.     }
  82.     distinctCount++;
  83.   }
  84.   /**
  85.    * Returns a human readable representation of this AttributeStats instance.
  86.    *
  87.    * @return a String represtinging these AttributeStats.
  88.    */
  89.   public String toString() {
  90.     StringBuffer sb = new StringBuffer();
  91.     sb.append(Utils.padLeft("Type", 4)).append(Utils.padLeft("Nom", 5));
  92.     sb.append(Utils.padLeft("Int", 5)).append(Utils.padLeft("Real", 5));
  93.     sb.append(Utils.padLeft("Missing", 12));
  94.     sb.append(Utils.padLeft("Unique", 12));
  95.     sb.append(Utils.padLeft("Dist", 6));
  96.     if (nominalCounts != null) {
  97.       sb.append(' ');
  98.       for (int i = 0; i < nominalCounts.length; i++) {
  99.         sb.append(Utils.padLeft("C[" + i + "]", 5));
  100.       }
  101.     }
  102.     sb.append('n');
  103.     long percent;
  104.     percent = Math.round(100.0 * intCount / totalCount);
  105.     if (nominalCounts != null) {
  106.       sb.append(Utils.padLeft("Nom", 4)).append(' ');
  107.       sb.append(Utils.padLeft("" + percent, 3)).append("% ");
  108.       sb.append(Utils.padLeft("" + 0, 3)).append("% ");
  109.     } else {
  110.       sb.append(Utils.padLeft("Num", 4)).append(' ');
  111.       sb.append(Utils.padLeft("" + 0, 3)).append("% ");
  112.       sb.append(Utils.padLeft("" + percent, 3)).append("% ");
  113.     }
  114.     percent = Math.round(100.0 * realCount / totalCount);
  115.     sb.append(Utils.padLeft("" + percent, 3)).append("% ");
  116.     sb.append(Utils.padLeft("" + missingCount, 5)).append(" /");
  117.     percent = Math.round(100.0 * missingCount / totalCount);
  118.     sb.append(Utils.padLeft("" + percent, 3)).append("% ");
  119.     sb.append(Utils.padLeft("" + uniqueCount, 5)).append(" /");
  120.     percent = Math.round(100.0 * uniqueCount / totalCount);
  121.     sb.append(Utils.padLeft("" + percent, 3)).append("% ");
  122.     sb.append(Utils.padLeft("" + distinctCount, 5)).append(' ');
  123.     if (nominalCounts != null) {
  124.       for (int i = 0; i < nominalCounts.length; i++) {
  125.         sb.append(Utils.padLeft("" + nominalCounts[i], 5));
  126.       }
  127.     }
  128.     sb.append('n');
  129.     return sb.toString();
  130.   }
  131. }