コード例 #1
0
        static void Main(string[] args)
        {
            IKMeans   _kMeans   = new KMeans();
            IDistance _distance = new EuclideanDistance();
            Random    rnd       = new Random(0);

            int objCount     = 10;
            int intrstsCount = 2;
            int clusterCount = 2;

            List <ClusterEntityDTO> clusters = new List <ClusterEntityDTO>();

            for (int i = 0; i < objCount; i++)
            {
                ClusterEntityDTO dTO = new ClusterEntityDTO
                {
                    Name = string.Format("{0:yyyyMMddHHmmss}", DateTime.Now),
                    Id   = i
                };
                List <Interest> interests = new List <Interest>();
                Console.WriteLine("___" + dTO.Name + "___");
                for (int j = 0; j < intrstsCount; j++)
                {
                    double   rand = Math.Round(rnd.NextDouble() * 200.0);
                    Interest intr = new Interest
                    {
                        Name    = string.Format("intr{0:yyyyMMddHHmmss}", DateTime.Now),
                        Weight  = rand,
                        Weight2 = rand
                    };
                    Console.WriteLine("--- " + intr.Weight + "---");
                    interests.Add(intr);
                }
                Console.WriteLine("-------------------------------");
                dTO.Interests = interests;
                clusters.Add(dTO);
            }
            Console.WriteLine("-----------------------------------");
            Console.WriteLine("-----------------------------------");
            Console.WriteLine("-----------------------------------");
            var result = _kMeans.Clustering(clusters, _distance, clusterCount);

            for (int i = 0; i < objCount; i++)
            {
                Console.WriteLine("___" + result[i].Name + "___");
                for (int j = 0; j < intrstsCount; j++)
                {
                    Console.WriteLine("---" + result[i].Interests[j].Weight2 + "---");
                }

                if (i < objCount - 1 && result[i].CentroidId != result[i + 1].CentroidId)
                {
                    Console.WriteLine("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%");
                }
            }

            Console.ReadLine();
        }
コード例 #2
0
        public double GetDinstance(ClusterEntityDTO first, ClusterEntityDTO second)
        {
            double distance = 0.0;

            for (int i = 0; i < first.Interests.Count; i++)
            {
                double diff = first.Interests[i].Weight - second.Interests[i].Weight;
                distance += Math.Abs(diff);
            }

            return(distance);
        }
コード例 #3
0
        public double GetDistance(List <ClusterEntityDTO> first, List <ClusterEntityDTO> second, IDistance distance)
        {
            ClusterEntityDTO firstDTO  = new ClusterEntityDTO();
            ClusterEntityDTO secondDTO = new ClusterEntityDTO();
            double           dist      = 0.0;

            for (int j = 0; j < first[0].Interests.Count; j++)
            {
                double weight = 0.0;

                for (int i = 0; i < first.Count; i++)
                {
                    weight += first[i].Interests[j].Weight;
                }

                firstDTO.Interests.Add(new Interest
                {
                    Name   = first[0].Interests[j].Name,
                    Weight = weight / (double)first.Count
                });
            }

            for (int j = 0; j < second[0].Interests.Count; j++)
            {
                double weight = 0.0;

                for (int i = 0; i < second.Count; i++)
                {
                    weight += second[i].Interests[j].Weight;
                }

                secondDTO.Interests.Add(new Interest
                {
                    Name   = first[0].Interests[j].Name,
                    Weight = weight / second.Count
                });
            }

            dist = distance.GetDinstance(firstDTO, secondDTO);

            return(dist);
        }
コード例 #4
0
        private List <ClusterEntityDTO> SetCentroids(ref List <ClusterEntityDTO> clusters, int clustersCount)
        {
            Random rnd = new Random(0);
            List <ClusterEntityDTO> centroids = new List <ClusterEntityDTO>();

            for (int i = 0; i < clustersCount; i++)
            {
                ClusterEntityDTO dTO = clusters[rnd.Next(0, clusters.Count - 1)];
                if (centroids.Contains(dTO))
                {
                    i--;
                }
                else
                {
                    dTO.Id = i;
                    centroids.Add(dTO.CloneObjectSerializable());
                }
            }
            return(centroids);
        }
