从cox.print_summary()提取数据摘要

时间:2019-05-09 06:42:21

标签: python cox-regression

我要输出print_summary的前6行。我该怎么办?

我有cox.print_summary()的全部摘要。 cox.summary()为列详细信息提供了一种数据框架格式,但为摘要编制索引并没有提供数据集检查器摘要

cph = CoxPHFitter()
cph.fit(self.data_train, duration_col='time', event_col='dead')
cph.print_summary()
'''<lifelines.CoxPHFitter: fitted with 6373 observations, 1974 censored>
      duration col = 'time'
         event col = 'dead'
number of subjects = 6373
  number of events = 4399
    log-likelihood = -34779.52
  time fit was run = 2019-05-09 06:28:06 UTC

---
                    coef  exp(coef)  se(coef)     z      p  -log2(p)  lower 0.95  upper 0.95
dzgroupCHF          0.49       1.64      0.06  8.19 <0.005     51.79        0.37        0.61
dzgroupCirrhosis    0.55       1.73      0.08  6.71 <0.005     35.63        0.39        0.71

以此类推

results = self.cph.summary
print(results.head())

这将以df格式提供变量详细信息。但是我想要:

'''<lifelines.CoxPHFitter: fitted with 6373 observations, 1974 censored>
      duration col = 'time'
         event col = 'dead'
number of subjects = 6373
  number of events = 4399
    log-likelihood = -34779.52
  time fit was run = 2019-05-09 06:28:06 UTC

索引产生错误:

  

cph.print_summary()[0:9]

     

TypeError:“ NoneType”对象不可下标

1 个答案:

答案 0 :(得分:0)

其中大多数是模型上的属性,可以直接访问。查看代码,print_summary如下所示:

        print(self)
        print("{} = '{}'".format(justify("duration col"), self.duration_col))

        if self.event_col:
            print("{} = '{}'".format(justify("event col"), self.event_col))
        if self.weights_col:
            print("{} = '{}'".format(justify("weights col"), self.weights_col))

        if self.cluster_col:
            print("{} = '{}'".format(justify("cluster col"), self.cluster_col))

        if self.robust or self.cluster_col:
            print("{} = {}".format(justify("robust variance"), True))

        if self.strata:
            print("{} = {}".format(justify("strata"), self.strata))

        if self.penalizer > 0:
            print("{} = {}".format(justify("penalizer"), self.penalizer))

        print("{} = {}".format(justify("number of subjects"), self._n_examples))
        print("{} = {}".format(justify("number of events"), self.event_observed.sum()))
        print("{} = {:.{prec}f}".format(justify("partial log-likelihood"), self._log_likelihood, prec=decimals))
        print("{} = {}".format(justify("time fit was run"), self._time_fit_was_called))

因此,可以使用self._log_likelihoodself._n_examples等访问所需的值。

将来有一些工作可能会简化提取数据的过程:https://github.com/CamDavidsonPilon/lifelines/issues/721#issuecomment-497180538