001    /*
002     * Copyright (C) 2008-2009 Holger Arndt, A. Naegele and M. Bundschus
003     *
004     * This file is part of the Java Data Mining Package (JDMP).
005     * See the NOTICE file distributed with this work for additional
006     * information regarding copyright ownership and licensing.
007     *
008     * JDMP is free software; you can redistribute it and/or modify
009     * it under the terms of the GNU Lesser General Public License as
010     * published by the Free Software Foundation; either version 2
011     * of the License, or (at your option) any later version.
012     *
013     * JDMP is distributed in the hope that it will be useful,
014     * but WITHOUT ANY WARRANTY; without even the implied warranty of
015     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
016     * GNU Lesser General Public License for more details.
017     *
018     * You should have received a copy of the GNU Lesser General Public
019     * License along with JDMP; if not, write to the
020     * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
021     * Boston, MA  02110-1301  USA
022     */
023    
024    package org.jdmp.weka.classifier;
025    
026    import java.lang.reflect.Constructor;
027    import java.util.logging.Level;
028    
029    import org.jdmp.core.algorithm.classification.AbstractClassifier;
030    import org.jdmp.core.dataset.RegressionDataSet;
031    import org.jdmp.weka.wrappers.DataSetToInstancesWrapper;
032    import org.jdmp.weka.wrappers.SampleToInstanceWrapper;
033    import org.ujmp.core.Matrix;
034    import org.ujmp.core.MatrixFactory;
035    
036    import weka.classifiers.Classifier;
037    import weka.core.Instance;
038    import weka.core.Instances;
039    
040    public class WekaClassifier extends AbstractClassifier {
041            private static final long serialVersionUID = 29290678735702499L;
042    
043            public enum WekaClassifierType {
044                    AODE, BayesNet, ComplementNaiveBayes, HNB, NaiveBayes, NaiveBayesMultinomial, NaiveBayesMultinomialUpdateable, NaiveBayesSimple, NaiveBayesUpdateable, WAODE, GaussianProcesses, IsotonicRegression, LeastMedSq, LinearRegression, Logistic, MultilayerPerceptron, PaceRegression, PLSClassifier, RBFNetwork, SimpleLinearRegression, SimpleLogistic, SMO, SMOreg, SVMreg, VotedPerceptron, Winnow, IB1, IBk, KStar, LBR, LWL, AdaBoostM1, AdditiveRegression, AttributeSelectedClassifier, Bagging, ClassificationViaRegression, CostSensitiveClassifier, CVParameterSelection, Dagging, Decorate, END, EnsembleSelection, FilteredClassifier, Grading, GridSearch, LogitBoost, MetaCost, MultiBoostAB, MultiClassClassifier, MultiScheme, OrdinalClassClassifier, RacedIncrementalLogitBoost, RandomCommittee, RandomSubSpace, RegressionByDiscretization, Stacking, StackingC, ThresholdSelector, Vote, CitationKNN, MDD, MIBoost, MIDD, MIEMDD, MILR, MINND, MIOptimalBall, MISMO, MISVM, MIWrapper, SimpleMI, TLD, TLDSimple, FLR, HyperPipes, MinMaxExtension, OLM, OSDL, SerializedClassifier, VFI, ConjunctiveRule, DecisionTable, JRip, M5Rules, NNge, OneR, PART, Prism, Ridor, ZeroR, ADTree, BFTree, DecisionStump, Id3, J48, LMT, M5P, NBTree, RandomForest, RandomTree, REPTree, SimpleCart
045            };
046    
047            private Classifier wekaClassifier = null;
048    
049            private Instances instances = null;
050    
051            private WekaClassifierType classifierName = null;
052    
053            private String[] options = null;
054    
055            private boolean discrete = false;
056    
057            public WekaClassifier(WekaClassifierType classifierName, boolean discrete, String... options)
058                            throws Exception {
059                    setLabel("Weka-" + classifierName);
060                    this.classifierName = classifierName;
061                    this.options = options;
062                    this.discrete = discrete;
063                    createAlgorithm();
064            }
065    
066            public void createAlgorithm() throws Exception {
067                    Class<?> c = null;
068                    if (c == null) {
069                            try {
070                                    c = Class.forName(classifierName.name());
071                            } catch (ClassNotFoundException e) {
072                            }
073                    }
074                    if (c == null) {
075                            try {
076                                    c = Class.forName("weka.classifiers." + classifierName.name());
077                            } catch (ClassNotFoundException e) {
078                            }
079                    }
080                    if (c == null) {
081                            try {
082                                    c = Class.forName("weka.classifiers.bayes." + classifierName.name());
083                            } catch (ClassNotFoundException e) {
084                            }
085                    }
086                    if (c == null) {
087                            try {
088                                    c = Class.forName("weka.classifiers.functions." + classifierName);
089                            } catch (ClassNotFoundException e) {
090                            }
091                    }
092                    if (c == null) {
093                            try {
094                                    c = Class.forName("weka.classifiers.lazy." + classifierName);
095                            } catch (ClassNotFoundException e) {
096                            }
097                    }
098                    if (c == null) {
099                            try {
100                                    c = Class.forName("weka.classifiers.meta." + classifierName);
101                            } catch (ClassNotFoundException e) {
102                            }
103                    }
104                    if (c == null) {
105                            try {
106                                    c = Class.forName("weka.classifiers.mi." + classifierName);
107                            } catch (ClassNotFoundException e) {
108                            }
109                    }
110                    if (c == null) {
111                            try {
112                                    c = Class.forName("weka.classifiers.misc." + classifierName);
113                            } catch (ClassNotFoundException e) {
114                            }
115                    }
116                    if (c == null) {
117                            try {
118                                    c = Class.forName("weka.classifiers.trees." + classifierName);
119                            } catch (ClassNotFoundException e) {
120                            }
121                    }
122                    if (c == null) {
123                            try {
124                                    c = Class.forName("weka.classifiers.rules." + classifierName);
125                            } catch (ClassNotFoundException e) {
126                            }
127                    }
128                    if (c == null) {
129                            throw new ClassNotFoundException("class not found: " + classifierName);
130                    } else {
131                            Constructor<?> constr = c.getConstructor(new Class[] {});
132                            wekaClassifier = (Classifier) constr.newInstance(new Object[] {});
133                            if (options != null || options.length != 0) {
134                                    wekaClassifier.setOptions(options);
135                            }
136                    }
137            }
138    
139            @Override
140            public Matrix predict(Matrix input, Matrix weight) throws Exception {
141                    double[] probabilities = null;
142                    Instance instance = new SampleToInstanceWrapper(input, weight, null, discrete, true);
143                    instance.setDataset(instances);
144                    probabilities = wekaClassifier.distributionForInstance(instance);
145                    double[][] v = new double[1][];
146                    v[0] = probabilities;
147                    Matrix output = MatrixFactory.linkToArray(v);
148                    return output;
149            }
150    
151            @Override
152            public void train(RegressionDataSet dataSet) throws Exception {
153                    instances = new DataSetToInstancesWrapper(dataSet, discrete, true);
154                    wekaClassifier.buildClassifier(instances);
155            }
156    
157            @Override
158            public void train(Matrix input, Matrix sampleWeight, Matrix targetOutput) {
159                    logger.log(Level.WARNING, "this method is not supported for WEKA classifiers");
160            }
161    
162            @Override
163            public void reset() throws Exception {
164                    createAlgorithm();
165            }
166    
167            public org.jdmp.core.algorithm.classification.Classifier emptyCopy() throws Exception {
168                    return new WekaClassifier(classifierName, discrete);
169            }
170    
171    }