# 数据分析：使用PYTHON做生存分析（生存曲线、COX回归）

#### 一、绘制S(T)

``````from lifelines.datasets import load_waltons
from lifelines import KaplanMeierFitter
from lifelines.utils import median_survival_times

print(df['T'].min(), df['T'].max(),'n')
print(df['E'].value_counts(),'n')
print(df['group'].value_counts(),'n')
``````

``````kmf = KaplanMeierFitter()
kmf.fit(df['T'], event_observed=df['E'])

kmf.plot_survival_function()

median_ = kmf.median_survival_time_
median_confidence_interval_ = median_survival_times(kmf.confidence_interval_)
print(median_confidence_interval_)
``````

``````groups = df['group']
ix = (groups == 'miR-137')

kmf.fit(df['T'][ix], df['E'][ix], label='miR-137')
ax = kmf.plot()
treatment_median_confidence_interval_ = median_survival_times(kmf.confidence_interval_)
print("带有miR-137病毒存活50%对应的存活时间95%置信区间：'n'", treatment_median_confidence_interval_, 'n')

kmf.fit(df['T'][~ix], df['E'][~ix], label='control')
#共享一个画布
ax = kmf.plot(ax=ax)

control_median_confidence_interval_ = median_survival_times(kmf.confidence_interval_)
print("未带有miR-137病毒存活50%对应的存活时间95%置信区间：'n'", control_median_confidence_interval_)
``````

#### 二、COX回归

``````from lifelines.datasets import load_regression_dataset
from lifelines import CoxPHFitter

print(regression_dataset['E'].value_counts())
``````

``````cph = CoxPHFitter()
cph.fit(regression_dataset, 'T', event_col='E')
cph.print_summary()
``````