【问题标题】:Xgboost cox survival time entryXgboost cox 生存时间入口
【发布时间】:2020-07-05 08:59:21
【问题描述】:

在 xgboost 0.81 中 cox ph 生存模型的新实现中,如何指定事件的开始和结束时间?

谢谢

R 等效函数例如:

cph_mod = coxph(Surv(Start, Stop, Status) ~ Age + Sex + SBP, data=data)

【问题讨论】:

    标签: xgboost survival cox


    【解决方案1】:

    XGBoost 不允许启动(即延迟进入)。如果对应用程序有意义,您可以随时更改基础时间刻度,以便所有主题从 time=0 开始。但是,XGBoost 确实允许右删失数据。似乎不可能找到任何关于如何实现 Cox 模型的文档/示例,但您可以从源代码中阅读"Cox regression for censored survival data (negative labels are considered censored)."

    下面是一个简短的示例,供任何想要使用 obj="survival:cox" 尝试 XGBoost 的人使用。我们可以将结果与 scikit-learn 生存包 sksurv 进行比较。为了使 XGBoost 更类似于该框架,我们使用线性助推器而不是树助推器。

    import pandas as pd
    import xgboost as xgb
    from sksurv.datasets import load_aids
    from sksurv.linear_model import CoxPHSurvivalAnalysis
    
    # load and inspect the data
    data_x, data_y = load_aids()
    data_y[10:15]
    Out[586]: 
    array([(False, 334.), (False, 285.), (False, 265.), ( True, 206.),
       (False, 305.)], dtype=[('censor', '?'), ('time', '<f8')])
    
    # Since XGBoost only allow one column for y, the censoring information
    # is coded as negative values:
    data_y_xgb = [x[1] if x[0] else -x[1] for x in data_y]
    data_y_xgb[10:15]
    Out[3]: [-334.0, -285.0, -265.0, 206.0, -305.0]
    
    data_x = data_x[['age', 'cd4']]
    data_x.head()
    Out[4]: 
        age    cd4
    0  34.0  169.0
    1  34.0  149.5
    2  20.0   23.5
    3  48.0   46.0
    4  46.0   10.0
    
    # Since sksurv output log hazard ratios (here relative to 0 on predictors)
    # we must use 'output_margin=True' for comparability.
    estimator = CoxPHSurvivalAnalysis().fit(data_x, data_y)
    gbm = xgb.XGBRegressor(objective='survival:cox',
                           booster='gblinear',
                           base_score=1,
                           n_estimators=1000).fit(data_x, data_y_xgb)
    prediction_sksurv = estimator.predict(data_x)
    predictions_xgb = gbm.predict(data_x, output_margin=True)
    d = pd.DataFrame({'xgb': predictions_xgb,
                      'sksurv': prediction_sksurv})
    d.head()
    Out[13]: 
         sksurv       xgb
    0 -1.892490 -1.843828
    1 -1.569389 -1.524385
    2  0.144572  0.207866
    3  0.519293  0.502953
    4  1.062392  1.045287
    
    d.plot.scatter('xgb', 'sksurv')
    

    请注意,这些是对用于拟合模型的相同数据的预测。 XGBoost 似乎获得了正确的值,但有时使用线性变换。我不知道为什么。玩弄 base_scoren_estimators。也许有人可以添加到这个答案。

    【讨论】:

    • 感谢您的回答。或者,我可以在 Dmatrix 中使用分组,但我认为目前还没有为 cox ph 处理。
    猜你喜欢
    • 2020-05-17
    • 1970-01-01
    • 2016-07-30
    • 1970-01-01
    • 1970-01-01
    • 2018-02-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多