public KMeans(HashSet <double[]> dataSet, bool hasClass) { patternMatrix = new PatternMatrix(); if (hasClass) { foreach (var item in dataSet) { pattern = new Pattern(); pattern.Id = index; pattern.AddAttributes(item); pattern.RemoveAttributeAt(pattern.GetDimension() - 1); //remove o atributo classe da colecao de atributos pattern.ClassAttribute = Convert.ToInt32(item[pattern.GetDimension()]); //guarda a classe do padrao patternMatrix.AddPattern(pattern); index++; } } else { foreach (var item in dataSet) { pattern = new Pattern(); pattern.Id = index; pattern.AddAttributes(item); patternMatrix.AddPattern(pattern); index++; } } }
private int index = 0; // identificador do padrao public KMeans(HashSet <double[]> dataSet) { patternMatrix = new PatternMatrix(); foreach (var item in dataSet) { pattern = new Pattern(); pattern.Id = index; pattern.AddAttributes(item); patternMatrix.AddPattern(pattern); index++; } }
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); }
public PatternMatrix Transform(HashSet <double[]> dataSet) { Pattern patternTemp = new Pattern(); // padrao PatternMatrix patternMatrix; // matriz de padroes int index = 0; // identificador do padrao PatternMatrix MinMaxPatternMatrix; //matriz de padroes normalizados double minValue = Double.MaxValue; double maxValue = Double.MinValue; double minMaxAttribute = 0; patternMatrix = new PatternMatrix(); MinMaxPatternMatrix = new PatternMatrix(); Pattern minMaxPattern; //armazena o padrao normalizado foreach (var item in dataSet) { patternTemp = new Pattern(); patternTemp.Id = index; patternTemp.AddAttributes(item); patternMatrix.AddPattern(patternTemp); index++; } double[] minMaxAttributes = new double[patternTemp.GetDimension()]; foreach (Pattern pattern in patternMatrix) { for (int i = 0; i < pattern.GetDimension(); i++) { foreach (Pattern patternx in patternMatrix) { if (patternx.GetAttribute(i) < minValue) { minValue = patternx.GetAttribute(i); } if (patternx.GetAttribute(i) > maxValue) { maxValue = patternx.GetAttribute(i); } } minMaxAttribute = (pattern.GetAttribute(i) - minValue) / (maxValue - minValue); minValue = Double.MaxValue; maxValue = Double.MinValue; minMaxAttributes[i] = Math.Round(minMaxAttribute, 2); } minMaxPattern = new Pattern(); minMaxPattern.AddAttributes(minMaxAttributes); minMaxPattern.Id = index; MinMaxPatternMatrix.AddPattern(minMaxPattern); index++; } return(MinMaxPatternMatrix); }