MOA 12.03
Real Time Analytics for Data Streams
|
00001 /* 00002 * LearningCurve.java 00003 * Copyright (C) 2007 University of Waikato, Hamilton, New Zealand 00004 * @author Richard Kirkby ([email protected]) 00005 * 00006 * This program is free software; you can redistribute it and/or modify 00007 * it under the terms of the GNU General Public License as published by 00008 * the Free Software Foundation; either version 3 of the License, or 00009 * (at your option) any later version. 00010 * 00011 * This program is distributed in the hope that it will be useful, 00012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00014 * GNU General Public License for more details. 00015 * 00016 * You should have received a copy of the GNU General Public License 00017 * along with this program. If not, see <http://www.gnu.org/licenses/>. 00018 * 00019 */ 00020 package moa.evaluation; 00021 00022 import java.util.ArrayList; 00023 import java.util.List; 00024 00025 import moa.AbstractMOAObject; 00026 import moa.core.DoubleVector; 00027 import moa.core.Measurement; 00028 import moa.core.StringUtils; 00029 00036 public class LearningCurve extends AbstractMOAObject { 00037 00038 private static final long serialVersionUID = 1L; 00039 00040 protected List<String> measurementNames = new ArrayList<String>(); 00041 00042 protected List<double[]> measurementValues = new ArrayList<double[]>(); 00043 00044 public LearningCurve(String orderingMeasurementName) { 00045 this.measurementNames.add(orderingMeasurementName); 00046 } 00047 00048 public String getOrderingMeasurementName() { 00049 return this.measurementNames.get(0); 00050 } 00051 00052 public void insertEntry(LearningEvaluation learningEvaluation) { 00053 Measurement[] measurements = learningEvaluation.getMeasurements(); 00054 Measurement orderMeasurement = Measurement.getMeasurementNamed( 00055 getOrderingMeasurementName(), measurements); 00056 if (orderMeasurement == null) { 00057 throw new IllegalArgumentException(); 00058 } 00059 DoubleVector entryVals = new DoubleVector(); 00060 for (Measurement measurement : measurements) { 00061 entryVals.setValue(addMeasurementName(measurement.getName()), 00062 measurement.getValue()); 00063 } 00064 double orderVal = orderMeasurement.getValue(); 00065 int index = 0; 00066 while ((index < this.measurementValues.size()) 00067 && (orderVal > this.measurementValues.get(index)[0])) { 00068 index++; 00069 } 00070 this.measurementValues.add(index, entryVals.getArrayRef()); 00071 } 00072 00073 public int numEntries() { 00074 return this.measurementValues.size(); 00075 } 00076 00077 protected int addMeasurementName(String name) { 00078 int index = this.measurementNames.indexOf(name); 00079 if (index < 0) { 00080 index = this.measurementNames.size(); 00081 this.measurementNames.add(name); 00082 } 00083 return index; 00084 } 00085 00086 public String headerToString() { 00087 StringBuilder sb = new StringBuilder(); 00088 boolean first = true; 00089 for (String name : this.measurementNames) { 00090 if (!first) { 00091 sb.append(','); 00092 } else { 00093 first = false; 00094 } 00095 sb.append(name); 00096 } 00097 return sb.toString(); 00098 } 00099 00100 public String entryToString(int entryIndex) { 00101 StringBuilder sb = new StringBuilder(); 00102 double[] vals = this.measurementValues.get(entryIndex); 00103 for (int i = 0; i < this.measurementNames.size(); i++) { 00104 if (i > 0) { 00105 sb.append(','); 00106 } 00107 if ((i >= vals.length) || Double.isNaN(vals[i])) { 00108 sb.append('?'); 00109 } else { 00110 sb.append(Double.toString(vals[i])); 00111 } 00112 } 00113 return sb.toString(); 00114 } 00115 00116 @Override 00117 public void getDescription(StringBuilder sb, int indent) { 00118 sb.append(headerToString()); 00119 for (int i = 0; i < numEntries(); i++) { 00120 StringUtils.appendNewlineIndented(sb, indent, entryToString(i)); 00121 } 00122 } 00123 00124 public double getMeasurement(int entryIndex, int measurementIndex) { 00125 return this.measurementValues.get(entryIndex)[measurementIndex]; 00126 } 00127 00128 public String getMeasurementName(int measurementIndex) { 00129 return this.measurementNames.get(measurementIndex); 00130 } 00131 }