Advertisement
Guest User

Untitled

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