[8815] | 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 | * Classifier.java
|
---|
| 19 | * Copyright (C) 1999 Eibe Frank, Len Trigg
|
---|
| 20 | *
|
---|
| 21 | */
|
---|
| 22 |
|
---|
| 23 | package weka.classifiers;
|
---|
| 24 |
|
---|
| 25 | import java.io.Serializable;
|
---|
| 26 | import weka.core.Instance;
|
---|
| 27 | import weka.core.Instances;
|
---|
| 28 | import weka.core.SerializedObject;
|
---|
| 29 | import weka.core.Utils;
|
---|
| 30 |
|
---|
| 31 |
|
---|
| 32 | /**
|
---|
| 33 | * Abstract classifier. All schemes for numeric or nominal prediction in
|
---|
| 34 | * Weka extend this class.
|
---|
| 35 | *
|
---|
| 36 | * @author Eibe Frank ([email protected])
|
---|
| 37 | * @author Len Trigg ([email protected])
|
---|
| 38 | * @version $Revision: 8815 $
|
---|
| 39 | */
|
---|
| 40 | public abstract class Classifier implements Cloneable, Serializable {
|
---|
| 41 |
|
---|
| 42 | /**
|
---|
| 43 | * Generates a classifier. Must initialize all fields of the classifier
|
---|
| 44 | * that are not being set via options (ie. multiple calls of buildClassifier
|
---|
| 45 | * must always lead to the same result). Must not change the dataset
|
---|
| 46 | * in any way.
|
---|
| 47 | *
|
---|
| 48 | * @param data set of instances serving as training data
|
---|
| 49 | * @exception Exception if the classifier has not been
|
---|
| 50 | * generated successfully
|
---|
| 51 | */
|
---|
| 52 | public abstract void buildClassifier(Instances data) throws Exception;
|
---|
| 53 |
|
---|
| 54 | /**
|
---|
| 55 | * Classifies a given instance.
|
---|
| 56 | *
|
---|
| 57 | * @param instance the instance to be classified
|
---|
| 58 | * @return index of the predicted class as a double
|
---|
| 59 | * if the class is nominal, otherwise the predicted value
|
---|
| 60 | * @exception Exception if instance could not be classified
|
---|
| 61 | * successfully
|
---|
| 62 | */
|
---|
| 63 | public abstract double classifyInstance(Instance instance) throws Exception;
|
---|
| 64 |
|
---|
| 65 |
|
---|
| 66 |
|
---|
| 67 | /**
|
---|
| 68 | * Creates a new instance of a classifier given it's class name and
|
---|
| 69 | * (optional) arguments to pass to it's setOptions method. If the
|
---|
| 70 | * classifier implements OptionHandler and the options parameter is
|
---|
| 71 | * non-null, the classifier will have it's options set.
|
---|
| 72 | *
|
---|
| 73 | * @param classifierName the fully qualified class name of the classifier
|
---|
| 74 | * @param options an array of options suitable for passing to setOptions. May
|
---|
| 75 | * be null.
|
---|
| 76 | * @return the newly created classifier, ready for use.
|
---|
| 77 | * @exception Exception if the classifier name is invalid, or the options
|
---|
| 78 | * supplied are not acceptable to the classifier
|
---|
| 79 | */
|
---|
| 80 | public static Classifier forName(String classifierName,
|
---|
| 81 | String [] options) throws Exception {
|
---|
| 82 |
|
---|
| 83 | return (Classifier)Utils.forName(Classifier.class,
|
---|
| 84 | classifierName,
|
---|
| 85 | options);
|
---|
| 86 | }
|
---|
| 87 |
|
---|
| 88 | /**
|
---|
| 89 | * Creates copies of the current classifier, which can then
|
---|
| 90 | * be used for boosting etc. Note that this method now uses
|
---|
| 91 | * Serialization to perform a deep copy, so the Classifier
|
---|
| 92 | * object must be fully Serializable. Any currently built model
|
---|
| 93 | * will now be copied as well.
|
---|
| 94 | *
|
---|
| 95 | * @param model an example classifier to copy
|
---|
| 96 | * @param num the number of classifiers copies to create.
|
---|
| 97 | * @return an array of classifiers.
|
---|
| 98 | * @exception Exception if an error occurs
|
---|
| 99 | */
|
---|
| 100 | public static Classifier [] makeCopies(Classifier model,
|
---|
| 101 | int num) throws Exception {
|
---|
| 102 |
|
---|
| 103 | if (model == null) {
|
---|
| 104 | throw new Exception("No model classifier set");
|
---|
| 105 | }
|
---|
| 106 | Classifier [] classifiers = new Classifier [num];
|
---|
| 107 | SerializedObject so = new SerializedObject(model);
|
---|
| 108 | for(int i = 0; i < classifiers.length; i++) {
|
---|
| 109 | classifiers[i] = (Classifier) so.getObject();
|
---|
| 110 | }
|
---|
| 111 | return classifiers;
|
---|
| 112 | }
|
---|
| 113 |
|
---|
| 114 | }
|
---|
| 115 |
|
---|