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.mallet.classifier;
025
026 import java.util.ArrayList;
027 import java.util.List;
028
029 import org.jdmp.core.algorithm.classification.AbstractClassifier;
030 import org.jdmp.core.algorithm.classification.Classifier;
031 import org.jdmp.core.dataset.ClassificationDataSet;
032 import org.jdmp.core.dataset.RegressionDataSet;
033 import org.jdmp.mallet.dataset.DataSet2InstanceList;
034 import org.jdmp.mallet.matrix.Labeling2Matrix;
035 import org.jdmp.mallet.sample.Sample2Instance;
036 import org.ujmp.core.Matrix;
037 import org.ujmp.core.calculation.Calculation.Ret;
038 import org.ujmp.core.exceptions.MatrixException;
039
040 import cc.mallet.classify.AdaBoostM2Trainer;
041 import cc.mallet.classify.AdaBoostTrainer;
042 import cc.mallet.classify.BalancedWinnowTrainer;
043 import cc.mallet.classify.Classification;
044 import cc.mallet.classify.ClassifierTrainer;
045 import cc.mallet.classify.DecisionTreeTrainer;
046 import cc.mallet.classify.NaiveBayesTrainer;
047 import cc.mallet.types.Instance;
048 import cc.mallet.types.InstanceList;
049 import cc.mallet.types.LabelAlphabet;
050
051 public class MalletClassifier extends AbstractClassifier {
052 private static final long serialVersionUID = -1594522916901093940L;
053
054 public enum MalletClassifiers {
055 NaiveBayes, AdaBoost, AdaBoostM2, Bagging, BalancedWinnow, C45, ConfidencePredictingClassifier, DecisionTree, MaxEnt, MCMaxEnt, Winnow
056 };
057
058 private MalletClassifiers type = null;
059
060 private ClassifierTrainer<?> trainer = null;
061
062 private List<Integer> cumSum = null;
063
064 private cc.mallet.classify.Classifier classifier = null;
065
066 public MalletClassifier(MalletClassifiers classifier) throws Exception {
067 super();
068 setLabel("Mallet Classifier " + classifier.name());
069 this.type = classifier;
070 reset();
071 }
072
073 @Override
074 public Matrix predict(Matrix input, Matrix sampleWeight) throws Exception {
075 Instance instance = new Sample2Instance(input, null, classifier.getAlphabet(), classifier
076 .getLabelAlphabet(), classifier.getInstancePipe(), cumSum);
077 Classification c = classifier.classify(instance);
078 return new Labeling2Matrix(c.getLabeling());
079 }
080
081 @Override
082 public void reset() throws Exception {
083 switch (type) {
084 case NaiveBayes:
085 this.trainer = new NaiveBayesTrainer();
086 this.classifier = null;
087 break;
088 case AdaBoost:
089 this.trainer = new AdaBoostTrainer(new DecisionTreeTrainer());
090 this.classifier = null;
091 break;
092 case AdaBoostM2:
093 this.trainer = new AdaBoostM2Trainer(new DecisionTreeTrainer());
094 this.classifier = null;
095 break;
096 // case Bagging:
097 // this.trainer = new BaggingTrainer(new DecisionTreeTrainer());
098 // this.classifier = null;
099 // break;
100 case BalancedWinnow:
101 this.trainer = new BalancedWinnowTrainer();
102 this.classifier = null;
103 break;
104 // case ConfidencePredictingClassifier:
105 // this.trainer = new ConfidencePredictingClassifierTrainer(new
106 // DecisionTreeTrainer());
107 // this.classifier = null;
108 // break;
109 case DecisionTree:
110 this.trainer = new DecisionTreeTrainer();
111 this.classifier = null;
112 break;
113 default:
114 throw new MatrixException("not implemented");
115 }
116
117 }
118
119 @Override
120 public void train(RegressionDataSet dataSet) throws Exception {
121 Matrix dataSetInput = dataSet.getInputMatrix();
122
123 Matrix max = dataSetInput.max(Ret.NEW, Matrix.ROW);
124 cumSum = new ArrayList<Integer>((int) max.getColumnCount());
125 int sum = 0;
126 cumSum.add(sum);
127 for (int i = 0; i < max.getColumnCount(); i++) {
128 sum += max.getAsInt(0, i) + 1;
129 cumSum.add(sum);
130 }
131
132 LabelAlphabet inputAlphabet = new LabelAlphabet();
133 for (int i = 0; i < dataSet.getFeatureCount(); i++) {
134 // iterate from 1 to max (inclusive!)
135 for (int fv = 1; fv < max.getAsDouble(0, i) + 1; fv++) {
136 inputAlphabet.lookupIndex("Feature" + i + "=" + fv, true);
137 }
138 }
139
140 LabelAlphabet targetAlphabet = new LabelAlphabet();
141 for (int i = 0; i < dataSet.getTargetMatrix().getColumnCount(); i++) {
142 targetAlphabet.lookupIndex("Class" + i, true);
143 }
144
145 InstanceList trainingSet = new DataSet2InstanceList((ClassificationDataSet) dataSet,
146 inputAlphabet, targetAlphabet, cumSum);
147
148 classifier = trainer.train(trainingSet);
149 }
150
151 @Override
152 public void train(Matrix input, Matrix sampleWeight, Matrix targetOutput) throws Exception {
153 throw new MatrixException("not implemented");
154 }
155
156 public Classifier emptyCopy() throws Exception {
157 return new MalletClassifier(type);
158 }
159
160 }