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.core.algorithm.classification.meta;
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.DataSetFactory;
033    import org.jdmp.core.dataset.RegressionDataSet;
034    import org.ujmp.core.Matrix;
035    import org.ujmp.core.MatrixFactory;
036    import org.ujmp.core.calculation.Calculation.Ret;
037    
038    public class MultiClassClassifier extends AbstractClassifier {
039            private static final long serialVersionUID = 466059743021340944L;
040    
041            private Classifier singleClassClassifier = null;
042    
043            private int classCount = 0;
044    
045            private boolean twoColumns = false;
046    
047            private final List<Classifier> singleClassClassifiers = new ArrayList<Classifier>();
048    
049            public MultiClassClassifier(Classifier singleClassClassifier, boolean twoColumns) {
050                    super();
051                    setLabel("MultiClassClassifier [" + singleClassClassifier.toString() + "]");
052                    this.singleClassClassifier = singleClassClassifier;
053                    this.twoColumns = twoColumns;
054            }
055    
056            @Override
057            public Matrix predict(Matrix input, Matrix sampleWeight) throws Exception {
058                    double[] results = new double[classCount];
059                    for (int i = 0; i < classCount; i++) {
060                            Classifier c = singleClassClassifiers.get(i);
061                            results[i] = c.predict(input, sampleWeight).getAsDouble(0, 0);
062                    }
063                    return MatrixFactory.linkToArray(results).transpose();
064            }
065    
066            @Override
067            public void reset() throws Exception {
068                    singleClassClassifiers.clear();
069            }
070    
071            @Override
072            public void train(RegressionDataSet dataSet) throws Exception {
073                    reset();
074                    classCount = ((ClassificationDataSet) dataSet).getClassCount();
075    
076                    for (int i = 0; i < classCount; i++) {
077                            System.out.println("Training class " + i);
078                            Classifier c = singleClassClassifier.emptyCopy();
079                            singleClassClassifiers.add(c);
080                            Matrix input = dataSet.getInputMatrix();
081                            Matrix target = dataSet.getTargetMatrix().selectColumns(Ret.LINK, i);
082                            if (twoColumns) {
083                                    Matrix target2 = target.minus(1).abs(Ret.NEW);
084                                    target = MatrixFactory.horCat(target, target2);
085                            }
086                            ClassificationDataSet ds = DataSetFactory.linkToMatrix(input, target);
087                            c.train(ds);
088                    }
089    
090            }
091    
092            @Override
093            public void train(Matrix input, Matrix sampleWeight, Matrix targetOutput) throws Exception {
094                    throw new Exception("not supported");
095            }
096    
097            public Classifier emptyCopy() {
098                    return new MultiClassClassifier(singleClassClassifier, twoColumns);
099            }
100    }