public double getDistance(AnalyzedData tuple, List <MinMaxKNN> minMax) { double result = 0; int limit = Math.Min(Attributes.Count, Math.Min(tuple.attributes.Count, minMax.Count)); for (int i = 0; i < limit; i++) { result += Math.Pow(Attributes[i].getDistance(tuple.Attributes[i], minMax[i].MinValue, minMax[i].MaxValue), 2); } return(Math.Sqrt(result)); }
public void findMostLikelyClass(AnalyzedData analyzedData) { string bestClass = Classes[0]; double bestProbability = 0; foreach (var c in Classes) { double r = checkProbabilityForClass(analyzedData, c); if (r > bestProbability) { bestProbability = r; bestClass = c; } } analyzedData.DataClass = bestClass; }
public void classifyTuple(AnalyzedData tuple) { if (leaf != null) { tuple.DataClass = leaf; return; } if (checkCondition(tuple.Attributes[nodeAttribute].getValueAsString())) { leftChild.classifyTuple(tuple); } else { rightChild.classifyTuple(tuple); } }
private List <AnalyzedData> findNeighbours(AnalyzedData tuple) { List <Tuple <AnalyzedData, double> > dataList = new List <Tuple <AnalyzedData, double> >(); foreach (var v in dataInHyperspace.DataValues) { dataList.Add(new Tuple <AnalyzedData, double>(v, tuple.getDistance(v, minMax))); } dataList = dataList.OrderBy(x => x.Item2).ToList(); List <AnalyzedData> toReturn = new List <AnalyzedData>(); for (int i = 0; i < k; i++) { toReturn.Add(dataList[i].Item1); } return(toReturn); }
public double checkProbabilityForClass(AnalyzedData analyzedData, string analyzedClass) { double result = 1; double classDenominator = 0; double classNominator = Counters[Counters.Count - 1].returnNumberForOneClass(analyzedClass, analyzedClass); foreach (var v in Counters[Counters.Count - 1].Attributes) { classDenominator += v.Number; } for (int i = 0; i < analyzedData.Attributes.Count; i++) { double nominator = Counters[i].returnNumberForOneClass(analyzedData.Attributes[i].getValueAsString(), analyzedClass) + 1; double denominator = classNominator + Counters[i].DiffValues; result = result * nominator / denominator; } result = result * classNominator / classDenominator; return(result); }
public DataSet(string path, char splitter, bool hasLabel) { Headers = new List <string>(); DataValues = new List <AnalyzedData>(); int rid = 1; try { using (var sr = new StreamReader(path)) { string headersLine = sr.ReadLine(); var headersNames = headersLine.Split(' '); for (int i = 0; i < headersNames.Length - 1; i++) { Headers.Add(headersNames[i]); } string newLine; while (true) { newLine = sr.ReadLine(); if (newLine == null) { break; } var attributes = newLine.Split(splitter); AnalyzedData toAdd = new AnalyzedData(rid, attributes, Headers, hasLabel); DataValues.Add(toAdd); rid++; } } } catch (IOException e) { Console.WriteLine("The file could not be read:"); Console.WriteLine(e.Message); } }
public void setClass(AnalyzedData data) { var neighbours = findNeighbours(data); data.DataClass = doVoting(neighbours); }