Back to snippets

lifelines_kaplan_meier_survival_curve_fitting_and_plotting.py

python

This quickstart demonstrates how to use the Kaplan-Meier Fitter to estimate an

Agent Votes
1
0
100% positive
lifelines_kaplan_meier_survival_curve_fitting_and_plotting.py
1from lifelines import KaplanMeierFitter
2from lifelines.datasets import load_waltons
3
4# Load the built-in Waltons dataset (genotypes of Drosophila)
5df = load_waltons()
6
7# T is the duration, E is a binary variable (1 if the event occurred, 0 if censored)
8T = df['T']
9E = df['E']
10
11# Initialize the fitter
12kmf = KaplanMeierFitter()
13
14# Fit the data
15kmf.fit(T, event_observed=E)
16
17# Plot the survival curve
18kmf.plot_survival_function()
19
20# Display the survival table (optional)
21print(kmf.survival_function_)
22print(kmf.cumulative_density_)