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 org.jdmp.core.algorithm.classification.AbstractClassifier;
027    import org.jdmp.core.algorithm.classification.Classifier;
028    import org.jdmp.core.dataset.ClassificationDataSet;
029    import org.jdmp.core.dataset.RegressionDataSet;
030    import org.jdmp.core.sample.Sample;
031    import org.ujmp.core.Matrix;
032    import org.ujmp.core.MatrixFactory;
033    import org.ujmp.core.calculation.Calculation.Ret;
034    
035    public class SemiSupervisedEM extends AbstractClassifier implements SemiSupervisedClassifier {
036            private static final long serialVersionUID = 7362798845466035645L;
037    
038            private int iterations = 10;
039    
040            private Classifier classifier = null;
041    
042            private boolean useRawPrediction = false;
043    
044            public SemiSupervisedEM(Classifier singleClassClassifier, boolean useRawPrediction) {
045                    super();
046                    setLabel("SemiSupervisedEM [" + singleClassClassifier.toString() + "]");
047                    this.classifier = singleClassClassifier;
048                    this.useRawPrediction = useRawPrediction;
049            }
050    
051            @Override
052            public Matrix predict(Matrix input, Matrix sampleWeight) throws Exception {
053                    return classifier.predict(input, sampleWeight);
054            }
055    
056            @Override
057            public void reset() throws Exception {
058                    classifier.reset();
059            }
060    
061            public void train(RegressionDataSet dataSet) throws Exception {
062                    throw new Exception("use train(labeledData,unlabeledData)");
063            }
064    
065            public void train(RegressionDataSet labeledData, RegressionDataSet unlabeledData)
066                            throws Exception {
067                    int classCount = ((ClassificationDataSet) labeledData).getClassCount();
068                    System.out.println("Step 0");
069                    classifier.reset();
070                    classifier.train(labeledData);
071                    classifier.predict(unlabeledData);
072                    for (Sample s : unlabeledData.getSamples()) {
073                            Matrix predicted = s.getMatrix(Sample.PREDICTED);
074                            if (useRawPrediction) {
075                                    s.setMatrix(Sample.TARGET, predicted);
076                            } else {
077                                    int max = (int) predicted.indexOfMax(Ret.NEW, Matrix.COLUMN).getAsDouble(0, 0);
078                                    Matrix target = MatrixFactory.zeros(1, classCount);
079                                    target.setAsDouble(1.0, 0, max);
080                                    s.setMatrix(Sample.TARGET, target);
081                            }
082                    }
083                    ClassificationDataSet completeData = new ClassificationDataSet();
084                    completeData.getSamples().addAll(labeledData.getSamples().toCollection());
085                    completeData.getSamples().addAll(unlabeledData.getSamples().toCollection());
086                    classifier.reset();
087                    classifier.train(completeData);
088    
089                    for (int i = 0; i < iterations; i++) {
090                            System.out.println("Step " + (i + 1));
091                            classifier.predict(unlabeledData);
092                            for (Sample s : unlabeledData.getSamples()) {
093                                    Matrix predicted = s.getMatrix(Sample.PREDICTED);
094                                    int max = (int) predicted.indexOfMax(Ret.NEW, Matrix.COLUMN).getAsDouble(0, 0);
095                                    Matrix target = MatrixFactory.zeros(1, classCount);
096                                    target.setAsDouble(1.0, 0, max);
097                                    s.setMatrix(Sample.TARGET, target);
098                            }
099                            completeData = new ClassificationDataSet();
100                            completeData.getSamples().addAll(labeledData.getSamples().toCollection());
101                            completeData.getSamples().addAll(unlabeledData.getSamples().toCollection());
102                            classifier.reset();
103                            classifier.train(completeData);
104                    }
105            }
106    
107            public Classifier emptyCopy() throws Exception {
108                    return new SemiSupervisedEM(classifier.emptyCopy(), useRawPrediction);
109            }
110    
111            @Override
112            public void train(Matrix input, Matrix sampleWeight, Matrix targetOutput) throws Exception {
113                    throw new Exception("not supported");
114            }
115    
116    }