private static Experiment3Output Experiment3(int neuronCount, decimal minAnnPPV, decimal minAnnNPV, int treeCount, decimal minForestPPV, decimal minForestNPV) { var result = new Experiment3Output(); result.NeuronCount = neuronCount; var metsRecords = LoadDataRecords(TRAINING_DATA_FILE, 0); DateTime et = DateTime.Now; var currentMinPPV = 0m; var currentMinNPV = 0m; Data data = null; var learningData = new List<AnnLearningRecord>(); var annPV = new AlgoritamPredictiveValues(); var forestPV = new AlgoritamPredictiveValues(); while (currentMinPPV < minAnnPPV || currentMinNPV < minAnnNPV || currentMinPPV <= currentMinNPV || currentMinPPV < minForestPPV || currentMinNPV < minForestNPV) { var es = DateTime.Now; data = CreateData(metsRecords, 0.8, 0.1); result.Means = data.Means; result.StandardDeviations = data.StandardDeviations; Console.Out.WriteLine("================================"); Console.Out.WriteLine($" ANN PPV, NC:{neuronCount}"); Console.Out.WriteLine("================================"); learningData.Clear(); annPV = Ann(data, neuronCount, learningData, out var ann); currentMinPPV = annPV.TestPPV; currentMinNPV = annPV.TestNPV; result.Ann = ann; LogAPV(annPV); LogMessage($"Experiment time: {DateTime.Now - es}, total time {DateTime.Now - et}"); } var experimentPV = new ExperimentPredictiveValues() { AnnPpvLearnNPV = annPV.LearnNPV, AnnPpvLearnPPV = annPV.LearnPPV, AnnPpvValidateNPV = annPV.ValidateNPV, AnnPpvValidatePPV = annPV.ValidatePPV, AnnPpvTestNPV = annPV.TestNPV, AnnPpvTestPPV = annPV.TestPPV, }; using (var tw = File.CreateText($"d:\\mets_experiment_3_{neuronCount}_{DateTime.Now:yyyy_MM_dd_hh_mm}_rndfrst.csv")) { var csv = new CsvHelper.CsvWriter(tw); csv.WriteHeader<AnnLearningRecord>(); csv.NextRecord(); csv.WriteRecords(learningData); } using (var tw = File.CreateText($"d:\\mets_experiment_3_PV_{neuronCount}_{DateTime.Now:yyyy_MM_dd_hh_mm}_rndfrst.csv")) { var csv = new CsvHelper.CsvWriter(tw); csv.WriteHeader<ExperimentPredictiveValues>(); csv.NextRecord(); csv.WriteRecords(new ExperimentPredictiveValues[] { experimentPV }); } return result; }
private static void Experiment4(Experiment3Output exp3) { var metsRecords = LoadDataRecords(TEST_DATA_FILE, 0); for (int i = 0; i < metsRecords.Count; i++) { var mets = CalculateMets(metsRecords[i]); if (metsRecords[i].METS != mets) throw new Exception("Invalid data"); } if (metsRecords.Count > 100) { var mr = new List<MetsDataRecord>(); var rnd = new Random(); while (mr.Count < 100) { var index = rnd.Next(metsRecords.Count); mr.Add(metsRecords[index]); metsRecords.RemoveAt(index); } metsRecords = mr; } var inputs = CreateInputs(metsRecords).ToArray(); inputs = inputs.ZScores(exp3.Means, exp3.StandardDeviations); var outputs = CreateOutputs(metsRecords).ToArray(); var annPredictions = new double[inputs.Length][]; for (int i = 0; i < inputs.Length; i++) { var annr = exp3.Ann.Compute(inputs[i])[0]; var to = Math.Round(outputs[i][0], 0); var anno = Math.Round(annr, 0); annPredictions[i] = new double[] { anno }; } var annPV = CalculatePredictiveValues(outputs, annPredictions); var results = new List<Experiment4ResultRecord>(); for (int i = 0; i < metsRecords.Count; i++) { results.Add(new Experiment4ResultRecord() { AGE = metsRecords[i].AGE, BMI = metsRecords[i].BMI, DBP = metsRecords[i].DBP, FPG = metsRecords[i].FPG, GEN = metsRecords[i].GEN, HDL = metsRecords[i].HDL, HT = metsRecords[i].HT, METS = metsRecords[i].METS, METS_PR_ANN = (int)Math.Round(annPredictions[i][0]), SBP = metsRecords[i].SBP, TG = metsRecords[i].TG, WC = metsRecords[i].WC, WHtR = metsRecords[i].WHtR, WT = metsRecords[i].WT }); } var experimentPV = new Experiment4PredictiveValues() { AnnNPV = annPV.NPV, AnnPPV = annPV.PPV, }; using (var tw = File.CreateText($"d:\\mets_experiment_4_{exp3.NeuronCount}_{DateTime.Now:yyyy_MM_dd_hh_mm}.csv")) { var csv = new CsvHelper.CsvWriter(tw); csv.WriteHeader<Experiment4ResultRecord>(); csv.NextRecord(); csv.WriteRecords(results); } using (var tw = File.CreateText($"d:\\mets_experiment_4_PV_{exp3.NeuronCount}_{DateTime.Now:yyyy_MM_dd_hh_mm}.csv")) { var csv = new CsvHelper.CsvWriter(tw); csv.WriteHeader<Experiment4PredictiveValues>(); csv.NextRecord(); csv.WriteRecords(new Experiment4PredictiveValues[] { experimentPV }); } }