Coverage Report - weka.classifiers.evaluation.output.prediction.PlainText
 
Classes in this File Line Coverage Branch Coverage Complexity
PlainText
0%
0/76
0%
0/56
5.667
 
 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 3 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, see <http://www.gnu.org/licenses/>.
 14  
  */
 15  
 
 16  
 /*
 17  
  * PlainText.java
 18  
  * Copyright (C) 2009-2012 University of Waikato, Hamilton, New Zealand
 19  
  */
 20  
 
 21  
 package weka.classifiers.evaluation.output.prediction;
 22  
 
 23  
 import weka.classifiers.Classifier;
 24  
 import weka.core.Instance;
 25  
 import weka.core.Utils;
 26  
 
 27  
 /**
 28  
  <!-- globalinfo-start -->
 29  
  * Outputs the predictions in plain text.
 30  
  * <p/>
 31  
  <!-- globalinfo-end -->
 32  
  *
 33  
  <!-- options-start -->
 34  
  * Valid options are: <p/>
 35  
  * 
 36  
  * <pre> -p &lt;range&gt;
 37  
  *  The range of attributes to print in addition to the classification.
 38  
  *  (default: none)</pre>
 39  
  * 
 40  
  * <pre> -distribution
 41  
  *  Whether to turn on the output of the class distribution.
 42  
  *  Only for nominal class attributes.
 43  
  *  (default: off)</pre>
 44  
  * 
 45  
  * <pre> -decimals &lt;num&gt;
 46  
  *  The number of digits after the decimal point.
 47  
  *  (default: 3)</pre>
 48  
  * 
 49  
  * <pre> -file &lt;path&gt;
 50  
  *  The file to store the output in, instead of outputting it on stdout.
 51  
  *  Gets ignored if the supplied path is a directory.
 52  
  *  (default: .)</pre>
 53  
  * 
 54  
  * <pre> -suppress
 55  
  *  In case the data gets stored in a file, then this flag can be used
 56  
  *  to suppress the regular output.
 57  
  *  (default: not suppressed)</pre>
 58  
  * 
 59  
  <!-- options-end -->
 60  
  *
 61  
  * @author  fracpete (fracpete at waikato dot ac dot nz)
 62  
  * @version $Revision: 8034 $
 63  
  */
 64  0
 public class PlainText
 65  
   extends AbstractOutput {
 66  
   
 67  
   /** for serialization. */
 68  
   private static final long serialVersionUID = 2033389864898242735L;
 69  
   
 70  
   /**
 71  
    * Returns a string describing the output generator.
 72  
    * 
 73  
    * @return                 a description suitable for
 74  
    *                         displaying in the GUI
 75  
    */
 76  
   public String globalInfo() {
 77  0
     return "Outputs the predictions in plain text.";
 78  
   }
 79  
   
 80  
   /**
 81  
    * Returns a short display text, to be used in comboboxes.
 82  
    * 
 83  
    * @return                 a short display text
 84  
    */
 85  
   public String getDisplay() {
 86  0
     return "Plain text";
 87  
   }
 88  
 
 89  
   /**
 90  
    * Performs the actual printing of the header.
 91  
    */
 92  
   protected void doPrintHeader() {
 93  0
     if (m_Header.classAttribute().isNominal())
 94  0
       if (m_OutputDistribution)
 95  0
         append(" inst#     actual  predicted error distribution");
 96  
       else
 97  0
         append(" inst#     actual  predicted error prediction");
 98  
     else
 99  0
       append(" inst#     actual  predicted      error");
 100  
     
 101  0
     if (m_Attributes != null) {
 102  0
       append(" (");
 103  0
       boolean first = true;
 104  0
       for (int i = 0; i < m_Header.numAttributes(); i++) {
 105  0
         if (i == m_Header.classIndex())
 106  0
           continue;
 107  
 
 108  0
         if (m_Attributes.isInRange(i)) {
 109  0
           if (!first)
 110  0
             append(",");
 111  0
           append(m_Header.attribute(i).name());
 112  0
           first = false;
 113  
         }
 114  
       }
 115  0
       append(")");
 116  
     }
 117  
     
 118  0
     append("\n");
 119  0
   }
 120  
 
 121  
   /**
 122  
    * Builds a string listing the attribute values in a specified range of indices,
 123  
    * separated by commas and enclosed in brackets.
 124  
    *
 125  
    * @param instance         the instance to print the values from
 126  
    * @return                 a string listing values of the attributes in the range
 127  
    */
 128  
   protected String attributeValuesString(Instance instance) {
 129  0
     StringBuffer text = new StringBuffer();
 130  0
     if (m_Attributes != null) {
 131  0
       boolean firstOutput = true;
 132  0
       m_Attributes.setUpper(instance.numAttributes() - 1);
 133  0
       for (int i=0; i<instance.numAttributes(); i++)
 134  0
         if (m_Attributes.isInRange(i) && i != instance.classIndex()) {
 135  0
           if (firstOutput) text.append("(");
 136  0
           else text.append(",");
 137  0
           text.append(instance.toString(i));
 138  0
           firstOutput = false;
 139  
         }
 140  0
       if (!firstOutput) text.append(")");
 141  
     }
 142  0
     return text.toString();
 143  
   }
 144  
 
 145  
   /**
 146  
    * Store the prediction made by the classifier as a string.
 147  
    * 
 148  
    * @param classifier        the classifier to use
 149  
    * @param inst        the instance to generate text from
 150  
    * @param index        the index in the dataset
 151  
    * @throws Exception        if something goes wrong
 152  
    */
 153  
   protected void doPrintClassification(Classifier classifier, Instance inst, int index) throws Exception {
 154  0
     int width = 7 + m_NumDecimals;
 155  0
     int prec = m_NumDecimals;
 156  
 
 157  0
     Instance withMissing = (Instance)inst.copy();
 158  0
     withMissing.setDataset(inst.dataset());    
 159  0
     inst = preProcessInstance(inst, withMissing, classifier);
 160  
     
 161  0
     double predValue = classifier.classifyInstance(withMissing);
 162  
 
 163  
     // index
 164  0
     append(Utils.padLeft("" + (index+1), 6));
 165  
 
 166  0
     if (inst.dataset().classAttribute().isNumeric()) {
 167  
       // actual
 168  0
       if (inst.classIsMissing())
 169  0
         append(" " + Utils.padLeft("?", width));
 170  
       else
 171  0
         append(" " + Utils.doubleToString(inst.classValue(), width, prec));
 172  
       // predicted
 173  0
       if (Utils.isMissingValue(predValue))
 174  0
         append(" " + Utils.padLeft("?", width));
 175  
       else
 176  0
         append(" " + Utils.doubleToString(predValue, width, prec));
 177  
       // error
 178  0
       if (Utils.isMissingValue(predValue) || inst.classIsMissing())
 179  0
         append(" " + Utils.padLeft("?", width));
 180  
       else
 181  0
         append(" " + Utils.doubleToString(predValue - inst.classValue(), width, prec));
 182  
     } else {
 183  
       // actual
 184  0
       append(" " + Utils.padLeft(((int) inst.classValue()+1) + ":" + inst.toString(inst.classIndex()), width));
 185  
       // predicted
 186  0
       if (Utils.isMissingValue(predValue))
 187  0
         append(" " + Utils.padLeft("?", width));
 188  
       else
 189  0
         append(" " + Utils.padLeft(((int) predValue+1) + ":" + inst.dataset().classAttribute().value((int)predValue), width));
 190  
       // error?
 191  0
       if (!Utils.isMissingValue(predValue) && !inst.classIsMissing() && ((int) predValue+1 != (int) inst.classValue()+1))
 192  0
         append(" " + "  +  ");
 193  
       else
 194  0
         append(" " + "     ");
 195  
       // prediction/distribution
 196  0
       if (m_OutputDistribution) {
 197  0
         if (Utils.isMissingValue(predValue)) {
 198  0
           append(" " + "?");
 199  
         }
 200  
         else {
 201  0
           append(" ");
 202  0
           double[] dist = classifier.distributionForInstance(withMissing);
 203  0
           for (int n = 0; n < dist.length; n++) {
 204  0
             if (n > 0)
 205  0
               append(",");
 206  0
             if (n == (int) predValue)
 207  0
               append("*");
 208  0
             append(Utils.doubleToString(dist[n], prec));
 209  
           }
 210  0
         }
 211  
       }
 212  
       else {
 213  0
         if (Utils.isMissingValue(predValue))
 214  0
           append(" " + "?");
 215  
         else
 216  0
           append(" " + Utils.doubleToString(classifier.distributionForInstance(withMissing) [(int)predValue], prec));
 217  
       }
 218  
     }
 219  
 
 220  
     // attributes
 221  0
     append(" " + attributeValuesString(withMissing) + "\n");
 222  0
   }
 223  
   
 224  
   /**
 225  
    * Does nothing.
 226  
    */
 227  
   protected void doPrintFooter() {
 228  0
   }
 229  
 }