コード例 #5
0
        private List <ClusterEntityDTO> GetDataFromTxt(IFormFile file)
        {
            List <string> lines = new List <string>();

            using (StreamReader reader = new StreamReader(file.OpenReadStream()))
            {
                string line;
                while ((line = reader.ReadLine()) != null)
                {
                    lines.Add(line);
                }
            }
            string[] names = lines[0].Split("\t");

            List <ClusterEntityDTO> result = new List <ClusterEntityDTO>();

            for (int i = 1; i < lines.Count; i++)
            {
                ClusterEntityDTO dTO       = new ClusterEntityDTO();
                string[]         parametrs = lines[i].Split("\t");
                dTO.Interests = new List <Interest>();
                dTO.Name      = parametrs[0];

                for (int j = 1; j < parametrs.Length; j++)
                {
                    Interest intrst = new Interest
                    {
                        Name    = names[j],
                        Weight  = double.Parse(parametrs[j]),
                        Weight2 = double.Parse(parametrs[j])
                    };
                    dTO.Interests.Add(intrst);
                }
                result.Add(dTO);
            }

            return(result);
        }
コード例 #6
0
        private List <ClusterEntityDTO> GetDataFromExcel(IFormFile file)
        {
            var wb       = new XLWorkbook(file.OpenReadStream());
            var ws       = wb.Worksheets.First();
            var range    = ws.RangeUsed();
            var rowCount = range.RowCount();
            var colCount = range.ColumnCount();
            List <ClusterEntityDTO> result = new List <ClusterEntityDTO>();
            List <string>           names  = new List <string>();

            for (int i = 2; i <= colCount; i++)
            {
                names.Add(ws.Cell(1, i).Value.ToString());
            }

            for (int i = 2; i <= rowCount; i++)
            {
                ClusterEntityDTO dTO = new ClusterEntityDTO();
                dTO.Name      = ws.Cell(i, 1).Value.ToString();
                dTO.Interests = new List <Interest>();

                for (int j = 2; j <= colCount; j++)
                {
                    Interest interest = new Interest
                    {
                        Name    = names[j - 2],
                        Weight  = double.Parse(ws.Cell(i, j).Value.ToString()),
                        Weight2 = double.Parse(ws.Cell(i, j).Value.ToString())
                    };
                    dTO.Interests.Add(interest);
                }
                result.Add(dTO);
            }

            return(result);
        }
コード例 #7
0
        private List <ClusterEntityDTO> Union(List <ClusterEntityDTO> data, int maxUnion, int step)
        {
            int unionCount = maxUnion;
            List <ClusterEntityDTO>            result    = new List <ClusterEntityDTO>();
            List <HierarchicalUnionObjectsDTO> distances = new List <HierarchicalUnionObjectsDTO>();

            for (int i = 0; i < data.Count; i++)
            {
                for (int j = i + 1; j < data.Count; j++)
                {
                    distances.Add(new HierarchicalUnionObjectsDTO
                    {
                        FirstId  = i,
                        SecondId = j,
                        Distance = _distance.GetDinstance(data[i], data[j])
                    });
                }
            }
            distances.Sort((x, y) => x.Distance.CompareTo(y.Distance));
            bool[] used    = new bool[data.Count];
            int    counter = 0;

            for (int i = 0; i < distances.Count; i++)
            {
                if (unionCount == 0)
                {
                    break;
                }
                if (!used[distances[i].FirstId] && !used[distances[i].SecondId])
                {
                    ClusterEntityDTO dTO = new ClusterEntityDTO();
                    dTO.Children = new List <ClusterEntityDTO>
                    {
                        data[distances[i].FirstId],
                        data[distances[i].SecondId]
                    };
                    if (step == 1)
                    {
                        dTO.Name = GetName(counter++);
                    }
                    else
                    {
                        dTO.Name = dTO.Children[0].Name + dTO.Children[1].Name;
                    }
                    used[distances[i].FirstId]  = true;
                    used[distances[i].SecondId] = true;
                    result.Add(dTO);
                    --unionCount;
                }
            }

            for (int i = 0; i < data.Count; i++)
            {
                if (!used[i])
                {
                    ClusterEntityDTO temp = new ClusterEntityDTO(data[i]);
                    temp.Children = new List <ClusterEntityDTO>(new List <ClusterEntityDTO> {
                        data[i]
                    });
                    temp.Name = data[i].Children == null?GetName(counter ++) : data[i].Name;

                    result.Add(temp);
                    //result.Add(data[i]);
                }
            }

            for (int k = 0; k < result.Count; k++)
            {
                result[k].Interests = new List <Interest>(data[0].Interests);
                if (result[k].Children != null)
                {
                    for (int j = 0; j < result[k].Children[0].Interests.Count; j++)
                    {
                        double weight = 0.0;
                        for (int i = 0; i < result[k].Children.Count; i++)
                        {
                            weight += result[k].Children[i].Interests[j].Weight;
                        }
                        result[k].Interests[j].Weight = weight / result[k].Children.Count;
                    }
                }
            }

            return(result);
        }