I'm working on a small command line application in c# where data is read from a CSV file along with a query and KNN is calculated for the query based on the data.
My problem isn't the algorithm at all, I understand it. Im having difficulty working with my code though. I think it's down to bad planning.
Here's my object for holding a "row" of data. the data is based on weather data and if something was played that day or not.
class dataRow
{
public double sun {get; set;}
public double temp {get; set;}
public double humidity {get; set;}
public double wind {get; set;}
public bool play {get; set;}
public override string ToString()
{
return string.Format("\n\tSun: {0}\n\tTemp: {1}\n\tHumidity: {2}\n\tWind: {3}\n\tPlay: {4}", sun, temp, humidity, wind, play);
}
public double[] toArray()
{
return new double[] { sun, temp, humidity, wind };
}
}
I'm trying to Znormalize the data and the query before I work out euclidean distance between points but I'm stuck in a rut of annoying nested loops and I have a hunch there's a better way to code what I'm doing
class KNN
{
public int numK;
List<dataRow> dataSet;
dataRow QueryRow;
dataRow stdDev;
dataRow avg;
public KNN(String[] input)
{
numK = Int32.Parse(input[0]);
dataSet = dataImport.import(input[1]);
//inputRow = dataImport.import(input[2])[0];
avg = getAverage(toDoubleList(dataSet));
}
public List<double[]> toDoubleList(List<dataRow> input)
{
var doubleList = new List<double[]>();
for(int i =0; i<input.Count; i++)
{
doubleList[i] = input[i].toArray();
}
return doubleList;
}
public dataRow getAverage(List<double[]> input)
{
var avgTotals = new double[4];
var avgRow = new dataRow();
for (int y = 0; y < 3; y++)
{
double avg = 0;
for (int x = 0; x < input.Count; x++)
{
avg += input[x][y];
}
avgTotals[y] = avg / input.Count;
}
avgRow.sun = avgTotals[0];
avgRow.temp = avgTotals[1];
avgRow.sun = avgTotals[2];
avgRow.wind = avgTotals[3];
return avgRow;
}
public dataRow getStdDev(List<double[]> input)
{
var stdDevTotals = new double[4];
var stdDevRow = new dataRow();
for (int y = 0; y < 3; y++)
{
double stdDev = 0;
for (int x = 0; x < input.Count; x++)
{
stdDev += Math.Pow(input[x][y] - this.avg.toArray()[y], 2);
}
stdDev = Math.Sqrt(stdDev/input.Count);
stdDevTotals[y] = stdDev / input.Count;
}
stdDevRow.sun = stdDevTotals[0];
stdDevRow.temp = stdDevTotals[1];
stdDevRow.sun = stdDevTotals[2];
stdDevRow.wind = stdDevTotals[3];
return stdDevRow;
}
}
Any advice or ideas? (All harsh criticism welcome!)
numK
public? Also what is KNN??? Try and use good/sensible names if you are unsure, put a comment saying//Need to think of good name here
. My rant is over. \$\endgroup\$