private void SelectCentroids(int k, Cluster[] clusters) { //seleciona, randomicamente, K padroes como centroides iniciais Dictionary <int, int> centroidPatternList = new Dictionary <int, int>(); int centroidPattern = -1; Random random = new Random(); while (centroidPatternList.Count < k) { centroidPattern = random.Next(0, patternMatrix.Size() - 1); if (!centroidPatternList.ContainsKey(centroidPattern)) { centroidPatternList.Add(centroidPattern, centroidPattern); } } for (int i = 0; i < k; i++) { //inicializa os k grupos clusters[i] = new Cluster(); clusters[i].Id = i; clusters[i].Centroid = patternMatrix.GetPattern(centroidPatternList.ElementAt(i).Value); // vincula o centroide ao grupo } }
public PatternMatrix Transform(HashSet <double[]> dataSet) { Pattern patternTemp = new Pattern(); // padrao PatternMatrix patternMatrix; // matriz de padroes int index = 0; // identificador do padrao PatternMatrix zPatternMatrix; //matriz de padroes normalizados patternMatrix = new PatternMatrix(); zPatternMatrix = new PatternMatrix(); foreach (var item in dataSet) { patternTemp = new Pattern(); patternTemp.Id = index; patternTemp.AddAttributes(item); patternMatrix.AddPattern(patternTemp); index++; } index = 0; double attributeSum = 0; double attributeMean = 0; double variance = 0; double attributeStandardDeviation = 0; double zAttribute = 0; double[] zpatternAttributes = new double[patternTemp.GetDimension()]; Pattern zPattern; //armazena o valor normalizado do padrao foreach (Pattern pattern in patternMatrix) { for (int i = 0; i < pattern.GetDimension(); i++) { foreach (Pattern patternx in patternMatrix) { attributeSum += patternx.GetAttribute(i); } // calcula a media do atributo attributeMean = attributeSum / patternMatrix.Size(); attributeSum = 0; foreach (Pattern patterny in patternMatrix) { variance += Math.Pow((patterny.GetAttribute(i) - attributeMean), 2); } // calcula a variancia do atributo variance = variance / patternMatrix.Size(); attributeStandardDeviation = Math.Sqrt(variance); // o desvio padrao eh a raiz quadrada da variancia zAttribute = (pattern.GetAttribute(i) - attributeMean) / attributeStandardDeviation; //atributo normalizado zpatternAttributes[i] = Math.Round(zAttribute, 2); attributeSum = 0; attributeMean = 0; variance = 0; attributeStandardDeviation = 0; } zPattern = new Pattern(); zPattern.AddAttributes(zpatternAttributes); zPattern.Id = index; zPatternMatrix.AddPattern(zPattern); index++; } return(zPatternMatrix); }