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)
Esempio n. 2
0
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
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 " +
# -*- 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()