Advertisement
Guest User

Untitled

a guest
Nov 14th, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.46 KB | None | 0 0
  1.  
  2. namespace Iris
  3. {
  4.  
  5. public class Data
  6. {
  7. public double[] Features { get; }
  8.  
  9. public string Classifcation { get; }
  10.  
  11. public Data(double[] features,string classification)
  12. {
  13. Features = features;
  14.  
  15. Classifcation = classification;
  16. }
  17. }
  18.  
  19. public class Classificator
  20. {
  21. private int featureCount;
  22.  
  23. private string positiveResult;
  24.  
  25. private double[] weights;
  26.  
  27. private readonly double learningRate;
  28.  
  29. public Classificator(int featureCount,string positiveResult,double learningRate=0.5)
  30. {
  31. this.featureCount = featureCount;
  32.  
  33. this.positiveResult = positiveResult;
  34.  
  35. this.learningRate = learningRate;
  36.  
  37. InitWeight(featureCount);
  38. }
  39.  
  40. private void InitWeight(int featureCount)
  41. {
  42. //+1 for bias weight
  43.  
  44. weights = new double[featureCount+1];
  45.  
  46. for (int i=0;i<weights.Length;i++)
  47. {
  48. //weights[i] = new Random().NextDouble();
  49.  
  50. weights[i] = 0.5;
  51. }
  52. }
  53.  
  54. public void Train(Data[] data,int epoch)
  55. {
  56. for (int i = 0; i < epoch; i++)
  57. {
  58. BatchGraidentDescent(data);
  59. }
  60. }
  61.  
  62. private void BatchGraidentDescent(Data[] data)
  63. {
  64. double wholeSum = WholeSum(data);
  65.  
  66. double[] newWeights = new double[weights.Length];
  67.  
  68. double tmp = learningRate * (1.0 / data.Length) * wholeSum;
  69.  
  70. for (int i=0;i<weights.Length;i++)
  71. {
  72. double[] newFeature = GetNewFeature(data[i].Features);
  73.  
  74. newWeights[i] = weights[i] - (tmp * newFeature[i]);
  75. }
  76.  
  77. ChangeWeight(newWeights);
  78.  
  79. }
  80.  
  81.  
  82. private double WholeSum(Data[] data)
  83. {
  84. double wholeSum = 0;
  85.  
  86. for(int i=0;i<data.Length;i++)
  87. {
  88. wholeSum = Calculate(data[i].Features)-ToY(data[i].Classifcation);
  89. }
  90.  
  91. return wholeSum;
  92. }
  93.  
  94. public double Cost(Data[] data)
  95. {
  96. int m = data.Length;
  97.  
  98. double cost = 0;
  99.  
  100. for(int i=0;i<m;i++)
  101. {
  102. double y = ToY(data[i].Classifcation);
  103.  
  104. double hypoSum=Calculate(data[i].Features);
  105.  
  106. cost+=(y * Math.Log(hypoSum)) + ((1 - y) * Math.Log(1 - hypoSum));
  107. }
  108.  
  109. return (-1.0*(1.0/m)*cost);
  110. }
  111.  
  112. public double Calculate(double[] features)
  113. {
  114. if(features.Length!=featureCount)
  115. {
  116. throw new ArgumentException("Invalid feature count");
  117. }
  118.  
  119. double[] newFeature = GetNewFeature(features);
  120.  
  121. double sum = 0;
  122.  
  123. for(int i=0;i< newFeature.Length;i++)
  124. {
  125. sum += newFeature[i] * weights[i];
  126. }
  127.  
  128. return Sigmoid(sum);
  129. }
  130.  
  131. private double Sigmoid(double hypoSum)
  132. {
  133. double exponent=Math.Exp(hypoSum * -1.0);
  134.  
  135. return (1.0 / (1.0 + exponent));
  136. }
  137.  
  138. private void ChangeWeight(double[] newWeights)
  139. {
  140. for(int i=0;i<weights.Length;i++)
  141. {
  142. weights[i] = newWeights[i];
  143. }
  144. }
  145.  
  146. /// <summary>
  147. /// Change multiclass to the output that logistic regression know
  148. /// </summary>
  149. /// <param name="output"></param>
  150. /// <returns></returns>
  151. private double ToY(string output)
  152. {
  153. if(output==positiveResult)
  154. {
  155. return 1.0;
  156. }
  157.  
  158. return 0.0;
  159. }
  160.  
  161. /// <summary>
  162. /// Add a bias input
  163. /// </summary>
  164. /// <param name="features"></param>
  165. /// <returns></returns>
  166. private double[] GetNewFeature(double[] features)
  167. {
  168. double[] newFeature = new double[features.Length + 1];
  169.  
  170. //bias input
  171.  
  172. newFeature[0] = 1;
  173.  
  174. for (int i = 1; i < newFeature.Length; i++)
  175. {
  176. newFeature[i] = features[i - 1];
  177. }
  178.  
  179. return newFeature;
  180. }
  181.  
  182. public string GetPositiveResult()
  183. {
  184. return positiveResult;
  185. }
  186. }
  187. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement