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.dataset;
025
026 import org.jdmp.core.dataset.wrappers.DataSetPredictedMatrixWrapper;
027 import org.jdmp.core.dataset.wrappers.DataSetTargetMatrixWrapper;
028 import org.jdmp.core.variable.Variable;
029 import org.jdmp.core.variable.VariableFactory;
030 import org.ujmp.core.Matrix;
031 import org.ujmp.core.exceptions.MatrixException;
032
033 public class RegressionDataSet extends DefaultDataSet {
034 private static final long serialVersionUID = -3243395577983195632L;
035
036 public static final String RMSE = "RMSE";
037
038 public static final String PREDICTED = "Predicted";
039
040 public static final String TARGET = "Target";
041
042 public RegressionDataSet() {
043 getVariables().put(RMSE, VariableFactory.labeledVariable("RMSE", 10000));
044 Matrix targetMatrix = new DataSetTargetMatrixWrapper(this);
045 Variable target = VariableFactory.labeledVariable("Target");
046 target.addMatrix(targetMatrix);
047 getVariables().put(TARGET, target);
048 Matrix predictedMatrix = new DataSetPredictedMatrixWrapper(this);
049 Variable predicted = VariableFactory.labeledVariable("Predicted");
050 predicted.addMatrix(predictedMatrix);
051 getVariables().put(PREDICTED, predicted);
052 }
053
054 public Variable getRMSEVariable() {
055 return getVariables().get(RMSE);
056 }
057
058 public Variable getTargetVariable() {
059 return getVariables().get(TARGET);
060 }
061
062 public void appendRMSEMatrix(Matrix m) {
063 getRMSEVariable().addMatrix(m);
064 }
065
066 public Matrix getTargetMatrix() {
067 return getTargetVariable().getMatrix();
068 }
069
070 public double getEarlyStoppingRMSE(int numberOfSteps) {
071 int index = getEarlyStoppingIndex(numberOfSteps);
072 if (index >= 0) {
073 return getRMSEVariable().getMatrix(index).getEuklideanValue();
074 }
075 return -1;
076 }
077
078 public boolean isEarlyStoppingReached(int numberOfSteps) {
079 return getEarlyStoppingIndex(numberOfSteps) >= 0;
080 }
081
082 public int getEarlyStoppingIndex(int numberOfSteps) {
083 Variable v = getRMSEVariable();
084 if (v.getMatrixCount() <= numberOfSteps) {
085 return -1;
086 }
087
088 double minRMSE = Double.MAX_VALUE;
089 int position = -1;
090 for (int i = 0; i < v.getMatrixCount(); i++) {
091 double e = v.getMatrix(i).getEuklideanValue();
092 if (e < minRMSE) {
093 minRMSE = e;
094 position = i;
095 }
096 if (i == position + numberOfSteps) {
097 return position;
098 }
099 }
100 return -1;
101 }
102
103 public Variable getPredictedVariable() {
104 return getVariables().get(PREDICTED);
105 }
106
107 public Matrix getPredictedMatrix() {
108 return getPredictedVariable().getMatrix();
109 }
110
111 public double getRMSE() throws MatrixException {
112 Matrix m = getRMSEMatrix();
113 if (m == null) {
114 return Double.NaN;
115 } else {
116 return m.getEuklideanValue();
117 }
118 }
119
120 public Matrix getRMSEMatrix() {
121 return getRMSEVariable().getMatrix();
122 }
123
124 }