本文整理汇总了Python中lifelines.KaplanMeierFitter方法的典型用法代码示例。如果您正苦于以下问题:Python lifelines.KaplanMeierFitter方法的具体用法?Python lifelines.KaplanMeierFitter怎么用?Python lifelines.KaplanMeierFitter使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类lifelines
的用法示例。
在下文中一共展示了lifelines.KaplanMeierFitter方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: calib_plot
# 需要导入模块: import lifelines [as 别名]
# 或者: from lifelines import KaplanMeierFitter [as 别名]
def calib_plot(fu_time, n_bins, pred_surv, time, dead, color, label, error_bars=0,alpha=1., markersize=1., markertype='o'):
cuts = np.concatenate((np.array([-1e6]),np.percentile(pred_surv, np.arange(100/n_bins,100,100/n_bins)),np.array([1e6])))
bin = pd.cut(pred_surv,cuts,labels=False)
kmf = KaplanMeierFitter()
est = []
ci_upper = []
ci_lower = []
mean_pred_surv = []
for which_bin in range(max(bin)+1):
kmf.fit(time[bin==which_bin], event_observed=dead[bin==which_bin])
est.append(np.interp(fu_time, kmf.survival_function_.index.values, kmf.survival_function_.KM_estimate))
ci_upper.append(np.interp(fu_time, kmf.survival_function_.index.values, kmf.confidence_interval_.loc[:,'KM_estimate_upper_0.95']))
ci_lower.append(np.interp(fu_time, kmf.survival_function_.index.values, kmf.confidence_interval_.loc[:,'KM_estimate_lower_0.95']))
mean_pred_surv.append(np.mean(pred_surv[bin==which_bin]))
est = np.array(est)
ci_upper = np.array(ci_upper)
ci_lower = np.array(ci_lower)
if error_bars:
plt.errorbar(mean_pred_surv, est, yerr = np.transpose(np.column_stack((est-ci_lower,ci_upper-est))), fmt='o',c=color,label=label)
else:
plt.plot(mean_pred_surv, est, markertype, c=color,label=label, alpha=alpha, markersize=markersize)
return (mean_pred_surv, est)
示例2: plot_km_survf
# 需要导入模块: import lifelines [as 别名]
# 或者: from lifelines import KaplanMeierFitter [as 别名]
def plot_km_survf(data, t_col="t", e_col="e"):
"""
Plot KM survival function curves.
Parameters
----------
data: pandas.DataFrame
Survival data to plot.
t_col: str
Column name in data indicating time.
e_col: str
Column name in data indicating events or status.
"""
from lifelines import KaplanMeierFitter
from lifelines.plotting import add_at_risk_counts
fig, ax = plt.subplots(figsize=(6, 4))
kmfh = KaplanMeierFitter()
kmfh.fit(data[t_col], event_observed=data[e_col], label="KM Survival Curve")
kmfh.survival_function_.plot(ax=ax)
plt.ylim(0, 1.01)
plt.xlabel("Time")
plt.ylabel("Probalities")
plt.legend(loc="best")
add_at_risk_counts(kmfh, ax=ax)
plt.show()
示例3: fit_plot
# 需要导入模块: import lifelines [as 别名]
# 或者: from lifelines import KaplanMeierFitter [as 别名]
def fit_plot(T1, T2, E1, E2, title, unit_of_time, label1, label2):
kmf1 = KaplanMeierFitter()
kmf2 = KaplanMeierFitter()
ax = kmf1.fit(T1, E1, label=label1, alpha=0.05).plot(show_censors=True)
ax = kmf2.fit(T2, E2, label=label2, alpha=0.05).plot(ax=ax, show_censors=True)
ax.set_title(title)
if unit_of_time:
plt.xlabel(f'timeline ({unit_of_time})')
lifelines.plotting.add_at_risk_counts(kmf1, kmf2, ax=ax, labels=None)
figname = ax.figure.canvas.get_window_title()
ax.figure.canvas.set_window_title(f'Party {mpc.pid} - {figname}')
return kmf1, kmf2
示例4: calibration_time_to_event
# 需要导入模块: import lifelines [as 别名]
# 或者: from lifelines import KaplanMeierFitter [as 别名]
def calibration_time_to_event(Forecast, T, E, bins=10, eps=1e-3):
"""
Calculate calibration in the time-to-event setting, with integral transform and KM.
"""
cdfs = Forecast.cdf(T)
kmf = KaplanMeierFitter()
kmf.fit(cdfs, E)
idxs = np.round(np.linspace(0, len(kmf.survival_function_) - 1, 11))
preds = np.array(kmf.survival_function_.iloc[idxs].index)
obs = 1 - np.array(kmf.survival_function_.iloc[idxs].KM_estimate)
slope, intercept = np.polyfit(preds, obs, deg=1)
return preds, obs, slope, intercept
示例5: test_kaplan_meier_vs_lifelines
# 需要导入模块: import lifelines [as 别名]
# 或者: from lifelines import KaplanMeierFitter [as 别名]
def test_kaplan_meier_vs_lifelines(n, p_cens):
from lifelines import KaplanMeierFitter
np.random.seed(0)
durations = np.random.uniform(0, 100, n)
events = np.random.binomial(1, 1 - p_cens, n).astype('float')
km = utils.kaplan_meier(durations, events)
kmf = KaplanMeierFitter().fit(durations, events).survival_function_['KM_estimate']
assert km.shape == kmf.shape
assert (km - kmf).abs().max() < 1e-14
assert (km.index == kmf.index).all()