/encog-core-silverlight/encog-core-silverlight/Normalize/Segregate/IntegerBalanceSegregator.cs

http://encog-cs.googlecode.com/ · C# · 198 lines · 95 code · 20 blank · 83 comment · 2 complexity · 09286fe215e88f18f922c6eef78e1dcd MD5 · raw file

  1. // Encog(tm) Artificial Intelligence Framework v2.5
  2. // .Net Version
  3. // http://www.heatonresearch.com/encog/
  4. // http://code.google.com/p/encog-java/
  5. //
  6. // Copyright 2008-2010 by Heaton Research Inc.
  7. //
  8. // Released under the LGPL.
  9. //
  10. // This is free software; you can redistribute it and/or modify it
  11. // under the terms of the GNU Lesser General Public License as
  12. // published by the Free Software Foundation; either version 2.1 of
  13. // the License, or (at your option) any later version.
  14. //
  15. // This software is distributed in the hope that it will be useful,
  16. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18. // Lesser General Public License for more details.
  19. //
  20. // You should have received a copy of the GNU Lesser General Public
  21. // License along with this software; if not, write to the Free
  22. // Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  23. // 02110-1301 USA, or see the FSF site: http://www.fsf.org.
  24. //
  25. // Encog and Heaton Research are Trademarks of Heaton Research, Inc.
  26. // For information on Heaton Research trademarks, visit:
  27. //
  28. // http://www.heatonresearch.com/copyright.html
  29. using System;
  30. using System.Collections.Generic;
  31. using System.Linq;
  32. using System.Text;
  33. using Encog.Normalize.Input;
  34. using Encog.Persist.Attributes;
  35. namespace Encog.Normalize.Segregate
  36. {
  37. /// <summary>
  38. /// Balance based on an input value. This allows you to make sure that one input
  39. /// class does not saturate the training data. To do this, you specify the input
  40. /// value to check and the number of occurrences of each integer value of this
  41. /// field to allow.
  42. /// </summary>
  43. public class IntegerBalanceSegregator : ISegregator
  44. {
  45. /// <summary>
  46. /// The normalization object to use.
  47. /// </summary>
  48. [EGReference]
  49. private DataNormalization normalization;
  50. /// <summary>
  51. /// The input field.
  52. /// </summary>
  53. [EGReference]
  54. private IInputField target;
  55. /// <summary>
  56. /// The count per each of the int values for the input field.
  57. /// </summary>
  58. private int count;
  59. /// <summary>
  60. /// The running totals.
  61. /// </summary>
  62. [EGIgnore]
  63. private IDictionary<int, int> runningCounts = new Dictionary<int, int>();
  64. /// <summary>
  65. /// Construct a balanced segregator.
  66. /// </summary>
  67. /// <param name="target">The input field to base this on, should
  68. /// be an integer value.</param>
  69. /// <param name="count">The number of rows to accept from each
  70. /// unique value for the input.</param>
  71. public IntegerBalanceSegregator(IInputField target, int count)
  72. {
  73. this.target = target;
  74. this.count = count;
  75. }
  76. /// <summary>
  77. /// Default constructor for reflection.
  78. /// </summary>
  79. public IntegerBalanceSegregator()
  80. {
  81. }
  82. /// <summary>
  83. /// The owner of this segregator.
  84. /// </summary>
  85. public DataNormalization Owner
  86. {
  87. get
  88. {
  89. return this.normalization;
  90. }
  91. }
  92. /// <summary>
  93. /// Get information on how many rows fall into each group.
  94. /// </summary>
  95. /// <returns>A string that contains the counts for each group.</returns>
  96. public String DumpCounts()
  97. {
  98. StringBuilder result = new StringBuilder();
  99. foreach (int key in this.runningCounts.Keys)
  100. {
  101. int value = this.runningCounts[key];
  102. result.Append(key);
  103. result.Append(" -> ");
  104. result.Append(value);
  105. result.Append(" count\n");
  106. }
  107. return result.ToString();
  108. }
  109. /// <summary>
  110. /// The number of groups found.
  111. /// </summary>
  112. public int Count
  113. {
  114. get
  115. {
  116. return this.count;
  117. }
  118. }
  119. /// <summary>
  120. /// A map of the running count for each group.
  121. /// </summary>
  122. public IDictionary<int, int> RunningCounts
  123. {
  124. get
  125. {
  126. return this.runningCounts;
  127. }
  128. }
  129. /// <summary>
  130. /// The target input field.
  131. /// </summary>
  132. public IInputField Target
  133. {
  134. get
  135. {
  136. return this.target;
  137. }
  138. }
  139. /// <summary>
  140. /// Init the segregator with the owning normalization object.
  141. /// </summary>
  142. /// <param name="normalization">The data normalization object to use.</param>
  143. public void Init(DataNormalization normalization)
  144. {
  145. this.normalization = normalization;
  146. }
  147. /// <summary>
  148. /// Init for a new pass.
  149. /// </summary>
  150. public void PassInit()
  151. {
  152. this.runningCounts.Clear();
  153. }
  154. /// <summary>
  155. /// Determine of the current row should be included.
  156. /// </summary>
  157. /// <returns>True if the current row should be included.</returns>
  158. public bool ShouldInclude()
  159. {
  160. int key = (int)this.target.CurrentValue;
  161. int value = 0;
  162. if (this.runningCounts.ContainsKey(key))
  163. {
  164. value = this.runningCounts[key];
  165. }
  166. if (value < this.count)
  167. {
  168. value++;
  169. this.runningCounts[key] = value;
  170. return true;
  171. }
  172. else
  173. {
  174. return false;
  175. }
  176. }
  177. }
  178. }