/// <summary> /// Creates a new (labeled) training set from a file. /// </summary> /// <param name="trainingSetFileName">The name of the file containing the training set.</param> public TrainingSet(string trainingSetFileName) { TextReader textReader = new StreamReader(trainingSetFileName); const char separator = ' '; // // 1. Read the input vector length and the input vector length. // string line = textReader.ReadLine(); string[] words = line.Split(separator); // Validate the input vector length. int inputVectorLength = Int32.Parse(words[0]); Utilities.NumberPositive(inputVectorLength, "inputVectorLength"); this.inputVectorLength = inputVectorLength; // Validate the output vector length. int outputVectorLength = Int32.Parse(words[1]); Utilities.NumberPositive(outputVectorLength, "outputVectorLength"); this.outputVectorLength = outputVectorLength; // Skip the blank line. textReader.ReadLine(); // // 2. Create the training patterns. // trainingPatterns = new List <TrainingPattern>(); while ((line = textReader.ReadLine()) != null) { words = line.Split(separator); // 2.1. Create the input vector. double[] inputVector = new double[inputVectorLength]; for (int i = 0; i < inputVectorLength; i++) { inputVector[i] = Double.Parse(words[i]); } // 2.2. Create the output vector length. double[] outputVector = new double[outputVectorLength]; for (int i = 0; i < outputVectorLength; i++) { outputVector[i] = Double.Parse(words[inputVectorLength + i]); } // 2.3. Add the training pattern into the training set. TrainingPattern trainingPattern = new TrainingPattern(inputVector, outputVector); Add(trainingPattern); } }
/// <summary> /// <para> /// Adds a (supervised) training pattern to the training set. /// </para> /// <para> /// Note that if the training pattern already exists in the set, it will be replaced. /// </para> /// </summary> /// <param name="trainingPattern">The training pattern to add.</param> /// <exception cref="ArgumentNullException"> /// Condition: <c>trainingPattern</c> is <c>null</c>. /// </exception> /// <exception cref="ArgumentException"> /// Condition: the lengths of input vector or output vector (from the training pattern) differ from their expected lengths (from the training set). /// </exception> public void Add(TrainingPattern trainingPattern) { // Validatate the arguments. Utilities.ObjectNotNull(trainingPattern, "trainingPattern"); // Validate the input vector length. if (trainingPattern.InputVector.Length != inputVectorLength) { throw new ArgumentException("The input vector must be of size " + inputVectorLength, "trainingPattern"); } // Validate the output vector length. if (trainingPattern.OutputVector.Length != outputVectorLength) { throw new ArgumentException("The output vector must be of size " + outputVectorLength, "trainingPattern"); } // Add the training pattern to the training set. trainingPatterns.Add(trainingPattern); }
/// <summary> /// Removes a training pattern from the training set. /// </summary> /// <param name="trainingPattern">The training sample to remove.</param> /// <returns> /// <c>True</c> if successful, <c>false</c> otherwise. /// </returns> public bool Remove(TrainingPattern trainingPattern) { return(trainingPatterns.Remove(trainingPattern)); }
/// <summary> /// Determines whether the training set contains a training pattern. /// </summary> /// <param name="trainingPattern">The training sample to check for containment.</param> /// <returns> /// <c>True</c> if contains, <c>false</c> otherwise. /// </returns> public bool Contains(TrainingPattern trainingPattern) { return(trainingPatterns.Contains(trainingPattern)); }