Ejemplo n.º 1
0
        private void CalculateStage()
        {
            TrainingSet   temp  = this.m_set;
            StringBuilder sb    = new StringBuilder();
            int           count = Enum.GetNames(typeof(ClusterType)).Length;

            sb.AppendLine("---------------------------------------------------------------------------------------------");
            sb.Append("Observations\t");
            for (int i = 0; i < count; i++)
            {
                sb.Append($"Distance from M{i+1}\t\t");
            }
            sb.Append("Cluster");
            sb.AppendLine();
            sb.AppendLine("---------------------------------------------------------------------------------------------");
            for (int i = 0; i < temp.Samples.Count; i++)
            {
                TrainingSample sample = temp.Samples[i];

                int val1 = sample.Variable1;
                int val2 = sample.Variable2;

                sb.Append(sample.Observation);
                sb.Append("\t\t");

                int    lowestC   = int.MaxValue;
                double lowestDis = double.MaxValue;
                for (int j = 0; j < count; j++)
                {
                    double mx = CalculateMX(j);
                    double my = CalculateMY(j);

                    double distance = GetOklidDistance(val1, mx, val2, my);

                    if (distance < lowestDis)
                    {
                        lowestDis = distance;
                        lowestC   = j;
                        ClusterType type = (ClusterType)Enum.ToObject(typeof(ClusterType), lowestC);
                        sample.SetClusterType(type);
                        this.m_set.Samples[i] = sample;
                    }

                    sb.Append($"d(M{j + 1}, {sample.Observation}) = {distance} \t");
                }
                sb.Append($"\tC{(int)sample.ClusterType + 1}");

                sb.AppendLine();
            }
            sb.Append("---------------------------------------------------------------------------------------------");
            Console.WriteLine(sb.ToString());
        }
Ejemplo n.º 2
0
        private double CalculateMY(int clusterIndex)
        {
            double temp = 0.0d;

            for (int i = 0; i < this.m_set.Samples.Count; i++)
            {
                TrainingSample sample = this.m_set.Samples[i];
                if ((int)sample.ClusterType == clusterIndex)
                {
                    temp += sample.Variable2;
                }
            }
            return(Math.Round(temp / GetMCount(clusterIndex), 2));
        }
Ejemplo n.º 3
0
 public void AddSample(TrainingSample sample)
 {
     if (this.m_canAddSample)
     {
         if (!this.Samples.Contains(sample))
         {
             this.Samples.Add(sample);
         }
         else
         {
             throw new Exception("Already contains value");
         }
     }
     else
     {
         throw new Exception("Training set was locked");
     }
 }
Ejemplo n.º 4
0
        private void PrintInit()
        {
            StringBuilder sb    = new StringBuilder();
            int           count = Enum.GetNames(typeof(ClusterType)).Length;

            sb.AppendLine("---------------------------------------------------------------------------------------------");
            sb.Append("Observations\tVar1\tVar2\tCluster");
            sb.AppendLine();
            sb.AppendLine("---------------------------------------------------------------------------------------------");
            for (int i = 0; i < this.m_set.Samples.Count; i++)
            {
                TrainingSample sample = this.m_set.Samples[i];
                sb.AppendLine($"{sample.Observation}\t\t{sample.Variable1}\t\t{sample.Variable2}\t\t{sample.ClusterType.ToString()}");
                sb.AppendLine();
            }
            sb.Append("---------------------------------------------------------------------------------------------");
            Console.WriteLine(sb.ToString());
        }
Ejemplo n.º 5
0
        private double CalculateSubE(int clusterIndex)
        {
            double temp = 0.0d;

            for (int i = 0; i < this.m_set.Samples.Count; i++)
            {
                TrainingSample sample = this.m_set.Samples[i];
                if ((int)sample.ClusterType == clusterIndex)
                {
                    int val1 = sample.Variable1;
                    int val2 = sample.Variable2;

                    double mx = CalculateMX(clusterIndex);
                    double my = CalculateMY(clusterIndex);

                    double distance = GetSqrtDistance(val1, mx, val2, my);

                    temp += distance;
                }
            }


            return(temp);
        }