예제 #1
0
        private static void coxProportionalHazards()
        {
            // Let's say we have the following survival problem. Each row in the table below
            // represents a patient under care in a hospital. The first colum represents their
            // age (a single feature, but there could have been many like age, height, weight,
            // etc), the time until an event has happened (like, for example, unfortunatey death)
            // and the event outcome (i.e. what has exactly happened after this amount of time,
            // has the patient died or did he simply leave the hospital and we couldn't get more
            // data about him?)

            object[,] data =
            {
                //    input         time until           outcome
                // (features)     event happened     (what happened?)
                { 50,  1, SurvivalOutcome.Censored },
                { 70,  2, SurvivalOutcome.Failed   },
                { 45,  3, SurvivalOutcome.Censored },
                { 35,  5, SurvivalOutcome.Censored },
                { 62,  7, SurvivalOutcome.Failed   },
                { 50, 11, SurvivalOutcome.Censored },
                { 45,  4, SurvivalOutcome.Censored },
                { 57,  6, SurvivalOutcome.Censored },
                { 32,  8, SurvivalOutcome.Censored },
                { 57,  9, SurvivalOutcome.Failed   },
                { 60, 10, SurvivalOutcome.Failed   },
            }; // Note: Censored means that we stopped recording data for that person,
               // so we do not know what actually happened to them, except that things
               // were going fine until the point in time appointed by "time to event"

            // Parse the data above
            double[][]        inputs = data.GetColumn(0).ToDouble().ToJagged();
            double[]          time   = data.GetColumn(1).ToDouble();
            SurvivalOutcome[] output = data.GetColumn(2).To <SurvivalOutcome[]>();

            // Create a new PH Newton-Raphson learning algorithm
            var teacher = new ProportionalHazardsNewtonRaphson()
            {
                ComputeBaselineFunction = true,
                ComputeStandardErrors   = true,
                MaxIterations           = 100
            };

            // Use the learning algorithm to infer a Proportional Hazards model
            ProportionalHazards regression = teacher.Learn(inputs, time, output);

            // Use the regression to make predictions (problematic)
            SurvivalOutcome[] prediction = regression.Decide(inputs);

            // Use the regression to make score estimates
            double[] score = regression.Score(inputs);

            // Use the regression to make probability estimates
            double[] probability = regression.Probability(inputs);
        }
예제 #2
0
        public void doc_learn()
        {
            // Data from: http://www.sph.emory.edu/~cdckms/CoxPH/prophaz2.html / http://statpages.info/prophaz2.html

            #region doc_learn
            // Let's say we have the following survival problem. Each row in the
            // table below represents a patient under care in a hospital. The first
            // colum represents their age (a single feature, but there could have
            // been many like age, height, weight, etc), the time until an event
            // has happened (like, for example, unfortunatey death) and the event
            // outcome (i.e. what has exactly happened after this amount of time,
            // has the patient died or did he simply leave the hospital and we
            // couldn't get more data about him?)

            object[,] data =
            {
                //    input         time until           outcome
                // (features)     event happened     (what happened?)
                { 50,  1, SurvivalOutcome.Censored },
                { 70,  2, SurvivalOutcome.Failed   },
                { 45,  3, SurvivalOutcome.Censored },
                { 35,  5, SurvivalOutcome.Censored },
                { 62,  7, SurvivalOutcome.Failed   },
                { 50, 11, SurvivalOutcome.Censored },
                { 45,  4, SurvivalOutcome.Censored },
                { 57,  6, SurvivalOutcome.Censored },
                { 32,  8, SurvivalOutcome.Censored },
                { 57,  9, SurvivalOutcome.Failed   },
                { 60, 10, SurvivalOutcome.Failed   },
            }; // Note: Censored means that we stopped recording data for that person,
               // so we do not know what actually happened to them, except that things
               // were going fine until the point in time appointed by "time to event"

            // Parse the data above
            double[][]        inputs = data.GetColumn(0).ToDouble().ToJagged();
            double[]          time   = data.GetColumn(1).ToDouble();
            SurvivalOutcome[] output = data.GetColumn(2).To <SurvivalOutcome[]>();

            // Create a new PH Newton-Raphson learning algorithm
            var teacher = new ProportionalHazardsNewtonRaphson()
            {
                ComputeBaselineFunction = true,
                ComputeStandardErrors   = true,
                MaxIterations           = 100
            };

            // Use the learning algorithm to infer a Proportional Hazards model
            ProportionalHazards regression = teacher.Learn(inputs, time, output);

            // Use the regression to make predictions (problematic)
            SurvivalOutcome[] prediction = regression.Decide(inputs);

            // Use the regression to make score estimates
            double[] score = regression.Score(inputs);

            // Use the regression to make probability estimates
            double[] probability = regression.Probability(inputs);
            #endregion

            string   str      = probability.ToCSharp();
            double[] expected = { 0.640442743460877, 1206.22665747906, 0.0972172106179122, 0.00224010744584941, 59.0812230260151, 0.640442743460877, 0.0972172106179122, 8.9683453534747, 0.000722814003252998, 8.9683453534747, 27.7942279934438 };
            Assert.IsTrue(expected.IsEqual(probability, rtol: 1e-8));
        }