public void ApplyCrossValidation(int folds, double[][] inputs, int[] outputs) { crossValidation = CrossValidation.Create( k: 10, // We will be using 10-fold cross validation learner: (p) => new B.NaiveBayesLearning() // here we create the learning algorithm { }, // Now we have to specify how the tree performance should be measured: loss: (actual, expected, p) => new ZeroOneLoss(expected).Loss(actual), // This function can be used to perform any special // operations before the actual learning is done, but // here we will just leave it as simple as it can be: fit: (teacher, x, y, w) => teacher.Learn(x, y, w), // Finally, we have to pass the input and output data // that will be used in cross-validation. x: inputs.Select(v => v.Select(u => Convert.ToInt32(u)).ToArray()).ToArray(), y: outputs ); var result = crossValidation.Learn(inputs.Select(u => u.Select(v => Convert.ToInt32(v)).ToArray()).ToArray(), outputs); ConfusionMatrix = result.ToConfusionMatrix(inputs.Select(v => v.Select(u => Convert.ToInt32(u)).ToArray()).ToArray(), outputs).Matrix; }
public static void SVM_crossValidation_AnomalyClassifier(double[][] inputs, int[] outputs) { Accord.Math.Random.Generator.Seed = 0; var crossvalidation = new CrossValidation <SupportVectorMachine <Gaussian, double[]>, double[]>() { K = 10, // Use 3 folds in cross-validation //Learner = (s) => new SequentialMinimalOptimization<Gaussian, double[]>() //{ // Complexity = 10, // Kernel = new Gaussian(3) //}, Learner = (s) => new SequentialMinimalOptimization <Gaussian, double[]>() { Complexity = 10, Kernel = new Gaussian(2.6) }, // Indicate how the performance of those models will be measured Loss = (expected, actual, p) => new ZeroOneLoss(expected).Loss(actual), Stratify = true, // force balancing of classes }; // If needed, control the parallelization degree crossvalidation.ParallelOptions.MaxDegreeOfParallelism = 1; // Compute the cross-validation var result = crossvalidation.Learn(inputs, outputs); // Finally, access the measured performance. double trainingErrors = result.Training.Mean; double validationErrors = result.Validation.Mean; // If desired, compute an aggregate confusion matrix for the validation sets: GeneralConfusionMatrix gcm = result.ToConfusionMatrix(inputs, outputs); double accuracy = gcm.Accuracy; double error = gcm.Error; Console.WriteLine("Accuracy:" + gcm.Accuracy); Console.WriteLine("Error:" + gcm.Error); Console.WriteLine("First Anomaly type Precision:" + gcm.Precision[0]); Console.WriteLine("First Anomaly type Recall:" + gcm.Recall[0]); Console.WriteLine("Second Anomaly type Precision:" + gcm.Precision[1]); Console.WriteLine("Second Anomaly type Recall:" + gcm.Recall[1]); //double anomalyFScore = 2 * (gcm.Precision[1] * gcm.Recall[1]) / (gcm.Precision[1] + gcm.Recall[1]); //double NotAnomalyFScore = 2 * (gcm.Precision[0] * gcm.Recall[0]) / (gcm.Precision[0] + gcm.Recall[0]); //Console.WriteLine("Not ANomaly F-score:" + NotAnomalyFScore); //Console.WriteLine("Anomaly F-score:" + anomalyFScore); Console.WriteLine("First Anomaly type F-score:" + gcm.PerClassMatrices[0].FScore); Console.WriteLine("Second Anomaly type F-score:" + gcm.PerClassMatrices[1].FScore); //var splitset = new SplitSetValidation<MulticlassSupportVectorMachine<Gaussian, double[]>, double[]>() //{ // Learner = (s) => new MulticlassSupportVectorLearning<Gaussian, double[]>() // { // Learner = (m) => new SequentialMinimalOptimization<Gaussian, double[]>() // { // Complexity = 10, // Kernel = new Gaussian(2.92) // } // } // ,Stratify =true //}; //var result = splitset.Learn(inputs, outputs); //double trainingErrors = result.Training.Value; //double validationErrors = result.Validation.Value; }
// public static void NaiveBaise(double[][] chain) // { // DataTable data = new DataTable("dataa"); // data.Rows.Add() // data.Columns.AddRange( "speedAvg1", "speedAvg2", "speedAvg3", "speedAvg4", "speedAvg5", "speedAvg6", "ContainAnomaly"); // Codification codebook = new Codification(data, //"speedAvg1", "speedAvg2", "speedAvg3", "speedAvg4", "speedAvg5", "speedAvg6", "ContainAnomaly"); // // Extract input and output pairs to train // DataTable symbols = codebook.Apply(data); // int[][] inputs = symbols.ToArray<int>("Outlook", "Temperature", "Humidity", "Wind"); // int[] outputs = symbols.ToArray<int>("PlayTennis"); // } public static void SVM_crossValidation_AnomalyDetection(double[][] inputs, int[] outputs) { Accord.Math.Random.Generator.Seed = 0; var crossvalidation = new CrossValidation <SupportVectorMachine <Gaussian, double[]>, double[]>() { K = 100, // Use 3 folds in cross-validation // Indicate how learning algorithms for the models should be created //Learner = (s) => new SequentialMinimalOptimization<Gaussian, double[]>() //{ // Complexity = 10, // Kernel = new Gaussian(0.5) //}, //if ssegments beside hyper removed //Learner = (s) => new SequentialMinimalOptimization<Gaussian, double[]>() //{ // Complexity = 100, // Kernel = new Gaussian(1) //}, //with max and min speed variance //Learner = (s) => new SequentialMinimalOptimization<Gaussian, double[]>() //{ // Complexity = 100, // Kernel = new Gaussian(1.55) //}, //Learner = (s) => new SequentialMinimalOptimization<Gaussian, double[]>() //{ // Complexity = 15, // Kernel = new Gaussian(1.55) //}, //Learner = (s) => new SequentialMinimalOptimization<Gaussian, double[]>() //{ // Complexity = 7, // Kernel = new Gaussian(1.9) //}, //best results for 7 segments witrh fields :sc.MaxSpeed.Value, sc.MinSpeed.Value, sc.SpeedRange.Value, sc.TotalTime.Value,sc.MinTime.Value,sc.MaxTime.Value, sc.TimeRange.Value,sc.MaxSpeedVar.Value,sc.MinSpeedVar.Value, sc.SpeedVarRange.Value //Learner = (s) => new SequentialMinimalOptimization<Gaussian, double[]>() //{ // Complexity = 7, // Kernel = new Gaussian(2.4) //}, //Best of the best //Learner = (s) => new SequentialMinimalOptimization<Gaussian, double[]>() //{ // Complexity = 7, // Kernel = new Gaussian(2.4) //}, //for Roadscanner DB //Learner = (s) => new SequentialMinimalOptimization<Gaussian, double[]>() //{ // Complexity = 10, // Kernel = new Gaussian(2.92) //}, //for Roadscanner3 DB //without pothole //Learner = (s) => new SequentialMinimalOptimization<Gaussian, double[]>() //{ // Complexity = 10, // Kernel = new Gaussian(2.1) //}, //for Roadscanner3 DB //with pothole //Learner = (s) => new SequentialMinimalOptimization<Gaussian, double[]>() //{ // Complexity = 15, // Kernel = new Gaussian(2.5) //}, //RoadScanner3 Db //with pothole // Learner = (s) => new SequentialMinimalOptimization <Gaussian, double[]>() { Complexity = 15, Kernel = new Gaussian(3) }, // Indicate how the performance of those models will be measured Loss = (expected, actual, p) => new ZeroOneLoss(expected).Loss(actual), Stratify = true, // force balancing of classes }; // If needed, control the parallelization degree crossvalidation.ParallelOptions.MaxDegreeOfParallelism = 1; // Compute the cross-validation var result = crossvalidation.Learn(inputs, outputs); // Finally, access the measured performance. double trainingErrors = result.Training.Mean; double validationErrors = result.Validation.Mean; // If desired, compute an aggregate confusion matrix for the validation sets: GeneralConfusionMatrix gcm = result.ToConfusionMatrix(inputs, outputs); double accuracy = gcm.Accuracy; double error = gcm.Error; Console.WriteLine("Accuracy:" + gcm.Accuracy); Console.WriteLine("Error:" + gcm.Error); Console.WriteLine("Not Anomaly Precision:" + gcm.Precision[0]); Console.WriteLine("Not Anomaly Recall:" + gcm.Recall[0]); Console.WriteLine("Anomaly Precision:" + gcm.Precision[1]); Console.WriteLine("Anomaly Recall:" + gcm.Recall[1]); //double anomalyFScore = 2 * (gcm.Precision[1] * gcm.Recall[1]) / (gcm.Precision[1] + gcm.Recall[1]); //double NotAnomalyFScore = 2 * (gcm.Precision[0] * gcm.Recall[0]) / (gcm.Precision[0] + gcm.Recall[0]); //Console.WriteLine("Not ANomaly F-score:" + NotAnomalyFScore); //Console.WriteLine("Anomaly F-score:" + anomalyFScore); Console.WriteLine("Not ANomaly F-score:" + gcm.PerClassMatrices[0].FScore); Console.WriteLine("Anomaly F-score:" + gcm.PerClassMatrices[1].FScore); }
public void learn_hmm() { #region doc_learn_hmm // Ensure results are reproducible Accord.Math.Random.Generator.Seed = 0; // This is a sample code on how to use Cross-Validation // to assess the performance of Hidden Markov Models. // Declare some testing data int[][] inputs = new int[][] { new int[] { 0, 1, 1, 0 }, // Class 0 new int[] { 0, 0, 1, 0 }, // Class 0 new int[] { 0, 1, 1, 1, 0 }, // Class 0 new int[] { 0, 1, 1, 1, 0 }, // Class 0 new int[] { 0, 1, 1, 0 }, // Class 0 new int[] { 0, 0, 0, 0, 0 }, // Class 1 new int[] { 0, 0, 0, 1, 0 }, // Class 1 new int[] { 0, 0, 0, 0, 0 }, // Class 1 new int[] { 0, 0, 0 }, // Class 1 new int[] { 0, 0, 0, 0 }, // Class 1 new int[] { 1, 0, 0, 1 }, // Class 2 new int[] { 1, 1, 0, 1 }, // Class 2 new int[] { 1, 0, 0, 0, 1 }, // Class 2 new int[] { 1, 0, 1 }, // Class 2 new int[] { 1, 1, 0, 1 }, // Class 2 }; int[] outputs = new int[] { 0, 0, 0, 0, 0, // First 5 sequences are of class 0 1, 1, 1, 1, 1, // Middle 5 sequences are of class 1 2, 2, 2, 2, 2, // Last 5 sequences are of class 2 }; // Create a new Cross-validation algorithm passing the data set size and the number of folds var crossvalidation = new CrossValidation <HiddenMarkovClassifier, int[]>() { K = 3, // Use 3 folds in cross-validation Learner = (s) => new HiddenMarkovClassifierLearning() { Learner = (p) => new BaumWelchLearning() { NumberOfStates = 3 } }, Loss = (expected, actual, p) => { var cm = new GeneralConfusionMatrix(classes: p.Model.NumberOfClasses, expected: expected, predicted: actual); p.Variance = cm.Variance; return(p.Value = cm.Kappa); }, Stratify = false, }; // If needed, control the parallelization degree crossvalidation.ParallelOptions.MaxDegreeOfParallelism = 1; // Compute the cross-validation var result = crossvalidation.Learn(inputs, outputs); // Finally, access the measured performance. double trainingErrors = result.Training.Mean; double validationErrors = result.Validation.Mean; double trainingErrorVar = result.Training.Variance; double validationErrorVar = result.Validation.Variance; double trainingErrorPooledVar = result.Training.PooledVariance; double validationErrorPooledVar = result.Validation.PooledVariance; #endregion Assert.AreEqual(5, result.AverageNumberOfSamples); Assert.AreEqual(15, result.NumberOfSamples); Assert.AreEqual(0, result.NumberOfInputs); Assert.AreEqual(3, result.NumberOfOutputs); Assert.AreEqual(3, result.Models.Length); for (int i = 0; i < 3; i++) { Assert.AreEqual(0, result.Models[i].NumberOfInputs); Assert.AreEqual(3, result.Models[i].NumberOfOutputs); Assert.AreEqual(3, result.Models[i].Model.NumberOfClasses); Assert.AreEqual(0, result.Models[i].Model.NumberOfInputs); Assert.AreEqual(3, result.Models[i].Model.NumberOfOutputs); Assert.AreEqual(10, result.Models[i].Training.NumberOfSamples); Assert.AreEqual(5, result.Models[i].Validation.NumberOfSamples); } Assert.AreEqual(3, crossvalidation.K); Assert.AreEqual(1.0, result.Training.Mean, 1e-10); Assert.AreEqual(0.78472222222222232, result.Validation.Mean, 1e-10); Assert.AreEqual(0, result.Training.Variance, 1e-10); Assert.AreEqual(0.034866898148148126, result.Validation.Variance, 1e-10); Assert.AreEqual(0, result.Training.StandardDeviation, 1e-10); Assert.AreEqual(0.18672680082984372, result.Validation.StandardDeviation, 1e-10); Assert.AreEqual(0, result.Training.PooledVariance); Assert.AreEqual(0.045256528501157391, result.Validation.PooledVariance); Assert.AreEqual(0, result.Training.PooledStandardDeviation); Assert.AreEqual(0.21273581856649668, result.Validation.PooledStandardDeviation); Assert.AreEqual(3, crossvalidation.Folds.Length); Assert.AreEqual(3, result.Models.Length); }
public void learn_test() { #region doc_learn // Ensure results are reproducible Accord.Math.Random.Generator.Seed = 0; // This is a sample code on how to use Cross-Validation // to assess the performance of Support Vector Machines. // Consider the example binary data. We will be trying // to learn a XOR problem and see how well does SVMs // perform on this data. double[][] data = { new double[] { -1, -1 }, new double[] { 1, -1 }, new double[] { -1, 1 }, new double[] { 1, 1 }, new double[] { -1, -1 }, new double[] { 1, -1 }, new double[] { -1, 1 }, new double[] { 1, 1 }, new double[] { -1, -1 }, new double[] { 1, -1 }, new double[] { -1, 1 }, new double[] { 1, 1 }, new double[] { -1, -1 }, new double[] { 1, -1 }, new double[] { -1, 1 }, new double[] { 1, 1 }, }; int[] xor = // result of xor for the sample input data { -1, 1, 1, -1, -1, 1, 1, -1, -1, 1, 1, -1, -1, 1, 1, -1, }; // Create a new Cross-validation algorithm passing the data set size and the number of folds var crossvalidation = new CrossValidation <SupportVectorMachine <Linear, double[]>, double[]>() { K = 3, // Use 3 folds in cross-validation // Indicate how learning algorithms for the models should be created Learner = (s) => new SequentialMinimalOptimization <Linear, double[]>() { Complexity = 100 }, // Indicate how the performance of those models will be measured Loss = (expected, actual, p) => new ZeroOneLoss(expected).Loss(actual), Stratify = false, // do not force balancing of classes }; // If needed, control the parallelization degree crossvalidation.ParallelOptions.MaxDegreeOfParallelism = 1; // Compute the cross-validation var result = crossvalidation.Learn(data, xor); // Finally, access the measured performance. double trainingErrors = result.Training.Mean; double validationErrors = result.Validation.Mean; #endregion Assert.AreEqual(3, crossvalidation.K); Assert.AreEqual(0.37575757575757579, result.Training.Mean, 1e-10); Assert.AreEqual(0.75555555555555554, result.Validation.Mean, 1e-10); Assert.AreEqual(0.00044077134986225924, result.Training.Variance, 1e-10); Assert.AreEqual(0.0059259259259259334, result.Validation.Variance, 1e-10); Assert.AreEqual(0.020994555243259126, result.Training.StandardDeviation, 1e-10); Assert.AreEqual(0.076980035891950155, result.Validation.StandardDeviation, 1e-10); Assert.AreEqual(0, result.Training.PooledStandardDeviation); Assert.AreEqual(0, result.Validation.PooledStandardDeviation); Assert.AreEqual(3, crossvalidation.Folds.Length); Assert.AreEqual(3, result.Models.Length); }
public void learn_test() { #region doc_learn // Ensure results are reproducible Accord.Math.Random.Generator.Seed = 0; // This is a sample code on how to use Cross-Validation // to assess the performance of Support Vector Machines. // Consider the example binary data. We will be trying // to learn a XOR problem and see how well does SVMs // perform on this data. double[][] data = { new double[] { -1, -1 }, new double[] { 1, -1 }, new double[] { -1, 1 }, new double[] { 1, 1 }, new double[] { -1, -1 }, new double[] { 1, -1 }, new double[] { -1, 1 }, new double[] { 1, 1 }, new double[] { -1, -1 }, new double[] { 1, -1 }, new double[] { -1, 1 }, new double[] { 1, 1 }, new double[] { -1, -1 }, new double[] { 1, -1 }, new double[] { -1, 1 }, new double[] { 1, 1 }, }; int[] xor = // result of xor for the sample input data { -1, 1, 1, -1, -1, 1, 1, -1, -1, 1, 1, -1, -1, 1, 1, -1, }; // Create a new Cross-validation algorithm passing the data set size and the number of folds var crossvalidation = new CrossValidation <SupportVectorMachine <Linear, double[]>, double[]>() { K = 3, // Use 3 folds in cross-validation // Indicate how learning algorithms for the models should be created Learner = (s) => new SequentialMinimalOptimization <Linear, double[]>() { Complexity = 100 }, // Indicate how the performance of those models will be measured Loss = (expected, actual, p) => new ZeroOneLoss(expected).Loss(actual), Stratify = false, // do not force balancing of classes }; // If needed, control the parallelization degree crossvalidation.ParallelOptions.MaxDegreeOfParallelism = 1; // Compute the cross-validation var result = crossvalidation.Learn(data, xor); // Finally, access the measured performance. double trainingErrors = result.Training.Mean; // should be 0.30606060606060609 (+/- var. 0.083498622589531682) double validationErrors = result.Validation.Mean; // should be 0.3666666666666667 (+/- var. 0.023333333333333334) // If desired, compute an aggregate confusion matrix for the validation sets: GeneralConfusionMatrix gcm = result.ToConfusionMatrix(data, xor); double accuracy = gcm.Accuracy; // should be 0.625 double error = gcm.Error; // should be 0.375 #endregion Assert.AreEqual(16, gcm.Samples); Assert.AreEqual(0.625, accuracy); Assert.AreEqual(0.375, error); Assert.AreEqual(2, gcm.Classes); Assert.AreEqual(3, crossvalidation.K); Assert.AreEqual(0.30606060606060609, result.Training.Mean, 1e-10); Assert.AreEqual(0.3666666666666667, result.Validation.Mean, 1e-10); Assert.AreEqual(0.083498622589531682, result.Training.Variance, 1e-10); Assert.AreEqual(0.023333333333333334, result.Validation.Variance, 1e-10); Assert.AreEqual(0.28896128216342704, result.Training.StandardDeviation, 1e-10); Assert.AreEqual(0.15275252316519467, result.Validation.StandardDeviation, 1e-10); Assert.AreEqual(0, result.Training.PooledStandardDeviation); Assert.AreEqual(0, result.Validation.PooledStandardDeviation); Assert.AreEqual(3, crossvalidation.Folds.Length); Assert.AreEqual(3, result.Models.Length); }