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.regression;
025    
026    import org.jdmp.core.algorithm.AbstractAlgorithm;
027    import org.jdmp.core.algorithm.Algorithm;
028    import org.jdmp.core.algorithm.basic.Minus;
029    import org.jdmp.core.dataset.ClassificationDataSet;
030    import org.jdmp.core.dataset.RegressionDataSet;
031    import org.jdmp.core.sample.ClassificationSample;
032    import org.jdmp.core.sample.Sample;
033    import org.ujmp.core.Matrix;
034    import org.ujmp.core.MatrixFactory;
035    import org.ujmp.core.util.MathUtil;
036    
037    public abstract class AbstractRegressor extends AbstractAlgorithm implements Regressor {
038            private static final long serialVersionUID = 4674447558395794134L;
039    
040            public static final int OUTPUTERRORALGORITHM = 0;
041    
042            public static final int TRAIN = 0;
043    
044            public static final int PREDICT = 1;
045    
046            public int mode = TRAIN;
047    
048            private boolean evaluate = true;
049    
050            public AbstractRegressor() {
051                    super();
052                    setAlgorithm(OUTPUTERRORALGORITHM, new Minus());
053            }
054    
055            public AbstractRegressor(boolean evaluate) {
056                    this();
057                    this.evaluate = evaluate;
058            }
059    
060            public abstract void train(RegressionDataSet dataSet) throws Exception;
061    
062            public abstract void reset() throws Exception;
063    
064            public abstract void train(Matrix input, Matrix sampleWeight, Matrix targetOutput)
065                            throws Exception;
066    
067            public final void predict(Sample sample) throws Exception {
068                    Matrix predicted = predict(sample.getMatrix(INPUT), sample.getMatrix(WEIGHT));
069                    sample.setMatrix(PREDICTED, predicted);
070                    if (evaluate) {
071                            Matrix error = MathUtil.getMatrix(getOutputErrorAlgorithm().calculate(predicted,
072                                            MathUtil.getMatrix(sample.getMatrix(TARGET))).values().iterator().next());
073                            sample.setMatrix(DIFFERENCE, error);
074                            sample.setMatrix(RMSE, MatrixFactory.linkToValue(error.getRMS()));
075                    }
076                    sample.notifyGUIObject();
077            }
078    
079            public final Matrix predict(Matrix input) throws Exception {
080                    return predict(input.toRowVector(), null);
081            }
082    
083            public final void train(Matrix input, Matrix targetOutput) throws Exception {
084                    train(input, MatrixFactory.linkToValue(1.0), targetOutput);
085            }
086    
087            public abstract Matrix predict(Matrix input, Matrix sampleWeight) throws Exception;
088    
089            public final void train(Sample sample) throws Exception {
090                    Matrix input = sample.getMatrix(INPUT);
091                    Matrix weight = sample.getMatrix(WEIGHT);
092                    Matrix target = sample.getMatrix(TARGET);
093                    train(input, weight, target);
094            }
095    
096            public void predict(RegressionDataSet dataSet) throws Exception {
097    
098                    Matrix confusion = null;
099                    double error = 0.0;
100                    int correctCount = 0;
101                    int errorCount = 0;
102                    int classCount = 0;
103    
104                    if (dataSet instanceof ClassificationDataSet) {
105                            classCount = ((ClassificationDataSet) dataSet).getClassCount();
106                            confusion = MatrixFactory.zeros(classCount, classCount);
107                    }
108    
109                    for (Sample sample : dataSet.getSamples()) {
110    
111                            predict(sample);
112    
113                            if (evaluate) {
114                                    double rmse = sample.getMatrix(RMSE).getEuklideanValue();
115                                    error += Math.pow(rmse, 2.0);
116    
117                                    if ((sample instanceof ClassificationSample)) {
118    
119                                            int recognized = ((ClassificationSample) sample).getRecognizedClass();
120                                            int targetClass = ((ClassificationSample) sample).getTargetClass();
121    
122                                            if (classCount == 1 || recognized == -1) {
123                                                    confusion.setAsDouble(confusion.getAsDouble(0, 0) + 1, 0, 0);
124                                            } else {
125                                                    confusion.setAsDouble(confusion.getAsDouble(recognized, targetClass) + 1,
126                                                                    recognized, targetClass);
127                                            }
128    
129                                            if (((ClassificationSample) sample).isCorrect()) {
130                                                    correctCount++;
131                                                    // weight = weight * 0.99;
132                                            } else {
133                                                    errorCount++;
134                                                    // weight = weight / 0.99;
135                                            }
136    
137                                            // ((RegressionSample) sample).setWeight(weight);
138                                    }
139    
140                            }
141                    }
142    
143                    Matrix outputError = MatrixFactory.linkToValue(Math.sqrt(error
144                                    / dataSet.getSamples().getSize()));
145                    outputError.setLabel("RMSE with " + getLabel());
146                    dataSet.appendRMSEMatrix(outputError);
147    
148                    if ((dataSet instanceof ClassificationDataSet)) {
149                            confusion.setLabel("Confusion with " + getLabel());
150                            ((ClassificationDataSet) dataSet).appendConfusionMatrix(confusion);
151    
152                            Matrix accuracy = MatrixFactory.linkToValue((double) correctCount
153                                            / (double) dataSet.getSamples().getSize());
154                            accuracy.setLabel("Accuracy with " + getLabel());
155                            ((ClassificationDataSet) dataSet).appendAccuracyMatrix(accuracy);
156    
157                            Matrix errorMatrix = MatrixFactory.linkToValue(errorCount);
158                            errorMatrix.setLabel("Errors with " + getLabel());
159                            ((ClassificationDataSet) dataSet).appendErrorCountMatrix(errorMatrix);
160                    }
161    
162                    dataSet.notifyGUIObject();
163            }
164    
165            public Algorithm getOutputErrorAlgorithm() {
166                    return getAlgorithms().get(OUTPUTERRORALGORITHM);
167            }
168    
169            public void setOutputErrorAlgorithm(Algorithm a) {
170                    setAlgorithm(OUTPUTERRORALGORITHM, a);
171            }
172    
173            public int getMode() {
174                    return mode;
175            }
176    
177            public void setMode(int mode) {
178                    this.mode = mode;
179            }
180    
181    }