/// <summary> /// Moves an example from the training set to /// the testing set. If the supplied example number /// is out of range no example is moved. /// </summary> public void moveToTestingSet(int exampleNum) { if (exampleNum < 0 || exampleNum > (TrainingSet.Count - 1)) { return; } TestingSet.Add((int[])TrainingSet.ElementAt(exampleNum)); TrainingSet.RemoveAt(exampleNum); }
/// <summary> /// Creates a random testing dataset. Calling this /// method will destroy any previously built testing set. /// </summary> /// <param name="percentage">Percentage of the entire dataset to use for testing.</param> /// <param name="balanced">to create a balanced /// testing set, where the testing set and the /// remaining training set have the same proportion /// of each class.</param> public void createRndTestSet(int percentage, bool balanced) { if (percentage < 0 || percentage > 100) { throw new Exception("Percentage value out of range."); } // Move any examples that are part of the current testing // set back to the training set. for (int i = 0; i < TestingSet.Count; i++) { TrainingSet.Add((int[])TestingSet[i]); } TestingSet.Clear(); // Calculate the number of examples that should be // in the testing set. int totalNumExamples = TrainingSet.Count; int numTestingExamples = (int)Math.Round(totalNumExamples * ((float)percentage) / 100.0f); Random rand = new Random(); // If the set doesn't have to be balanced, then just // pick examples at random. if (!balanced) { for (int i = 0; i < numTestingExamples; i++) { int temp = rand.Next(TrainingSet.Count); TestingSet.Add((int[])TrainingSet[temp]); TrainingSet.RemoveAt(temp); } } else { // We have the target value distribution for the dataset, // so reference it. for (int i = 0; i < TargetSums.Length; i++) { int numExamplesToMove = (int)Math.Round(TargetSums[i] / ((float)totalNumExamples) * numTestingExamples); for (int j = 0; j < numExamplesToMove; j++) { // Attempt to randomly pick examples from the // dataset that have the required target classification. int[] example = null; while (true) { example = (int[])TrainingSet[rand.Next(TrainingSet.Count)]; if (example[0] == i) { break; } } int temp = TrainingSet.IndexOf(example); TestingSet.Add((int[])TrainingSet[temp]); TrainingSet.RemoveAt(temp); } } } }