Exemple #1
0
    def _run_lifelines(model: CoxTimeVaryingFitter,
                       data: pd.DataFrame) -> pd.DataFrame:
        """Retrieve the win probability.

        Parameters
        ----------
        model : CoxTimeVaryingFitter
            The fitted model.
        data : pd.DataFrame
            The input dataset to be evaluated.

        Returns
        -------
        pd.DataFrame
            The updated dataset.
        """
        # Get the cumulative hazard -- copying from ``lifelines.fitters.SemiParametericPHFitter``
        vals = model.predict_partial_hazard(data[META["static"] +
                                                 META["dynamic"]])
        c0 = interpolate_at_times(model.baseline_cumulative_hazard_,
                                  data["stop"].values)
        # Survival is the negative exponent of the cumulative hazard
        new = data.copy()
        new[META["survival"]] = 1 - np.exp(-(c0 * vals.values))

        return new
Exemple #2
0
        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,
            }
Exemple #3
0
        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
predictData["Partial Hazard"] = ctv.predict_partial_hazard(predictData)
predictData["Log Partial Hazard"] = ctv.predict_log_partial_hazard(predictData)
## clean up the data frame and display the ordered relative risks
predictData = predictData.sort_values(["Partial Hazard"], ascending=[True])
print("Model predictions for season " + str(season) + ", episode " +
      str(episode))
print(predictData[[
    "FirstName", "LastName", "Partial Hazard", "Log Partial Hazard"
]])
"""
What if we account for how many people are left in the competition
"""
ctv_compleft = CoxTimeVaryingFitter()
ctv_compleft.fit(trainData[[
    "Age", "CompLeft", "QuickfireWins", "Wins", "Highs", "Lows", "Start",
    "End", "ID", "Out"