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