public double Classify(Patient patient, PatientVisitData visitData) { var enumerable = RuntimeClassificationModel .Properties .Select(p => p.Scaler[Convert.ToDouble(PatientPropertyProvider.GetPropertyValue(p.Name, patient, visitData))] * p.ModelCoefficient) .ToList(); var intermediateResult = enumerable.Sum() + RuntimeClassificationModel.FreeCoefficient; return(Math.Exp(intermediateResult) / (1 + Math.Exp(intermediateResult))); }
public double CalculateOptimalCutOff(ClassificationModel model, List <Patient> patients) { //ToDo: filter patients which could be used for current model var applicablePatients = patients.Where( patient => model.Properties.All(p => PatientPropertyProvider.GetPropertyValue( p.Name, patient, patient.PatientVisitDataHistory.OrderByDescending(d => d.VisitDate).First()) != null)) .ToList(); foreach (var p in applicablePatients) { var temp = p.PatientVisitDataHistory.OrderByDescending(d => d.VisitDate).First(); Console.Write(p.Surname + " bmi " + temp.ObesityBMI + " waist " + temp.ObesityWaistCircumference + " gene " + p.AGT_AGTR2 + " " + " gender " + p.Gender + " phiz " + temp.PhysicalActivity + " maleHered " + p.MaleHeredity + "; \n"); } var classificator = new PatientClassificator(model); var healthyCorrect = 0; var illCorrect = applicablePatients.Count(p => p.Stage != HypertensionStage.Healthy); var healthyIncorrect = applicablePatients.Count(p => p.Stage == HypertensionStage.Healthy); var illIncorrect = 0; var basePatientStageScoresGroupped = applicablePatients.Select(patient => new { Patient = patient, Score = classificator.Classify(patient, patient.PatientVisitDataHistory.OrderByDescending(d => d.VisitDate).First()) }) .GroupBy(patientScore => patientScore.Score) .OrderBy(scoreGroup => scoreGroup.Key) .ToList(); var cutoffs = new List <CutOffValues> { new CutOffValues { HealthyCorrect = healthyCorrect, HealthyIncorrect = healthyIncorrect, IllCorrect = illCorrect, IllIncorrect = illIncorrect, Score = 0 } }; foreach (var scoreGroup in basePatientStageScoresGroupped) { foreach (var patientScore in scoreGroup) { if (patientScore.Patient.Stage == HypertensionStage.Healthy) { healthyCorrect++; healthyIncorrect--; } else { illCorrect--; illIncorrect++; } } cutoffs.Add(new CutOffValues { HealthyCorrect = healthyCorrect, HealthyIncorrect = healthyIncorrect, IllCorrect = illCorrect, IllIncorrect = illIncorrect, Score = scoreGroup.Key } ); } var cutoff = cutoffs.OrderByDescending(c => c.SumPercent).ThenBy(c => c.PercentDifference); // foreach (var coutoffTest in cutoffs) // { // Console.Write(coutoffTest.SumPercent + " " + coutoffTest.HealthPercent + " " + coutoffTest.IllPercent + "; \n"); // } var cutoff2 = cutoffs.OrderBy(c => c.PercentDifference).ThenByDescending(c => c.SumPercent); return(cutoff.FirstOrDefault().Score); }