/** * {@inheritDoc} */ @Override public double processDouble(ColumnDefinition colDef) { return colDef.getMean(); }
/** * Define an array of classes for a catagorical value. * @param str The classes to add. */ public void defineClass(String[] str) { for(String s: str) { defineClass(s); } }
/** * Analyze the specified value. * @param value The value to analyze. */ public void analyze(String value) { switch(this.dataType) { case continuous: analyzeContinuous(value); break; case ordinal: analyzeOrdinal(value); break; case nominal: analyzeNominal(value); break; } }
/** * Find the index of a column. * @param colDef The column. * @return The column index. */ private int findIndex(ColumnDefinition colDef) { if (colDef.getIndex() != -1) { return colDef.getIndex(); } int index = this.source.columnIndex(colDef.getName()); colDef.setIndex(index); if (index == -1) { throw new EncogError("Can't find column"); } return index; }
/** * {@inheritDoc} */ @Override public int outputSize(ColumnDefinition colDef) { return colDef.getClasses().size(); }
/** * {@inheritDoc} */ @Override public int normalizeColumn(ColumnDefinition colDef, double value, double[] outputData, int outputColumn) { double result = ((value - colDef.getLow()) / (colDef.getHigh() - colDef.getLow())) * (this.normalizedHigh - this.normalizedLow) + this.normalizedLow; // typically caused by a number that should not have been normalized // (i.e. normalization or actual range is infinitely small. if( Double.isNaN(result) ) { result = ((this.normalizedHigh-this.normalizedLow)/2)+this.normalizedLow; } outputData[outputColumn] = result; return outputColumn+1; }
/** * Define a source column. These define the raw input. Use this function if * you know the index of the column in a non-header file. * * @param name * The name of the column. * @param index * The index of the column, needed for non-headered files. * @param colType * The column type. * @return The column definition */ public ColumnDefinition defineSourceColumn(String name, int index, ColumnType colType) { ColumnDefinition result = new ColumnDefinition(name, colType); result.setIndex(index); addSourceColumn(result); return result; }
/** * Find a normalizer for the specified column definition, and if it is input or output. * @param colDef The column definition. * @param isInput True if the column is input. * @return The normalizer to use. */ private Normalizer findNormalizer(ColumnDefinition colDef, boolean isInput) { Normalizer norm = null; if(isInput) { if( this.inputNormalizers.containsKey(colDef.getDataType())) { norm = this.inputNormalizers.get(colDef.getDataType()); } } else { if( this.outputNormalizers.containsKey(colDef.getDataType())) { norm = this.outputNormalizers.get(colDef.getDataType()); } } if( norm==null ) { throw new EncogError("No normalizer defined for input="+isInput+", type=" + colDef.getDataType()); } return norm; }
/** * Add a source column. These define the raw input. * * @param def * The column definition. */ public void addSourceColumn(ColumnDefinition def) { this.sourceColumns.add(def); def.setOwner(this); }
/** * {@inheritDoc} */ public String toString() { StringBuilder result = new StringBuilder(); result.append("[NormalizationHelper:\n"); for (ColumnDefinition colDef : this.sourceColumns) { result.append(colDef.toString()); result.append("\n"); } result.append("]"); return result.toString(); }
/** * {@inheritDoc} */ @Override public int normalizeColumn(ColumnDefinition colDef, String value, double[] outputData, int outputColumn) { for (int i = 0; i < colDef.getClasses().size(); i++) { double d = this.normalizedLow; if (colDef.getClasses().get(i).equals(value)) { d = this.normalizedHigh; } outputData[outputColumn + i] = d; } return outputColumn + colDef.getClasses().size(); }
/** * {@inheritDoc} */ @Override public String denormalizeColumn(ColumnDefinition colDef, MLData data, int dataColumn) { double value = data.getData(dataColumn); final double result = ((colDef.getLow() - colDef.getHigh()) * value - this.normalizedHigh * colDef.getLow() + colDef.getHigh() * this.normalizedLow) / (this.normalizedLow - this.normalizedHigh); // typically caused by a number that should not have been normalized // (i.e. normalization or actual range is infinitely small. if( Double.isNaN(result) ) { return ""+(((this.normalizedHigh-this.normalizedLow)/2)+this.normalizedLow); } return ""+result; } }
/** * Define a single column as an output column, all others as inputs. * @param outputColumn The output column. */ public void defineSingleOutputOthersInput(ColumnDefinition outputColumn) { this.helper.clearInputOutput(); for (ColumnDefinition colDef : this.helper.getSourceColumns()) { if (colDef == outputColumn) { defineOutput(colDef); } else if (colDef.getDataType() != ColumnType.ignore) { defineInput(colDef); } } }
/** * {@inheritDoc} */ @Override public int normalizeColumn(ColumnDefinition colDef, String value, double[] outputData, int outputColumn) { if (!colDef.getClasses().contains(value)) { throw new EncogError("Undefined value: " + value); } outputData[outputColumn] = colDef.getClasses().indexOf(value); return outputColumn + 1; }
/** * Define multiple output columns, all others as inputs. * @param outputColumns The output columns. */ public void defineMultipleOutputsOthersInput(ColumnDefinition[] outputColumns) { this.helper.clearInputOutput(); for (ColumnDefinition colDef : this.helper.getSourceColumns()) { boolean isOutput = false; for(ColumnDefinition col : outputColumns) { if( col==colDef) { isOutput = true; } } if ( isOutput) { defineOutput(colDef); } else if (colDef.getDataType() != ColumnType.ignore) { defineInput(colDef); } } }
/** * {@inheritDoc} */ @Override public String denormalizeColumn(ColumnDefinition colDef, MLData data, int dataColumn) { return colDef.getClasses().get((int) data.getData(dataColumn)); }
/** * {@inheritDoc} */ @Override public String denormalizeColumn(ColumnDefinition colDef, MLData data, int dataColumn) { double high = colDef.getClasses().size(); double low = 0; double value = data.getData(dataColumn); final double result = ((low - high) * value - this.normalizedHigh * low + high * this.normalizedLow) / (this.normalizedLow - this.normalizedHigh); // typically caused by a number that should not have been normalized // (i.e. normalization or actual range is infinitely small. if (Double.isNaN(result)) { return colDef.getClasses().get(0); } return colDef.getClasses().get((int) result); } }
/** * {@inheritDoc} */ @Override public int normalizeColumn(ColumnDefinition colDef, String theValue, double[] outputData, int outputColumn) { // Find the index of the ordinal int v = colDef.getClasses().indexOf(theValue); if (v == -1) { throw new EncogError("Unknown ordinal: " + theValue); } double high = colDef.getClasses().size(); double value = v; double result = (value / high) * (this.normalizedHigh - this.normalizedLow) + this.normalizedLow; // typically caused by a number that should not have been normalized // (i.e. normalization or actual range is infinitely small. if (Double.isNaN(result)) { result = ((this.normalizedHigh - this.normalizedLow) / 2) + this.normalizedLow; } outputData[outputColumn] = result; return outputColumn + 1; }
/** * {@inheritDoc} */ @Override public int determineOutputCount(VersatileMLDataSet dataset) { return dataset.getNormHelper().getOutputColumns().get(0).getClasses().size(); } }
/** * {@inheritDoc} */ @Override public String denormalizeColumn(ColumnDefinition colDef, MLData data, int dataColumn) { double bestValue = Double.NEGATIVE_INFINITY; int bestIndex = 0; for (int i = 0; i < data.size(); i++) { double d = data.getData(dataColumn + i); if (d > bestValue) { bestValue = d; bestIndex = i; } } return colDef.getClasses().get(bestIndex); }