public double Classify(object dataSource) { var enumerable = ClassificationModel .Properties .Select(p => p.Scaler[Convert.ToDouble(PatientPropertyProvider.GetPropertyValue(dataSource, p.Name))] * p.Coefficient) .ToList(); var intermediateResult = enumerable.Sum() + ClassificationModel.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 => { var dataSource = new { Patient = patient, PatientVisit = patient.LastVisit }; return(model.Properties.All(p => PatientPropertyProvider.GetPropertyValue(dataSource, p.Name) != null)); }) .ToList(); foreach (var p in applicablePatients) { var lastVisitData = p.LastVisit; Console.Write($@"{p.Surname} bmi {lastVisitData.ObesityBmi} waist {lastVisitData.ObesityWaistCircumference} gene {p.AgtAgtr2} gender {p.Gender} phiz {lastVisitData.PhysicalActivity} maleHered {p.MaleHeredity}; \n"); } var classificator = new PatientClassificator(model); var healthyCorrect = 0; var illCorrect = applicablePatients.Count(p => p.HypertensionStage != HypertensionStage.Healthy); var healthyIncorrect = applicablePatients.Count(p => p.HypertensionStage == HypertensionStage.Healthy); var illIncorrect = 0; var basePatientStageScoresGroupped = applicablePatients.Select(patient => new { Patient = patient, Score = classificator.Classify(new { Patient = patient, PatientVisit = patient.LastVisit }) }) .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.HypertensionStage == 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); }