public ISolver simpleSeparation(int percentTrain) { int sizeTrainDataset = Convert.ToInt32(InputData.Length * ((double)percentTrain / 100)); int sizeTestDataset = InputData.Length - sizeTrainDataset; float[][] trainInputDataset = new float[sizeTrainDataset][]; float[][] testInputDataset = new float[InputData.Length - sizeTrainDataset][]; float[] trainOutputDataset = new float[sizeTrainDataset]; float[] testOutputDataset = new float[InputData.Length - sizeTrainDataset]; Array.Copy(InputData, trainInputDataset, sizeTrainDataset); Array.Copy(InputData, sizeTrainDataset, testInputDataset, 0, sizeTestDataset); Array.Copy(OutputData, trainOutputDataset, sizeTrainDataset); Array.Copy(OutputData, sizeTrainDataset, testOutputDataset, 0, sizeTestDataset); if (ISolver is INeuralNetwork) { LearningAlgoManager la = new LearningAlgoManager() { usedAlgo = LS.LearningAlgorithmName, GeneticParams = LS.LAParameters }; ClosingError = la.startLearn(ISolver, trainInputDataset, trainOutputDataset); } else if (ISolver is DecisionTree) { DecisionTreeLearning la = new DecisionTreeLearning(); ClosingError = la.startLearn(ISolver, trainInputDataset, trainOutputDataset); } PreprocessingManager preprocessing = new PreprocessingManager(); mistakeTrain = 0; List <string> expectedOutputValues = trainOutputDataset.Select(x => Convert.ToString(x)).ToList(); List <string> obtainedOutputValues = new List <string>(); for (int i = 0; i < sizeTrainDataset; i++) { obtainedOutputValues.Add(Convert.ToString(ISolver.Solve(trainInputDataset[i])[0])); } List <bool> comparisonOfResult = preprocessing.compareExAndObValues(expectedOutputValues, obtainedOutputValues, SelectionID, ParameterID); var counts = comparisonOfResult.GroupBy(x => x).ToDictionary(x => x.Key, x => x.Count()); mistakeTrain = (float)counts[false] / (float)sizeTrainDataset; mistakeTest = 0; expectedOutputValues = testOutputDataset.Select(x => Convert.ToString(x)).ToList(); obtainedOutputValues.Clear(); for (int i = 0; i < sizeTestDataset; i++) { obtainedOutputValues.Add(Convert.ToString(ISolver.Solve(testInputDataset[i])[0])); } comparisonOfResult = preprocessing.compareExAndObValues(expectedOutputValues, obtainedOutputValues, SelectionID, ParameterID); counts = comparisonOfResult.GroupBy(x => x).ToDictionary(x => x.Key, x => x.Count()); mistakeTest = (float)counts[false] / (float)sizeTestDataset; return(ISolver); }
private ISolver crossValidation(int folds) { ISolver curSolver = ISolver.Copy(); List <float> listOfTestMistakes = new List <float>(); for (int k = 0; k < folds; k++) { float kMistakeTrain = 0; float kMistakeTest = 0; float[][] trainInputDataset = GetInputTrainData(InputData, folds, k); float[][] testInputDataset = GetInputTestData(InputData, folds, k); float[] trainOutputDataset = GetOutputTrainData(OutputData, folds, k); float[] testOutputDataset = GetTestOutputData(OutputData, folds, k); LearningAlgoManager la = new LearningAlgoManager() { usedAlgo = LS.LearningAlgorithmName, GeneticParams = LS.LAParameters }; PreprocessingManager preprocessing = new PreprocessingManager(); ClosingError = la.startLearn(ISolver, trainInputDataset, trainOutputDataset); int sizeTrainDataset = trainInputDataset.Length; List <string> expectedOutputValues = trainOutputDataset.Select(x => Convert.ToString(x)).ToList(); List <string> obtainedOutputValues = new List <string>(); for (int i = 0; i < sizeTrainDataset; i++) { obtainedOutputValues.Add(Convert.ToString(ISolver.Solve(trainInputDataset[i])[0])); } List <bool> comparisonOfResult = preprocessing.compareExAndObValues(expectedOutputValues, obtainedOutputValues, SelectionID, ParameterID); var counts = comparisonOfResult.GroupBy(x => x).ToDictionary(x => x.Key, x => x.Count()); kMistakeTrain = (float)counts[false] / (float)sizeTrainDataset; int sizeTestDataset = testOutputDataset.Length; expectedOutputValues = testOutputDataset.Select(x => Convert.ToString(x)).ToList(); obtainedOutputValues.Clear(); for (int i = 0; i < sizeTestDataset; i++) { obtainedOutputValues.Add(Convert.ToString(ISolver.Solve(testInputDataset[i])[0])); } comparisonOfResult = preprocessing.compareExAndObValues(expectedOutputValues, obtainedOutputValues, SelectionID, ParameterID); counts = comparisonOfResult.GroupBy(x => x).ToDictionary(x => x.Key, x => x.Count()); kMistakeTest = (float)counts[false] / (float)sizeTestDataset; if (k != 0 && kMistakeTest < listOfTestMistakes.Min()) { curSolver = ISolver.Copy(); mistakeTest = kMistakeTest; mistakeTrain = kMistakeTrain; } listOfTestMistakes.Add(kMistakeTest); } ISolver = curSolver.Copy() as INeuralNetwork; return(ISolver); }