PageRenderTime 58ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/src/main/java/org/encog/app/analyst/script/normalize/AnalystNormalize.java

http://github.com/encog/encog-java-core
Java | 189 lines | 105 code | 21 blank | 63 comment | 29 complexity | ba893bd96a22bc8a19245b09a8dafc1d MD5 | raw file
  1. /*
  2. * Encog(tm) Core v3.4 - Java Version
  3. * http://www.heatonresearch.com/encog/
  4. * https://github.com/encog/encog-java-core
  5. * Copyright 2008-2017 Heaton Research, Inc.
  6. *
  7. * Licensed under the Apache License, Version 2.0 (the "License");
  8. * you may not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS,
  15. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. *
  19. * For more information on Heaton Research copyrights, licenses
  20. * and trademarks visit:
  21. * http://www.heatonresearch.com/copyright
  22. */
  23. package org.encog.app.analyst.script.normalize;
  24. import java.util.ArrayList;
  25. import java.util.List;
  26. import org.encog.app.analyst.AnalystError;
  27. import org.encog.app.analyst.missing.DiscardMissing;
  28. import org.encog.app.analyst.missing.HandleMissingValues;
  29. import org.encog.app.analyst.missing.MeanAndModeMissing;
  30. import org.encog.app.analyst.missing.NegateMissing;
  31. import org.encog.app.analyst.script.AnalystClassItem;
  32. import org.encog.app.analyst.script.AnalystScript;
  33. import org.encog.app.analyst.script.DataField;
  34. import org.encog.app.analyst.script.prop.ScriptProperties;
  35. import org.encog.util.arrayutil.ClassItem;
  36. import org.encog.util.arrayutil.NormalizationAction;
  37. /**
  38. * This class holds information about the fields that the Encog Analyst will
  39. * normalize.
  40. *
  41. */
  42. public class AnalystNormalize {
  43. /**
  44. * The normalized fields. These fields define the order and format
  45. * that data will be presented to the ML method.
  46. */
  47. private final List<AnalystField> normalizedFields
  48. = new ArrayList<AnalystField>();
  49. /**
  50. * The parent script.
  51. */
  52. private AnalystScript script;
  53. /**
  54. * Construct the object.
  55. * @param theScript The script.
  56. */
  57. public AnalystNormalize(AnalystScript theScript) {
  58. this.script = theScript;
  59. }
  60. /**
  61. * @return Calculate the input columns.
  62. */
  63. public int calculateInputColumns() {
  64. int result = 0;
  65. for (final AnalystField field : this.normalizedFields) {
  66. if (field.isInput()) {
  67. result += field.getColumnsNeeded();
  68. }
  69. }
  70. return result;
  71. }
  72. /**
  73. * Calculate the output columns.
  74. * @return The output columns.
  75. */
  76. public int calculateOutputColumns() {
  77. int result = 0;
  78. for (final AnalystField field : this.normalizedFields) {
  79. if (field.isOutput()) {
  80. result += field.getColumnsNeeded();
  81. }
  82. }
  83. return result;
  84. }
  85. /**
  86. * @return Count the active fields.
  87. */
  88. public int countActiveFields() {
  89. int result = 0;
  90. for (final AnalystField field : this.normalizedFields) {
  91. if (field.getAction() != NormalizationAction.Ignore) {
  92. result++;
  93. }
  94. }
  95. return result;
  96. }
  97. /**
  98. * @return the normalizedFields
  99. */
  100. public List<AnalystField> getNormalizedFields() {
  101. return this.normalizedFields;
  102. }
  103. /**
  104. * Init the normalized fields.
  105. * @param script The script.
  106. */
  107. public void init(final AnalystScript script) {
  108. if (this.normalizedFields == null) {
  109. return;
  110. }
  111. for (final AnalystField norm : this.normalizedFields) {
  112. final DataField f = script.findDataField(norm.getName());
  113. if (f == null) {
  114. throw new AnalystError("Normalize specifies unknown field: "
  115. + norm.getName());
  116. }
  117. if (norm.getAction() == NormalizationAction.Normalize) {
  118. norm.setActualHigh(f.getMax());
  119. norm.setActualLow(f.getMin());
  120. }
  121. if ((norm.getAction() == NormalizationAction.Equilateral)
  122. || (norm.getAction() == NormalizationAction.OneOf)
  123. || (norm.getAction() == NormalizationAction.SingleField)) {
  124. int index = 0;
  125. for (final AnalystClassItem item : f.getClassMembers()) {
  126. norm.getClasses().add(
  127. new ClassItem(item.getName(), index++));
  128. }
  129. }
  130. }
  131. }
  132. /** {@inheritDoc} */
  133. @Override
  134. public String toString() {
  135. final StringBuilder result = new StringBuilder("[");
  136. result.append(getClass().getSimpleName());
  137. result.append(": ");
  138. if (this.normalizedFields != null) {
  139. result.append(this.normalizedFields.toString());
  140. }
  141. result.append("]");
  142. return result.toString();
  143. }
  144. /**
  145. * @return the missingValues
  146. */
  147. public HandleMissingValues getMissingValues() {
  148. final String type = this.script.getProperties().getPropertyString(
  149. ScriptProperties.ML_CONFIG_TYPE);
  150. if( type.equals("DiscardMissing") ) {
  151. return new DiscardMissing();
  152. } else if( type.equals("MeanAndModeMissing") ) {
  153. return new MeanAndModeMissing();
  154. } else if( type.equals("NegateMissing") ) {
  155. return new NegateMissing();
  156. } else {
  157. return new DiscardMissing();
  158. }
  159. }
  160. /**
  161. * @param missingValues the missingValues to set
  162. */
  163. public void setMissingValues(HandleMissingValues missingValues) {
  164. this.script.getProperties().setProperty(
  165. ScriptProperties.NORMALIZE_MISSING_VALUES, missingValues.getClass().getSimpleName());
  166. }
  167. }