[29] | 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 | * OSDL.java |
---|
| 19 | * Copyright (C) 2004 Stijn Lievens |
---|
| 20 | * |
---|
| 21 | */ |
---|
| 22 | |
---|
| 23 | package weka.classifiers.misc; |
---|
| 24 | |
---|
| 25 | import weka.classifiers.misc.monotone.OSDLCore; |
---|
| 26 | import weka.core.Attribute; |
---|
| 27 | import weka.core.Instance; |
---|
| 28 | import weka.core.RevisionUtils; |
---|
| 29 | import weka.core.Utils; |
---|
| 30 | |
---|
| 31 | /** |
---|
| 32 | <!-- globalinfo-start --> |
---|
| 33 | * This class is an implementation of the Ordinal Stochastic Dominance Learner.<br/> |
---|
| 34 | * Further information regarding the OSDL-algorithm can be found in:<br/> |
---|
| 35 | * <br/> |
---|
| 36 | * S. Lievens, B. De Baets, K. Cao-Van (2006). A Probabilistic Framework for the Design of Instance-Based Supervised Ranking Algorithms in an Ordinal Setting. Annals of Operations Research..<br/> |
---|
| 37 | * <br/> |
---|
| 38 | * Kim Cao-Van (2003). Supervised ranking: from semantics to algorithms.<br/> |
---|
| 39 | * <br/> |
---|
| 40 | * Stijn Lievens (2004). Studie en implementatie van instantie-gebaseerde algoritmen voor gesuperviseerd rangschikken.<br/> |
---|
| 41 | * <br/> |
---|
| 42 | * For more information about supervised ranking, see<br/> |
---|
| 43 | * <br/> |
---|
| 44 | * http://users.ugent.be/~slievens/supervised_ranking.php |
---|
| 45 | * <p/> |
---|
| 46 | <!-- globalinfo-end --> |
---|
| 47 | * |
---|
| 48 | <!-- technical-bibtex-start --> |
---|
| 49 | * BibTeX: |
---|
| 50 | * <pre> |
---|
| 51 | * @article{Lievens2006, |
---|
| 52 | * author = {S. Lievens and B. De Baets and K. Cao-Van}, |
---|
| 53 | * journal = {Annals of Operations Research}, |
---|
| 54 | * title = {A Probabilistic Framework for the Design of Instance-Based Supervised Ranking Algorithms in an Ordinal Setting}, |
---|
| 55 | * year = {2006} |
---|
| 56 | * } |
---|
| 57 | * |
---|
| 58 | * @phdthesis{Cao-Van2003, |
---|
| 59 | * author = {Kim Cao-Van}, |
---|
| 60 | * school = {Ghent University}, |
---|
| 61 | * title = {Supervised ranking: from semantics to algorithms}, |
---|
| 62 | * year = {2003} |
---|
| 63 | * } |
---|
| 64 | * |
---|
| 65 | * @mastersthesis{Lievens2004, |
---|
| 66 | * author = {Stijn Lievens}, |
---|
| 67 | * school = {Ghent University}, |
---|
| 68 | * title = {Studie en implementatie van instantie-gebaseerde algoritmen voor gesuperviseerd rangschikken}, |
---|
| 69 | * year = {2004} |
---|
| 70 | * } |
---|
| 71 | * </pre> |
---|
| 72 | * <p/> |
---|
| 73 | <!-- technical-bibtex-end --> |
---|
| 74 | * |
---|
| 75 | <!-- options-start --> |
---|
| 76 | * Valid options are: <p/> |
---|
| 77 | * |
---|
| 78 | * <pre> -D |
---|
| 79 | * If set, classifier is run in debug mode and |
---|
| 80 | * may output additional info to the console</pre> |
---|
| 81 | * |
---|
| 82 | * <pre> -C <REG|WSUM|MAX|MED|RMED> |
---|
| 83 | * Sets the classification type to be used. |
---|
| 84 | * (Default: MED)</pre> |
---|
| 85 | * |
---|
| 86 | * <pre> -B |
---|
| 87 | * Use the balanced version of the Ordinal Stochastic Dominance Learner</pre> |
---|
| 88 | * |
---|
| 89 | * <pre> -W |
---|
| 90 | * Use the weighted version of the Ordinal Stochastic Dominance Learner</pre> |
---|
| 91 | * |
---|
| 92 | * <pre> -S <value of interpolation parameter> |
---|
| 93 | * Sets the value of the interpolation parameter (not with -W/T/P/L/U) |
---|
| 94 | * (default: 0.5).</pre> |
---|
| 95 | * |
---|
| 96 | * <pre> -T |
---|
| 97 | * Tune the interpolation parameter (not with -W/S) |
---|
| 98 | * (default: off)</pre> |
---|
| 99 | * |
---|
| 100 | * <pre> -L <Lower bound for interpolation parameter> |
---|
| 101 | * Lower bound for the interpolation parameter (not with -W/S) |
---|
| 102 | * (default: 0)</pre> |
---|
| 103 | * |
---|
| 104 | * <pre> -U <Upper bound for interpolation parameter> |
---|
| 105 | * Upper bound for the interpolation parameter (not with -W/S) |
---|
| 106 | * (default: 1)</pre> |
---|
| 107 | * |
---|
| 108 | * <pre> -P <Number of parts> |
---|
| 109 | * Determines the step size for tuning the interpolation |
---|
| 110 | * parameter, nl. (U-L)/P (not with -W/S) |
---|
| 111 | * (default: 10)</pre> |
---|
| 112 | * |
---|
| 113 | <!-- options-end --> |
---|
| 114 | * |
---|
| 115 | * More precisely, this is a simple extension of the OSDLCore class, |
---|
| 116 | * so that the OSDLCore class can be used within the WEKA environment. |
---|
| 117 | * The problem with OSDLCore is that it implements both |
---|
| 118 | * <code> classifyInstance </code> and <code> distributionForInstance </code> |
---|
| 119 | * in a non trivial way. |
---|
| 120 | * <p> |
---|
| 121 | * One can evaluate a model easily with the method <code> evaluateModel </code> |
---|
| 122 | * from the <code> Evaluation </code> class. However, for nominal classes |
---|
| 123 | * they do the following: they use <code> distributionForInstance </code> |
---|
| 124 | * and then pick the class with maximal probability. This procedure |
---|
| 125 | * is <b> not </b> valid for a ranking algorithm, since this destroys |
---|
| 126 | * the required monotonicity property. |
---|
| 127 | * </p> |
---|
| 128 | * <p> |
---|
| 129 | * This class reimplements <code> distributionForInstance </code> in the |
---|
| 130 | * following way: first <code> classifyInstance </code> of |
---|
| 131 | * <code> OSDLCore </code> is used and the chosen label then gets |
---|
| 132 | * assigned probability one. This ensures that the classification |
---|
| 133 | * accuracy is calculated correctly, but possibly some other statistics |
---|
| 134 | * are no longer meaningful. |
---|
| 135 | * </p> |
---|
| 136 | * |
---|
| 137 | * @author Stijn Lievens (stijn.lievens@ugent.be) |
---|
| 138 | * @version $Revision: 5987 $ |
---|
| 139 | */ |
---|
| 140 | public class OSDL |
---|
| 141 | extends OSDLCore { |
---|
| 142 | |
---|
| 143 | /** for serialization */ |
---|
| 144 | private static final long serialVersionUID = -4534219825732505381L; |
---|
| 145 | |
---|
| 146 | /** |
---|
| 147 | * Use <code> classifyInstance </code> from <code> OSDLCore </code> and |
---|
| 148 | * assign probability one to the chosen label. |
---|
| 149 | * The implementation is heavily based on the same method in |
---|
| 150 | * the <code> Classifier </code> class. |
---|
| 151 | * |
---|
| 152 | * @param instance the instance to be classified |
---|
| 153 | * @return an array containing a single '1' on the index |
---|
| 154 | * that <code> classifyInstance </code> returns. |
---|
| 155 | */ |
---|
| 156 | public double[] distributionForInstance(Instance instance) { |
---|
| 157 | |
---|
| 158 | // based on the code from the Classifier class |
---|
| 159 | double[] dist = new double[instance.numClasses()]; |
---|
| 160 | int classification = 0; |
---|
| 161 | switch (instance.classAttribute().type()) { |
---|
| 162 | case Attribute.NOMINAL: |
---|
| 163 | try { |
---|
| 164 | classification = |
---|
| 165 | (int) Math.round(classifyInstance(instance)); |
---|
| 166 | } catch (Exception e) { |
---|
| 167 | System.out.println("There was a problem with classifyIntance"); |
---|
| 168 | System.out.println(e.getMessage()); |
---|
| 169 | e.printStackTrace(); |
---|
| 170 | } |
---|
| 171 | if (Utils.isMissingValue(classification)) { |
---|
| 172 | return dist; |
---|
| 173 | } |
---|
| 174 | dist[classification] = 1.0; |
---|
| 175 | return dist; |
---|
| 176 | |
---|
| 177 | case Attribute.NUMERIC: |
---|
| 178 | try { |
---|
| 179 | dist[0] = classifyInstance(instance); |
---|
| 180 | } catch (Exception e) { |
---|
| 181 | System.out.println("There was a problem with classifyIntance"); |
---|
| 182 | System.out.println(e.getMessage()); |
---|
| 183 | e.printStackTrace(); |
---|
| 184 | } |
---|
| 185 | return dist; |
---|
| 186 | |
---|
| 187 | default: |
---|
| 188 | return dist; |
---|
| 189 | } |
---|
| 190 | } |
---|
| 191 | |
---|
| 192 | /** |
---|
| 193 | * Returns the revision string. |
---|
| 194 | * |
---|
| 195 | * @return the revision |
---|
| 196 | */ |
---|
| 197 | public String getRevision() { |
---|
| 198 | return RevisionUtils.extract("$Revision: 5987 $"); |
---|
| 199 | } |
---|
| 200 | |
---|
| 201 | /** |
---|
| 202 | * Main method for testing this class and for using it from the |
---|
| 203 | * command line. |
---|
| 204 | * |
---|
| 205 | * @param args array of options for both the classifier <code> |
---|
| 206 | * OSDL </code> and for <code> evaluateModel </code> |
---|
| 207 | */ |
---|
| 208 | public static void main(String[] args) { |
---|
| 209 | runClassifier(new OSDL(), args); |
---|
| 210 | } |
---|
| 211 | } |
---|