[4] | 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 | * TwoClassStats.java |
---|
| 19 | * Copyright (C) 2002 University of Waikato, Hamilton, New Zealand |
---|
| 20 | * |
---|
| 21 | */ |
---|
| 22 | |
---|
| 23 | package weka.classifiers.evaluation; |
---|
| 24 | |
---|
| 25 | import weka.core.RevisionHandler; |
---|
| 26 | import weka.core.RevisionUtils; |
---|
| 27 | |
---|
| 28 | /** |
---|
| 29 | * Encapsulates performance functions for two-class problems. |
---|
| 30 | * |
---|
| 31 | * @author Len Trigg (len@reeltwo.com) |
---|
| 32 | * @version $Revision: 1.9 $ |
---|
| 33 | */ |
---|
| 34 | public class TwoClassStats |
---|
| 35 | implements RevisionHandler { |
---|
| 36 | |
---|
| 37 | /** The names used when converting this object to a confusion matrix */ |
---|
| 38 | private static final String [] CATEGORY_NAMES = {"negative", "positive"}; |
---|
| 39 | |
---|
| 40 | /** Pos predicted as pos */ |
---|
| 41 | private double m_TruePos; |
---|
| 42 | |
---|
| 43 | /** Neg predicted as pos */ |
---|
| 44 | private double m_FalsePos; |
---|
| 45 | |
---|
| 46 | /** Neg predicted as neg */ |
---|
| 47 | private double m_TrueNeg; |
---|
| 48 | |
---|
| 49 | /** Pos predicted as neg */ |
---|
| 50 | private double m_FalseNeg; |
---|
| 51 | |
---|
| 52 | /** |
---|
| 53 | * Creates the TwoClassStats with the given initial performance values. |
---|
| 54 | * |
---|
| 55 | * @param tp the number of correctly classified positives |
---|
| 56 | * @param fp the number of incorrectly classified negatives |
---|
| 57 | * @param tn the number of correctly classified negatives |
---|
| 58 | * @param fn the number of incorrectly classified positives |
---|
| 59 | */ |
---|
| 60 | public TwoClassStats(double tp, double fp, double tn, double fn) { |
---|
| 61 | |
---|
| 62 | setTruePositive(tp); |
---|
| 63 | setFalsePositive(fp); |
---|
| 64 | setTrueNegative(tn); |
---|
| 65 | setFalseNegative(fn); |
---|
| 66 | } |
---|
| 67 | |
---|
| 68 | /** Sets the number of positive instances predicted as positive */ |
---|
| 69 | public void setTruePositive(double tp) { m_TruePos = tp; } |
---|
| 70 | |
---|
| 71 | /** Sets the number of negative instances predicted as positive */ |
---|
| 72 | public void setFalsePositive(double fp) { m_FalsePos = fp; } |
---|
| 73 | |
---|
| 74 | /** Sets the number of negative instances predicted as negative */ |
---|
| 75 | public void setTrueNegative(double tn) { m_TrueNeg = tn; } |
---|
| 76 | |
---|
| 77 | /** Sets the number of positive instances predicted as negative */ |
---|
| 78 | public void setFalseNegative(double fn) { m_FalseNeg = fn; } |
---|
| 79 | |
---|
| 80 | /** Gets the number of positive instances predicted as positive */ |
---|
| 81 | public double getTruePositive() { return m_TruePos; } |
---|
| 82 | |
---|
| 83 | /** Gets the number of negative instances predicted as positive */ |
---|
| 84 | public double getFalsePositive() { return m_FalsePos; } |
---|
| 85 | |
---|
| 86 | /** Gets the number of negative instances predicted as negative */ |
---|
| 87 | public double getTrueNegative() { return m_TrueNeg; } |
---|
| 88 | |
---|
| 89 | /** Gets the number of positive instances predicted as negative */ |
---|
| 90 | public double getFalseNegative() { return m_FalseNeg; } |
---|
| 91 | |
---|
| 92 | /** |
---|
| 93 | * Calculate the true positive rate. |
---|
| 94 | * This is defined as<p> |
---|
| 95 | * <pre> |
---|
| 96 | * correctly classified positives |
---|
| 97 | * ------------------------------ |
---|
| 98 | * total positives |
---|
| 99 | * </pre> |
---|
| 100 | * |
---|
| 101 | * @return the true positive rate |
---|
| 102 | */ |
---|
| 103 | public double getTruePositiveRate() { |
---|
| 104 | if (0 == (m_TruePos + m_FalseNeg)) { |
---|
| 105 | return 0; |
---|
| 106 | } else { |
---|
| 107 | return m_TruePos / (m_TruePos + m_FalseNeg); |
---|
| 108 | } |
---|
| 109 | } |
---|
| 110 | |
---|
| 111 | /** |
---|
| 112 | * Calculate the false positive rate. |
---|
| 113 | * This is defined as<p> |
---|
| 114 | * <pre> |
---|
| 115 | * incorrectly classified negatives |
---|
| 116 | * -------------------------------- |
---|
| 117 | * total negatives |
---|
| 118 | * </pre> |
---|
| 119 | * |
---|
| 120 | * @return the false positive rate |
---|
| 121 | */ |
---|
| 122 | public double getFalsePositiveRate() { |
---|
| 123 | if (0 == (m_FalsePos + m_TrueNeg)) { |
---|
| 124 | return 0; |
---|
| 125 | } else { |
---|
| 126 | return m_FalsePos / (m_FalsePos + m_TrueNeg); |
---|
| 127 | } |
---|
| 128 | } |
---|
| 129 | |
---|
| 130 | /** |
---|
| 131 | * Calculate the precision. |
---|
| 132 | * This is defined as<p> |
---|
| 133 | * <pre> |
---|
| 134 | * correctly classified positives |
---|
| 135 | * ------------------------------ |
---|
| 136 | * total predicted as positive |
---|
| 137 | * </pre> |
---|
| 138 | * |
---|
| 139 | * @return the precision |
---|
| 140 | */ |
---|
| 141 | public double getPrecision() { |
---|
| 142 | if (0 == (m_TruePos + m_FalsePos)) { |
---|
| 143 | return 0; |
---|
| 144 | } else { |
---|
| 145 | return m_TruePos / (m_TruePos + m_FalsePos); |
---|
| 146 | } |
---|
| 147 | } |
---|
| 148 | |
---|
| 149 | /** |
---|
| 150 | * Calculate the recall. |
---|
| 151 | * This is defined as<p> |
---|
| 152 | * <pre> |
---|
| 153 | * correctly classified positives |
---|
| 154 | * ------------------------------ |
---|
| 155 | * total positives |
---|
| 156 | * </pre><p> |
---|
| 157 | * (Which is also the same as the truePositiveRate.) |
---|
| 158 | * |
---|
| 159 | * @return the recall |
---|
| 160 | */ |
---|
| 161 | public double getRecall() { return getTruePositiveRate(); } |
---|
| 162 | |
---|
| 163 | /** |
---|
| 164 | * Calculate the F-Measure. |
---|
| 165 | * This is defined as<p> |
---|
| 166 | * <pre> |
---|
| 167 | * 2 * recall * precision |
---|
| 168 | * ---------------------- |
---|
| 169 | * recall + precision |
---|
| 170 | * </pre> |
---|
| 171 | * |
---|
| 172 | * @return the F-Measure |
---|
| 173 | */ |
---|
| 174 | public double getFMeasure() { |
---|
| 175 | |
---|
| 176 | double precision = getPrecision(); |
---|
| 177 | double recall = getRecall(); |
---|
| 178 | if ((precision + recall) == 0) { |
---|
| 179 | return 0; |
---|
| 180 | } |
---|
| 181 | return 2 * precision * recall / (precision + recall); |
---|
| 182 | } |
---|
| 183 | |
---|
| 184 | /** |
---|
| 185 | * Calculate the fallout. |
---|
| 186 | * This is defined as<p> |
---|
| 187 | * <pre> |
---|
| 188 | * incorrectly classified negatives |
---|
| 189 | * -------------------------------- |
---|
| 190 | * total predicted as positive |
---|
| 191 | * </pre> |
---|
| 192 | * |
---|
| 193 | * @return the fallout |
---|
| 194 | */ |
---|
| 195 | public double getFallout() { |
---|
| 196 | if (0 == (m_TruePos + m_FalsePos)) { |
---|
| 197 | return 0; |
---|
| 198 | } else { |
---|
| 199 | return m_FalsePos / (m_TruePos + m_FalsePos); |
---|
| 200 | } |
---|
| 201 | } |
---|
| 202 | |
---|
| 203 | /** |
---|
| 204 | * Generates a <code>ConfusionMatrix</code> representing the current |
---|
| 205 | * two-class statistics, using class names "negative" and "positive". |
---|
| 206 | * |
---|
| 207 | * @return a <code>ConfusionMatrix</code>. |
---|
| 208 | */ |
---|
| 209 | public ConfusionMatrix getConfusionMatrix() { |
---|
| 210 | |
---|
| 211 | ConfusionMatrix cm = new ConfusionMatrix(CATEGORY_NAMES); |
---|
| 212 | cm.setElement(0, 0, m_TrueNeg); |
---|
| 213 | cm.setElement(0, 1, m_FalsePos); |
---|
| 214 | cm.setElement(1, 0, m_FalseNeg); |
---|
| 215 | cm.setElement(1, 1, m_TruePos); |
---|
| 216 | return cm; |
---|
| 217 | } |
---|
| 218 | |
---|
| 219 | /** |
---|
| 220 | * Returns a string containing the various performance measures |
---|
| 221 | * for the current object |
---|
| 222 | */ |
---|
| 223 | public String toString() { |
---|
| 224 | |
---|
| 225 | StringBuffer res = new StringBuffer(); |
---|
| 226 | res.append(getTruePositive()).append(' '); |
---|
| 227 | res.append(getFalseNegative()).append(' '); |
---|
| 228 | res.append(getTrueNegative()).append(' '); |
---|
| 229 | res.append(getFalsePositive()).append(' '); |
---|
| 230 | res.append(getFalsePositiveRate()).append(' '); |
---|
| 231 | res.append(getTruePositiveRate()).append(' '); |
---|
| 232 | res.append(getPrecision()).append(' '); |
---|
| 233 | res.append(getRecall()).append(' '); |
---|
| 234 | res.append(getFMeasure()).append(' '); |
---|
| 235 | res.append(getFallout()).append(' '); |
---|
| 236 | return res.toString(); |
---|
| 237 | } |
---|
| 238 | |
---|
| 239 | /** |
---|
| 240 | * Returns the revision string. |
---|
| 241 | * |
---|
| 242 | * @return the revision |
---|
| 243 | */ |
---|
| 244 | public String getRevision() { |
---|
| 245 | return RevisionUtils.extract("$Revision: 1.9 $"); |
---|
| 246 | } |
---|
| 247 | } |
---|