示例#1
0
 def test_coxtv_plotting_with_subset_of_columns(self, block):
     df = load_stanford_heart_transplants()
     ctv = CoxTimeVaryingFitter()
     ctv.fit(df, id_col="id", event_col="event")
     ctv.plot(columns=["age", "year"])
     self.plt.title("test_coxtv_plotting_with_subset_of_columns")
     self.plt.show(block=block)
示例#2
0
 def test_coxtv_plotting(self, block):
     df = load_stanford_heart_transplants()
     ctv = CoxTimeVaryingFitter()
     ctv.fit(df, id_col="id", event_col="event")
     ctv.plot(fmt="o")
     self.plt.title("test_coxtv_plotting")
     self.plt.show(block=block)
示例#3
0
文件: tuning.py 项目: ak-gupta/nbaspa
        def func(params):
            model = CoxTimeVaryingFitter(**params, **kwargs)
            model.fit(
                train_data[[META["id"], META["event"]] + ["start", "stop"] +
                           META["static"] + META["dynamic"]],
                id_col=META["id"],
                event_col=META["event"],
                start_col="start",
                stop_col="stop",
            )
            metric: List[float] = []
            for dataset in tune_data:
                predt = model.predict_partial_hazard(dataset)
                c0 = interpolate_at_times(model.baseline_cumulative_hazard_,
                                          dataset["stop"].values)
                metric.append(
                    roc_auc_score(
                        y_true=dataset[META["event"]],
                        y_score=1 - np.exp(-(c0 * predt.values)),
                    ))
            metric = -np.average(np.array(metric))

            if metric < self.metric_:
                self.metric_ = metric
                self.best_.update(params)
                self.logger.info(
                    f"New best metric value of {self.metric_} with \n\n{pformat(self.best_)}\n"
                )

            return {
                "loss": metric,
                "status": STATUS_OK,
            }
def unit_timevarying_cox(df, features=[]):
    factors = ['user_id', 'start', 'stop', 'event'] + features
    df_ = df[factors]

    ctv = CoxTimeVaryingFitter()

    ctv.fit(df_, id_col='user_id', event_col='event', start_col='start', stop_col='stop', show_progress=True)
    ctv.print_summary(3)
示例#5
0
    def run(  # type: ignore
            self, model: CoxTimeVaryingFitter, data: pd.DataFrame,
            **kwargs) -> CoxTimeVaryingFitter:
        """Fit the lifelines model.

        Parameters
        ----------
        model : CoxTimeVaryingFitter
            The initialized model.
        data : pd.DataFrame
            The ``lifelines`` format data.
        **kwargs
            Keyword arguments for the ``fit`` method.

        Returns
        -------
        CoxTimeVaryingFitter
            The fitted model.
        """
        model = model.fit(
            data[[META["id"], META["event"]] + ["start", "stop"] +
                 META["static"] + META["dynamic"]],
            id_col=META["id"],
            event_col=META["event"],
            start_col="start",
            stop_col="stop",
            **kwargs)

        return model
示例#6
0
        label="> 3")
kmf.plot(ax=ax)

plt.title("Survival separated by number of wins")
plt.ylim(0, 1)
plt.show()
"""
Cox Propotional Model using Age, Quickfire Wins, Wins, Highs and Lows to predict Out
"""
from lifelines import CoxTimeVaryingFitter
ctv = CoxTimeVaryingFitter()
ctv.fit(trainData[[
    "Age", "QuickfireWins", "Wins", "Highs", "Lows", "Start", "End", "ID",
    "Out"
]],
        id_col="ID",
        event_col="Out",
        start_col="Start",
        stop_col="End",
        show_progress=True)
ctv.print_summary()

#Get Hazards for specific episode
####EDIT THIS TO CHANGE WHICH SEASON/EPISODE YOU WANT TO APPLY THE MODEL TO#####
episode = 5
season = 13

predictData = allSeasonsByEpisode.loc[
    (allSeasonsByEpisode["Episode"] == episode)
    & (allSeasonsByEpisode["Season"] == season)]
## generate the relative risk with the model and merge it with the existing data
# -*- coding: utf-8 -*-
if __name__ == "__main__":
    import time
    import pandas as pd
    from lifelines import CoxTimeVaryingFitter
    from lifelines.datasets import load_rossi
    from lifelines.utils import to_long_format

    df = load_rossi()
    df = pd.concat([df] * 20)
    df = df.reset_index()
    df = to_long_format(df, duration_col="week")
    ctv = CoxTimeVaryingFitter()
    start_time = time.time()
    ctv.fit(df,
            id_col="index",
            event_col="arrest",
            start_col="start",
            stop_col="stop")
    time_took = time.time() - start_time
    print("--- %s seconds ---" % time_took)
    ctv.print_summary()
示例#8
0
# -*- coding: utf-8 -*-
if __name__ == "__main__":
    import time
    import pandas as pd
    from lifelines import CoxTimeVaryingFitter
    from lifelines.datasets import load_rossi
    from lifelines.utils import to_long_format

    df = load_rossi()
    df = pd.concat([df] * 20)
    df = df.reset_index()
    df = to_long_format(df, duration_col="week")
    ctv = CoxTimeVaryingFitter()
    start_time = time.time()
    ctv.fit(df,
            id_col="index",
            event_col="arrest",
            start_col="start",
            stop_col="stop",
            strata=["wexp", "prio"])
    time_took = time.time() - start_time
    print("--- %s seconds ---" % time_took)
    ctv.print_summary()