public override float GetDistance(WineAttributes wine1, WineAttributes wine2) { float result = 0; float temp = 0; temp = wine1.Alcohol - wine2.Alcohol; result = temp * temp; temp = wine1.Chlorides - wine2.Chlorides; result += temp * temp; temp = wine1.CitricAcid - wine2.CitricAcid; result += temp * temp; temp = wine1.Density - wine2.Density; result += temp * temp; temp = wine1.FixedAcidity - wine2.FixedAcidity; result += temp * temp; temp = wine1.FreeSulfurDioxide - wine2.FreeSulfurDioxide; result += temp * temp; temp = wine1.PH - wine2.PH; result += temp * temp; temp = wine1.ResidualSugar - wine2.ResidualSugar; result += temp * temp; temp = wine1.Sulphates - wine2.Sulphates; result += temp * temp; temp = wine1.TotalSulfurDioxide - wine2.TotalSulfurDioxide; result += temp * temp; temp = wine1.VolatileAcidity - wine2.VolatileAcidity; result += temp * temp; return((float)Math.Sqrt(result)); }
public override float GetDistance(WineAttributes wine1, WineAttributes wine2) { // Removed following parameters: // Residual Sugar // Density // Chlorides // Sulphates // Citric Acid float result = 0; float temp = 0; temp = wine1.Alcohol - wine2.Alcohol; result += temp * temp; temp = wine1.FixedAcidity - wine2.FixedAcidity; result += temp * temp; temp = wine1.FreeSulfurDioxide - wine2.FreeSulfurDioxide; result += temp * temp; temp = wine1.PH - wine2.PH; result += temp * temp; temp = wine1.TotalSulfurDioxide - wine2.TotalSulfurDioxide; result += temp * temp; temp = wine1.VolatileAcidity - wine2.VolatileAcidity; result += temp * temp; return((float)Math.Sqrt(result)); }
/// <summary> /// Parses all the Attributes out of one Line. /// </summary> /// <param name="line">The string representing the current line.</param> /// <returns>An object, which holds all the Wine's Data.</returns> private WineAttributes ParseAttributes(string line) { int i = 0; WineAttributes attribute = new WineAttributes(); CultureInfo ci = (CultureInfo)CultureInfo.CurrentCulture.Clone(); ci.NumberFormat.CurrencyDecimalSeparator = "."; var fields = typeof(WineAttributes).GetFields(); var lst = line.Split(';'); foreach (var item in fields) { if (item.FieldType == typeof(float)) { item.SetValue(attribute, float.Parse(lst[i], NumberStyles.Any, ci)); } i++; } attribute.Quality = int.Parse(lst.Last()); return(attribute); }
public bool Equals(WineAttributes y) { return(Alcohol == y.Alcohol && Chlorides == y.Chlorides && CitricAcid == y.CitricAcid && Density == y.Density && FixedAcidity == y.FixedAcidity && FreeSulfurDioxide == y.FreeSulfurDioxide && PH == y.PH && ResidualSugar == y.ResidualSugar && Sulphates == y.Sulphates && TotalSulfurDioxide == y.TotalSulfurDioxide && VolatileAcidity == y.VolatileAcidity); }
public override float GetDistance(WineAttributes wine1, WineAttributes wine2) { float result = 0; result += Math.Abs(wine1.Alcohol - wine2.Alcohol); result += Math.Abs(wine1.Chlorides - wine2.Chlorides); result += Math.Abs(wine1.CitricAcid - wine2.CitricAcid); result += Math.Abs(wine1.Density - wine2.Density); result += Math.Abs(wine1.FixedAcidity - wine2.FixedAcidity); result += Math.Abs(wine1.FreeSulfurDioxide - wine2.FreeSulfurDioxide); result += Math.Abs(wine1.PH - wine2.PH); result += Math.Abs(wine1.ResidualSugar - wine2.ResidualSugar); result += Math.Abs(wine1.Sulphates - wine2.Sulphates); result += Math.Abs(wine1.TotalSulfurDioxide - wine2.TotalSulfurDioxide); result += Math.Abs(wine1.VolatileAcidity - wine2.VolatileAcidity); return(result); }
/// <summary> /// Reads all the Data from the CSV file and parses it into the Dictionaries. /// </summary> /// <param name="filename">The path to the File including the File Extension.</param> /// <returns>True if parsing was successful, false otherwise</returns> public bool ReadWineData(string filename) { try { string line; // Read the file and display it line by line. using (StreamReader sr = new StreamReader(filename)) { sr.ReadLine(); //Throw away header while ((line = sr.ReadLine()) != null) { WineAttributes attr = ParseAttributes(line); if (_TotalWineData[attr.Quality].Where(x => x.Equals(attr)).Count() == 0) { _TotalWineData[attr.Quality].Add(attr); } else { _floatWineData[attr.Quality].Add(attr); } } } } catch (FileNotFoundException) { return(false); } if (_LoggingEnabled) { Console.WriteLine("Found " + _TotalWineData.Values.Sum(list => list.Count) + " unique wine datasets."); Console.WriteLine("Found " + _floatWineData.Values.Sum(list => list.Count) + " redundant wine datasets.\n"); foreach (var dataSet in _TotalWineData.Where(x => x.Value.Count() > 0)) { Console.WriteLine("quality: " + dataSet.Key + " - wines: " + dataSet.Value.Count()); } } return(true); }
public abstract float GetDistance(WineAttributes wine1, WineAttributes wine2);