using System; using Accord.Math.Optimization.Losses; using Accord.Statistics; using Accord.Statistics.Analysis; using Accord.MachineLearning.DecisionTrees; using Accord.Statistics.Models.Regression; using Accord.Statistics.Models.Regression.Fitting; using Accord.Statistics.Models.Regression.Linear; using Accord.MachineLearning; using Accord.MachineLearning.VectorMachines.Learning; using Accord.Statistics.Kernels; using Accord.MachineLearning.VectorMachines; using Accord.MachineLearning.Bayes; using Accord.Math; using System.Windows; using System.Security.Cryptography; using Accord.MachineLearning.Boosting; using Accord.MachineLearning.DecisionTrees.Learning; using Accord.MachineLearning.Boosting.Learners; using System.Security.AccessControl; using Accord.Statistics.Models.Fields.Learning; using Accord.Math.Optimization; using Accord.Math.Integration; using System.Diagnostics; using Accord.Math.Geometry; namespace ConsoleApp3 { internal class Program2 { [Obsolete] private static void Main(string[] args) { Stopwatch stopwatch = new Stopwatch(); Stopwatch sw1 = new Stopwatch(); Stopwatch sw2 = new Stopwatch(); Stopwatch sw3 = new Stopwatch(); Stopwatch sw4 = new Stopwatch(); Stopwatch sw5 = new Stopwatch(); Stopwatch sw6 = new Stopwatch(); Stopwatch sw7 = new Stopwatch(); Stopwatch sw8 = new Stopwatch(); Stopwatch swarr = new Stopwatch(); stopwatch.Start(); stopwatch.Start(); int sample = 30000; // max size is 36284 double[][] inputsLH = new double[sample][]; bool[] outputStatus = new bool[sample]; var Filedata = File.ReadAllLines(@"booking.csv"); swarr.Start(); /* Parallel.For(0, sample, i => { var delimited = Filedata[i].Split(','); inputsLH[i] = new double[] { Convert.ToDouble(delimited[0]), Convert.ToDouble(delimited[1]) }; outputStatus[i] = Convert.ToBoolean(delimited[2].Trim()); });*/ for (int i = 0; i < sample; i++)// => { var delimited = Filedata[i].Split(','); inputsLH[i] = new double[] { Convert.ToDouble(delimited[0]), Convert.ToDouble(delimited[1]) }; outputStatus[i] = Convert.ToBoolean(delimited[2].Trim()); } swarr.Stop(); /*double[][] inputsLH = new double[12][]; { //independent variables: lowTemp, Hightemp inputsLH[0] = new double[] { 25, 30 }; // input day 1 inputsLH[1] = new double[] { 21, 34 }; // input day 2 inputsLH[2] = new double[] { 20, 27 }; // input day 3 inputsLH[3] = new double[] { 22, 32 }; // input day 4 inputsLH[4] = new double[] { 20, 30 }; // input day 5 inputsLH[5] = new double[] { 24, 33 }; // input day 6 inputsLH[6] = new double[] { 25, 31 }; // input day 7 inputsLH[7] = new double[] { 24, 31 }; // input day 8 inputsLH[8] = new double[] { 20, 27 }; // input day 9 inputsLH[9] = new double[] { 21, 29 }; // input day 10 inputsLH[10] = new double[] { 18, 20 }; // input day 11 inputsLH[11] = new double[] { 22, 26 }; // input day 12 } bool[] outputStatus = { true, true, true, false, false, false, true, false, true, true, false, true, }; double[][] outputsQty = new double[12][]; //Quantity for regression { // variables: Cooking Oil, Vegetables outputsQty[0] = new double[] { 9, 27.5 }; // input day 1 outputsQty[1] = new double[] { 9, 22.5 }; // input day 2 outputsQty[2] = new double[] { 23, 32.5 }; // input day 3 outputsQty[3] = new double[] { 11, 35 }; // input day 4 outputsQty[4] = new double[] { 20.5, 21 }; // input day 5 outputsQty[5] = new double[] { 20, 26 }; // input day 6 outputsQty[6] = new double[] { 9, 35 }; // input day 7 outputsQty[7] = new double[] { 9, 25 }; // input day 8 outputsQty[8] = new double[] { 9, 22.5 }; // input day 9 outputsQty[9] = new double[] { 6, 20 }; // input day 10 outputsQty[10] = new double[] { 12, 37.5 }; // input day 11 outputsQty[11] = new double[] { 9, 22.5 }; // input day 12 } //For Support Vector Machine Quantities // double[] outSVMQtyCo = { 9, 9, 23, 11, 20.5, 26, 9, 9, 9, 6, 12, 9 }; // Cooking Oil */ double[][] test ={ // variables: x1 x2 new double[] { 21, 30 } }; double[] test1 = { 30, 35 }; Accord.Math.Random.Generator.Seed = 0; int ite = 100; double tor = 1e-4; var learnPCD = new ProbabilisticCoordinateDescent() { Tolerance = tor, Complexity = ite, // learn a hard-margin model MaximumIterations = ite, }; var learnSMOP = new SequentialMinimalOptimization() { Tolerance = tor, // Let's set some convergence parameters Kernel = new Polynomial(2), // Polynomial Kernel of 2nd degree UseComplexityHeuristic = true, //Complexity = 150 //Complexity = 200 }; var learnSMOG = new SequentialMinimalOptimization { Tolerance = tor, // Let's set some convergence parameters UseKernelEstimation = true, //UseComplexityHeuristic = true, Complexity = ite }; var learnIRLS = new IterativeReweightedLeastSquares() { Tolerance = tor, // Let's set some convergence parameters Regularization = 0, MaxIterations = ite, }; var learnFCLS = new FanChenLinSupportVectorRegression() { Tolerance = tor, UseKernelEstimation = true, UseComplexityHeuristic = true, Kernel = new Gaussian(0.1), Complexity = ite,// maximum number of iterations to perform 100 }; var learnLRNM = new LinearRegressionNewtonMethod() { Tolerance = tor, UseComplexityHeuristic = true, MaximumIterations = ite, // Complexity = 150, // maximum number of iterations to perform 200 }; //esembles var learnTHL = new ThresholdLearning(); var learnAdBDS = new AdaBoost() { Learner = (p) => new ThresholdLearning(), MaxIterations = ite, Tolerance = tor }; var learnAdBRL = new AdaBoost() { Learner = (param) => new IterativeReweightedLeastSquares() { ComputeStandardErrors = false, MaxIterations = ite, Tolerance = tor }, MaxIterations = ite, Tolerance = tor, }; var learnAdBDT = new AdaBoost() { Learner = (param) => new C45Learning() { MaxHeight = 50, MaxVariables = 50, }, MaxIterations = ite, Tolerance = tor, }; // Using the learner to estimate the model double PCD = 0, IRLS = 0, SMOP = 0, SMOG = 0, THL = 0, AdBDS = 0, AdBRL = 0, AdBDT = 0; bool bPCD = true, bIRLS = true, bSMOP = true, bSMOG = true, bTHL = true, bAdBDS = true, bAdBRL = true, bAdBDT = true; double[] EseMode = new double[8]; string[] EseModeEr = new string[8]; Parallel.Invoke( () => { sw1.Start(); var modelPCD = learnPCD.Learn(inputsLH, outputStatus);sw1.Stop(); PCD = modelPCD.Compute(test[0]); bPCD = modelPCD.Decide(test[0]); ConfusionMatrix cPC = ConfusionMatrix.Estimate(modelPCD, inputsLH, outputStatus); EseMode[0] = cPC.MatthewsCorrelationCoefficient; EseModeEr[0] = cPC.Error + "\t Coefficient " + cPC.MatthewsCorrelationCoefficient; }, () => { sw2.Start(); var modelIRLS = learnIRLS.Learn(inputsLH, outputStatus);sw2.Stop(); IRLS = modelIRLS.Compute(test[0]); bIRLS = modelIRLS.Decide(test[0]); ConfusionMatrix cIR = ConfusionMatrix.Estimate(modelIRLS, inputsLH, outputStatus); EseMode[1] = cIR.MatthewsCorrelationCoefficient; EseModeEr[1] = cIR.Error + "\t Coefficient " + cIR.MatthewsCorrelationCoefficient; }, () => { sw3.Start(); var modelSMOP = learnSMOP.Learn(inputsLH, outputStatus);sw3.Stop(); SMOP = modelSMOP.Compute(test[0]); bSMOP = modelSMOP.Decide(test[0]); ConfusionMatrix cSMP = ConfusionMatrix.Estimate(modelSMOP, inputsLH, outputStatus); EseMode[2] = cSMP.MatthewsCorrelationCoefficient; EseModeEr[2] = cSMP.Error + "\t Coefficient " + cSMP.MatthewsCorrelationCoefficient; }, /*() => { var modelSMOG = learnSMOG.Learn(inputsLH, outputStatus); SMOG = modelSMOG.Compute(test[0]); bSMOG = modelSMOG.Decide(test[0]); ConfusionMatrix cSMG = ConfusionMatrix.Estimate(modelSMOG, inputsLH, outputStatus); EseMode[3] = cSMG.MatthewsCorrelationCoefficient; EseModeEr[3] = cSMG.Error + "\t Coefficient " + cSMG.MatthewsCorrelationCoefficient; },*/ () => { sw5.Start(); var modelTHL = learnTHL.Learn(inputsLH, outputStatus);sw5.Stop(); THL = modelTHL.Compute(test[0]); bTHL = modelTHL.Decide(test[0]); ConfusionMatrix cTH = ConfusionMatrix.Estimate(modelTHL, inputsLH, outputStatus); EseMode[4] = cTH.MatthewsCorrelationCoefficient; EseModeEr[4] = cTH.Error + "\t Coefficient " + cTH.MatthewsCorrelationCoefficient; }, () => { sw6.Start(); var modelAdBDS = learnAdBDS.Learn(inputsLH, outputStatus);sw6.Stop(); AdBDS = modelAdBDS.Compute(test[0]); bAdBDS = modelAdBDS.Decide(test[0]); ConfusionMatrix cBDS = ConfusionMatrix.Estimate(modelAdBDS, inputsLH, outputStatus); EseMode[5] = cBDS.MatthewsCorrelationCoefficient; EseModeEr[5] = cBDS.Error + "\t Coefficient " + cBDS.MatthewsCorrelationCoefficient; }, () => { sw7.Start(); var modelAdBRL = learnAdBRL.Learn(inputsLH, outputStatus); sw7.Stop(); AdBRL = modelAdBRL.Compute(test[0]); bAdBRL = modelAdBRL.Decide(test[0]); ConfusionMatrix cBRL = ConfusionMatrix.Estimate(modelAdBRL, inputsLH, outputStatus); EseMode[6] = cBRL.MatthewsCorrelationCoefficient; EseModeEr[6] = cBRL.Error + "\t Coefficient " + cBRL.MatthewsCorrelationCoefficient; }, () => { sw8.Start(); var modelAdBDT = learnAdBDT.Learn(inputsLH, outputStatus); sw8.Stop(); modelAdBDT.Compute(test[0]); bAdBDT = modelAdBDT.Decide(test[0]); ConfusionMatrix cBDT = ConfusionMatrix.Estimate(modelAdBDT, inputsLH, outputStatus); EseMode[7] = cBDT.MatthewsCorrelationCoefficient; EseModeEr[7] = cBDT.Error + "\t Coefficient " + cBDT.MatthewsCorrelationCoefficient; } ); bool[] EseArr = new bool[8]; EseArr[0] = bPCD; EseArr[1] = bIRLS; EseArr[2] = bSMOP; EseArr[3] = bSMOG; EseArr[4] = bTHL; EseArr[5] = bAdBDS; EseArr[6] = bAdBRL; EseArr[7] = bAdBDT; int MleMLQTYCox = -1, MleMLQTYCon = -1; //the index for best model and the second best model double cmdx = int.MinValue, cmdn = int.MinValue; //cmdx is the caem is an array of models' errors Parallel.For(0, EseMode.Length, i =>// for (int i = 0; i < sm; i++) { if (EseMode[i] > cmdx) { cmdn = cmdx; MleMLQTYCon = MleMLQTYCox; cmdx = EseMode[i];//selecting the best mode MleMLQTYCox = i; } else if (EseMode[i] > cmdn && EseMode[i] < cmdx) { cmdn = EseMode[i]; //selecting the 2nd best model MleMLQTYCon = i; } }); bool[] Ese2B = { EseArr[MleMLQTYCox], EseArr[MleMLQTYCon] }; { Console.WriteLine("\nEsmbles"); { Console.WriteLine(bPCD + "\t with " + PCD + "\t Error " + EseModeEr[0].ToString()); Console.WriteLine(bIRLS + "\t with " + IRLS + "\t error " + EseModeEr[1].ToString()); Console.WriteLine(bSMOP + "\t with " + SMOP + "\t error " + EseModeEr[2].ToString()); //Console.WriteLine(bSMOG + "\t with " + SMOG + "\t error " + EseModeEr[3].ToString()); Console.WriteLine(bTHL + "\t with " + THL + "\t error " + EseModeEr[4].ToString()); Console.WriteLine(bAdBDS + "\t with " + AdBDS + "\t error " + EseModeEr[5].ToString()); Console.WriteLine(bAdBRL + "\t with " + AdBRL + "\t error " + EseModeEr[6].ToString()); Console.WriteLine(bAdBDT + "\t with " + AdBDT + "\t error " + EseModeEr[7].ToString()); } Console.WriteLine("Best models Indece: " + MleMLQTYCox + " and " + MleMLQTYCon); Console.WriteLine("Best 2 voted QTYCo values: " + Ese2B.Mode()); Console.WriteLine("Mode QTYCo values: " + EseArr.Mode()); Console.Write(""); } stopwatch.Stop(); Console.WriteLine("Time passed: " + stopwatch.ElapsedMilliseconds); Console.ReadKey(); Console.WriteLine(sw1.ElapsedMilliseconds + " " + sw2.ElapsedMilliseconds + " " + sw3.ElapsedMilliseconds + " " + sw5.ElapsedMilliseconds + " " + sw6.ElapsedMilliseconds + " " + sw7.ElapsedMilliseconds + " " + sw8.ElapsedMilliseconds + " " + swarr.ElapsedMilliseconds + " "); } } }