/// <summary> /// Trains the classifier and computes the training error if option provided. /// </summary> /// <param name="trainingData">The training data that will be used to train classifier.</param> /// <param name="trainingLabels">The training labels related to provided training data.</param> /// <param name="calculateError">The boolean check to tell if the training error should be calculated.</param> public override void Train(List <double[]> trainingData, List <int> trainingLabels, bool calculateError = true) { Model = LearningAlgorithm.Learn(trainingData.ToArray(), trainingLabels.ToArray()); if (calculateError == true) { CalculateTrainingError(trainingData, trainingLabels); } }
public override void Train(List <double[]> trainingData, List <double> trainingLabels, bool calculateError = true) { int[] TrainingLabels = TypeCasters.DoubleArrayToInt(trainingLabels).ToArray(); double[][] TrainingData = trainingData.ToArray(); Model = LearningAlgorithm.Learn(TrainingData, TrainingLabels); if (calculateError == true) { CalculateTrainingError(trainingData, trainingLabels); } }
public void Train(List <TrainingValue> trainingData) { List <DecisionVariable> trainingVariables = new List <DecisionVariable>(); for (int i = 0; i < featureSize; i++) { trainingVariables.Add(DecisionVariable.Continuous(i.ToString())); } tree = new DecisionTree(inputs: trainingVariables, classes: 2); double[][] featuresArray = new double[trainingData.Count][]; int[] labels = new int[trainingData.Count]; for (int i = 0; i < featuresArray.Length; i++) { featuresArray[i] = trainingData[i].Features; labels[i] = Convert.ToInt32(trainingData[i].State); } switch (type) { case ClassifierType.DecisionTree: C45Learning teacher = new C45Learning(tree); teacher.Learn(featuresArray, labels); break; case ClassifierType.LDA: LinearDiscriminantAnalysis lda = new LinearDiscriminantAnalysis(); pipeline = lda.Learn(featuresArray, labels); break; case ClassifierType.SVM: LinearCoordinateDescent svmLearner = new LinearCoordinateDescent(); svm = svmLearner.Learn(featuresArray, labels); break; case ClassifierType.Bayes: NaiveBayesLearning <NormalDistribution> learner = new NaiveBayesLearning <NormalDistribution>(); bayes = learner.Learn(featuresArray, labels); break; } Trained = true; }
/// <summary> /// Loads the trained model. /// </summary> /// <param name="path">The location from where to load the trained model.</param> public override void Load(string path) { Model = Accord.IO.Serializer.Load <LinearDiscriminantAnalysis.Pipeline>(path); }