static void Main(string[] args) { if (usePca) { //Application.EnableVisualStyles(); //Application.Run(new Diagrams()); (List <List <double> > data, _) = ReadDataAsList(dataFile, ';', columnsToIgnore, targetColumns, dataStartLine, dataEndLine); KNN(data); var pca = PCA.Compute(data, 5); //read data from file (double[][] dataArray, double[][] targetArray) = ReadDataAsArray(dataFile, ';', columnsToIgnore, targetColumns, dataStartLine, dataEndLine); double[][] pcaData = List2DToArray2D(pca); //normalize all data values (scale between 0 and 1) (double[][] normalizedPcaData, _, _) = NormalizeData(pcaData); (double[][] normalizedTargets, double[] min, double[] max) = NormalizeData(targetArray); //split data into two sets - training and testing = 80% and 20% (double[][] trainDataArray, double[][] trainTargetArray, double[][] testDataArray, double[][] testTargetArray) = SplitData(normalizedPcaData, normalizedTargets, dataSplitPercentage); //var nn2 = new NeuralNetwork2(); //nn2.Run(trainDataArray, trainTargetArray, testDataArray, testTargetArray); //create network var nn = new BPNeuralNetwork(normalizedPcaData[0].Length, hiddenNeuronCount, outputNeuronCount, min, max, testDataArray, testTargetArray); //var nn = new BPNeuralNetwork(normalizedPcaData[0].Length, hiddenNeuronCount, outputNeuronCount, min, max); //nn.CrossValidation(trainDataArray, trainTargetArray, trainingEpochCount, learningRate, learningMomentum, nn); //train network (double[] trainErrors, double[] validationErrors, double[] testErrors) = nn.Train(trainDataArray, trainTargetArray, trainingEpochCount, learningRate, learningMomentum); PlotValidationChart(trainErrors, validationErrors, testErrors); //string[] propertyNames = ReadPropertyNames("../../Data/property_names.csv", ','); //for (int i = 0; i < propertyNames.Length; i++) // Console.WriteLine(i + " | " + propertyNames[i]); //Test the network for (int i = 0; i < testDataArray.Length; i++) { nn.ComputeOutputs(testDataArray[i], testTargetArray[i]); } } else { //read data from file (double[][] dataArray, double[][] targetArray) = ReadDataAsArray(dataFile, ';', columnsToIgnore, targetColumns, dataStartLine, dataEndLine); //normalize all data values (scale between 0 and 1) (double[][] normalizedData, _, _) = NormalizeData(dataArray); (double[][] normalizedTargets, double[] min, double[] max) = NormalizeData(targetArray); //split data into two sets - training and testing = 80% and 20% (double[][] trainDataArray, double[][] trainTargetArray, double[][] testDataArray, double[][] testTargetArray) = SplitData(normalizedData, normalizedTargets, dataSplitPercentage); //(double[][] trainDataArray, double[][] trainTargetArray, double[][] testDataArray, double[][] testTargetArray) = SplitData(dataArray, targetArray, 0.8); //create network var nn = new BPNeuralNetwork(dataArray[0].Length, hiddenNeuronCount, outputNeuronCount, min, max, testDataArray, testTargetArray); //train network (double[] trainErrors, double[] validationErrors, double[] testErrors) = nn.Train(trainDataArray, trainTargetArray, trainingEpochCount, learningRate, learningMomentum); PlotValidationChart(trainErrors, validationErrors, testErrors); //string[] propertyNames = ReadPropertyNames("../../Data/property_names.csv", ','); //for (int i = 0; i < propertyNames.Length; i++) // Console.WriteLine(i + " | " + propertyNames[i]); //Test the network for (int i = 0; i < testDataArray.Length; i++) { nn.ComputeOutputs(testDataArray[i], testTargetArray[i]); } } Console.WriteLine(); Console.ReadLine(); }
static void KNN(List <List <double> > data) { Console.WriteLine("-------------------------------------"); Console.WriteLine("K-Nearest Neighbour method"); Console.WriteLine("-------------------------------------\n"); var normalized = NormalizeData(data); var pca = PCA.Compute(normalized, 2); (double[][] dataArray, double[][] targetArray) = ReadDataAsArray(dataFile, ';', columnsToIgnore, targetColumns, dataStartLine, dataEndLine); double[][] normalizedPcaData = List2DToArray2D(pca); (double[][] normalizedTargets, double[] min, double[] max) = NormalizeData(targetArray); //(var data1, var data2) = CrossValidationSplitData(normalizedPcaData, normalizedTargets, 10); (double[][] trainDataArray, double[][] trainTargetArray, double[][] testDataArray, double[][] testTargetArray) = SplitData(normalizedPcaData, normalizedTargets, dataSplitPercentage); //Application.EnableVisualStyles(); //Application.Run(new Diagrams(trainDataArray, trainTargetArray, min[0], max[0])); ChartValues <ObservablePoint> points1 = new ChartValues <ObservablePoint>(); ChartValues <ObservablePoint> points2 = new ChartValues <ObservablePoint>(); ChartValues <ObservablePoint> points3 = new ChartValues <ObservablePoint>(); ChartValues <ObservablePoint> points1Wrong = new ChartValues <ObservablePoint>(); ChartValues <ObservablePoint> points2Wrong = new ChartValues <ObservablePoint>(); ChartValues <ObservablePoint> points3Wrong = new ChartValues <ObservablePoint>(); int K = 131; int N = 200; int correct = 0; int wrong = 0; for (int i = 0; i < N; i++)//testDataArray.Length { int classIdx = KNearestNeighbor.Classify(testDataArray[i], trainDataArray, trainTargetArray, K, min[0], max[0]); Console.WriteLine("Classified: {0}", classIdx == 1?"0-50": classIdx == 2 ? "50-150" : ">150"); var actual = testTargetArray[i][0] * (max[0] - min[0]) + min[0]; Console.WriteLine("Actual: {0}\n", actual); if (classIdx == 1) { if (actual < 50) { correct++; points1.Add(new ObservablePoint { X = testDataArray[i][0], Y = testDataArray[i][1] }); } else { wrong++; points1Wrong.Add(new ObservablePoint { X = testDataArray[i][0], Y = testDataArray[i][1] }); } } else if (classIdx == 2) { if (actual > 150) { correct++; points2.Add(new ObservablePoint { X = testDataArray[i][0], Y = testDataArray[i][1] }); } else { wrong++; points2Wrong.Add(new ObservablePoint { X = testDataArray[i][0], Y = testDataArray[i][1] }); } } else { if (actual > 50 && actual < 150) { correct++; points3.Add(new ObservablePoint { X = testDataArray[i][0], Y = testDataArray[i][1] }); } else { wrong++; points3Wrong.Add(new ObservablePoint { X = testDataArray[i][0], Y = testDataArray[i][1] }); } } } Console.WriteLine("Accuracy: {0} %", (double)correct / N * 100); Application.EnableVisualStyles(); Application.Run(new Diagrams(points1, points2, points3, points1Wrong, points2Wrong, points3Wrong)); Console.WriteLine("-------------------------------------\n"); }