本文整理汇总了Python中lifelines.estimation.KaplanMeierFitter.plot方法的典型用法代码示例。如果您正苦于以下问题:Python KaplanMeierFitter.plot方法的具体用法?Python KaplanMeierFitter.plot怎么用?Python KaplanMeierFitter.plot使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类lifelines.estimation.KaplanMeierFitter
的用法示例。
在下文中一共展示了KaplanMeierFitter.plot方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_kmf_with_risk_counts
# 需要导入模块: from lifelines.estimation import KaplanMeierFitter [as 别名]
# 或者: from lifelines.estimation.KaplanMeierFitter import plot [as 别名]
def test_kmf_with_risk_counts(self, block):
data1 = np.random.exponential(10, size=(100))
kmf = KaplanMeierFitter()
kmf.fit(data1)
kmf.plot(at_risk_counts=True)
self.plt.title("test_kmf_with_risk_counts")
self.plt.show(block=block)
示例2: test_kmf_minimum_observation_bias
# 需要导入模块: from lifelines.estimation import KaplanMeierFitter [as 别名]
# 或者: from lifelines.estimation.KaplanMeierFitter import plot [as 别名]
def test_kmf_minimum_observation_bias():
N = 250
kmf = KaplanMeierFitter()
T, C = exponential_survival_data(N, 0.1, scale=10)
B = 0.01 * T
kmf.fit(T, C, entry=B)
kmf.plot()
plt.title("Should have larger variances in the tails")
示例3: test_flat_style_and_marker
# 需要导入模块: from lifelines.estimation import KaplanMeierFitter [as 别名]
# 或者: from lifelines.estimation.KaplanMeierFitter import plot [as 别名]
def test_flat_style_and_marker(self, block):
data1 = np.random.exponential(10, size=200)
data2 = np.random.exponential(2, size=200)
C1 = np.random.binomial(1, 0.9, size=200)
C2 = np.random.binomial(1, 0.95, size=200)
kmf = KaplanMeierFitter()
kmf.fit(data1, C1, label='test label 1')
ax = kmf.plot(flat=True, censor_styles={'marker': '+', 'mew': 2, 'ms': 7})
kmf.fit(data2, C2, label='test label 2')
kmf.plot(ax=ax, censor_styles={'marker': 'o', 'ms': 7}, flat=True)
self.plt.title("testing kmf flat styling + marker")
self.plt.show(block=block)
return
示例4: test_kmf_left_censorship_plots
# 需要导入模块: from lifelines.estimation import KaplanMeierFitter [as 别名]
# 或者: from lifelines.estimation.KaplanMeierFitter import plot [as 别名]
def test_kmf_left_censorship_plots(self, block):
kmf = KaplanMeierFitter()
lcd_dataset = load_lcd()
alluvial_fan = lcd_dataset.loc[lcd_dataset['group'] == 'alluvial_fan']
basin_trough = lcd_dataset.loc[lcd_dataset['group'] == 'basin_trough']
kmf.fit(alluvial_fan['T'], alluvial_fan['C'], left_censorship=True, label='alluvial_fan')
ax = kmf.plot()
kmf.fit(basin_trough['T'], basin_trough['C'], left_censorship=True, label='basin_trough')
ax = kmf.plot(ax=ax)
self.plt.title("test_kmf_left_censorship_plots")
self.plt.show(block=block)
return
示例5: test_kmf_plotting
# 需要导入模块: from lifelines.estimation import KaplanMeierFitter [as 别名]
# 或者: from lifelines.estimation.KaplanMeierFitter import plot [as 别名]
def test_kmf_plotting(self, block):
data1 = np.random.exponential(10, size=(100))
data2 = np.random.exponential(2, size=(200, 1))
data3 = np.random.exponential(4, size=(500, 1))
kmf = KaplanMeierFitter()
kmf.fit(data1, label='test label 1')
ax = kmf.plot()
kmf.fit(data2, label='test label 2')
kmf.plot(ax=ax)
kmf.fit(data3, label='test label 3')
kmf.plot(ax=ax)
self.plt.title("test_kmf_plotting")
self.plt.show(block=block)
return
示例6: test_kmf_left_censorship_plots
# 需要导入模块: from lifelines.estimation import KaplanMeierFitter [as 别名]
# 或者: from lifelines.estimation.KaplanMeierFitter import plot [as 别名]
def test_kmf_left_censorship_plots(self):
matplotlib = pytest.importorskip("matplotlib")
from matplotlib import pyplot as plt
kmf = KaplanMeierFitter()
lcd_dataset = load_lcd()
alluvial_fan = lcd_dataset.ix[lcd_dataset['group'] == 'alluvial_fan']
basin_trough = lcd_dataset.ix[lcd_dataset['group'] == 'basin_trough']
kmf.fit(alluvial_fan['T'], alluvial_fan['C'], left_censorship=True, label='alluvial_fan')
ax = kmf.plot()
kmf.fit(basin_trough['T'], basin_trough['C'], left_censorship=True, label='basin_trough')
ax = kmf.plot(ax=ax)
plt.show()
return
示例7: test_seaborn_doesnt_cause_kmf_plot_error
# 需要导入模块: from lifelines.estimation import KaplanMeierFitter [as 别名]
# 或者: from lifelines.estimation.KaplanMeierFitter import plot [as 别名]
def test_seaborn_doesnt_cause_kmf_plot_error(self, block, kmf, capsys):
import seaborn as sns
df = load_waltons()
T = df['T']
E = df['E']
kmf = KaplanMeierFitter()
kmf.fit(T, event_observed=E)
kmf.plot()
self.plt.title('test_seaborn_doesnt_cause_kmf_plot_error')
self.plt.show(block=block)
_, err = capsys.readouterr()
assert err == ""
示例8: test_flat_style_no_censor
# 需要导入模块: from lifelines.estimation import KaplanMeierFitter [as 别名]
# 或者: from lifelines.estimation.KaplanMeierFitter import plot [as 别名]
def test_flat_style_no_censor(self, block):
data1 = np.random.exponential(10, size=200)
kmf = KaplanMeierFitter()
kmf.fit(data1, label='test label 1')
ax = kmf.plot(flat=True, censor_styles={'marker': '+', 'mew': 2, 'ms': 7})
self.plt.title('test_flat_style_no_censor')
self.plt.show(block=block)
return
示例9: test_negative_times_still_plots
# 需要导入模块: from lifelines.estimation import KaplanMeierFitter [as 别名]
# 或者: from lifelines.estimation.KaplanMeierFitter import plot [as 别名]
def test_negative_times_still_plots(self, block):
n = 40
T = np.linspace(-2, 3, n)
C = np.random.randint(2, size=n)
kmf = KaplanMeierFitter()
kmf.fit(T, C)
ax = kmf.plot()
self.plt.title('test_negative_times_still_plots')
self.plt.show(block=block)
return
示例10: plot_KM
# 需要导入模块: from lifelines.estimation import KaplanMeierFitter [as 别名]
# 或者: from lifelines.estimation.KaplanMeierFitter import plot [as 别名]
def plot_KM(stime, censor, g1, pval, figname):
sns.set_style('white')
kmf = KaplanMeierFitter()
f, ax = plt.subplots(figsize=(3, 3))
np.set_printoptions(precision=2, suppress=False)
kmf.fit(stime[g1], event_observed=censor[g1], label=["high-risk group"])
kmf.plot(ax=ax, ci_show=False, show_censors=True)
kmf.fit(stime[~g1], event_observed=censor[~g1], label=["low-risk group"])
kmf.plot(ax=ax, ci_show=False, show_censors=True)
ax.grid(b=False)
sns.despine()
plt.ylim(0,1)
plt.xlabel("time", fontsize=14)
plt.ylabel("survival", fontsize=14)
plt.text(0.7, 0.85, 'pval = %.2e' % (pval), fontdict={'size': 12},
horizontalalignment='center', verticalalignment='center',
transform=ax.transAxes)
plt.xticks(rotation=45)
for item in (ax.get_xticklabels() + ax.get_yticklabels()):
item.set_fontsize(10)
plt.tight_layout()
plt.savefig(figname, format='eps')
plt.close()
示例11: plot_kaplan_function
# 需要导入模块: from lifelines.estimation import KaplanMeierFitter [as 别名]
# 或者: from lifelines.estimation.KaplanMeierFitter import plot [as 别名]
def plot_kaplan_function(duration_key):
from lifelines.estimation import KaplanMeierFitter
from lifelines.statistics import logrank_test
import matplotlib.pyplot as plt
duration_keys = ["days_from_priced_to_listing",
"days_to_final_price_revision",
# "days_to_first_price_update",
"days_from_s1_to_listing",
"days_to_first_price_change"]
duration_key = duration_keys[-1]
kmf = KaplanMeierFitter()
f, ax = plt.subplots(1,1,figsize=(12, 4), sharex=True)
T = 1 # annotation line thickness
xoffset = 0.4 # annotation offset (x-axis)
yoffset = 0.04
# Above filing price range
kmf.fit(above[duration_key], label='Upward Price Amendment: N={}'.format(len(above)), alpha=0.9)
kmf.plot(ax=ax, c=colors[5], alpha=0.7)
quartiles = [int(np.percentile(kmf.durations, x)) for x in [25, 50, 75]][::-1]
aprops = dict(facecolor=colors[5], width=T, headwidth=T)
plt.annotate("75%: {} days".format(quartiles[0]),
(quartiles[0], 0.25),
xytext=(quartiles[0]+xoffset, 0.25+yoffset),
arrowprops=aprops)
plt.annotate("50%: {} days".format(quartiles[1]),
(quartiles[1], 0.50),
xytext=(quartiles[1]+xoffset, 0.50+yoffset),
arrowprops=aprops)
plt.annotate("25%: {} days".format(quartiles[2]),
(quartiles[2], 0.75),
xytext=(quartiles[2]+xoffset, 0.75+yoffset),
arrowprops=aprops)
# Under filing price range
kmf.fit(under[duration_key], label='Downward Price Amendment: N={}'.format(len(under)),)
kmf.plot(ax=ax, c=colors[2], alpha=0.7)
quartiles = [int(np.percentile(kmf.durations, x)) for x in [25, 50, 75]][::-1]
aprops = dict(facecolor=colors[2], width=T, headwidth=T)
plt.annotate("75%: {} days".format(quartiles[0]),
(quartiles[0], 0.25),
xytext=(quartiles[0]+xoffset, 0.25+yoffset+0.05),
arrowprops=aprops)
plt.annotate("50%: {} days".format(quartiles[1]),
(quartiles[1], 0.50),
xytext=(quartiles[1]+xoffset, 0.50+yoffset+0.05),
arrowprops=aprops)
plt.annotate("25%: {} days".format(quartiles[2]),
(quartiles[2], 0.75),
xytext=(quartiles[2]+xoffset, 0.75+yoffset+0.05),
arrowprops=aprops)
# log rank tests + general graph labels
# summary, p_value, results = logrank_test(
# above[duration_key],
# within[duration_key],
# under[duration_key],
# alpha=0.95)
# ax.annotate("Log-rank test: (prob={p:.3f})".format(p=p_value),
# xy=(1210, 0.08))
plt.ylim(0,1)
plt.xlim(0, max(np.percentile(above[duration_key], 90), np.percentile(under[duration_key],90)))
plt.title("Kaplan-Meier Survival Functions")
plt.xlabel("Delay (days) in {}".format(duration_key))
plt.ylabel(r"$S(t)=Pr(T>t)$")
示例12:
# 需要导入模块: from lifelines.estimation import KaplanMeierFitter [as 别名]
# 或者: from lifelines.estimation.KaplanMeierFitter import plot [as 别名]
# The method takes the same parameters as it's R counterpart, a time vector and a vector indicating which observations are observed or censored. The model fitting sequence is similar to the [scikit-learn](http://scikit-learn.org/stable/) api.
# In[16]:
f = tongue.type==1
T = tongue[f]['time']
C = tongue[f]['delta']
kmf.fit(T, event_observed=C)
# To get a plot with the confidence intervals, we simply can call `plot()` on our `kmf` object.
# In[17]:
kmf.plot(title='Tumor DNA Profile 1')
# Now we can convert this plot to an interactive [Plotly](https://plot.ly) object. However, we will have to augment the legend and filled area manually. Once we create a helper function, the process is simple.
#
# Please see the Plotly Python [user guide](https://plot.ly/python/overview/#in-%5B37%5D) for more insight on how to update plot parameters.
#
# > Don't forget you can also easily edit the chart properties using the Plotly GUI interface by clicking the "Play with this data!" link below the chart.
# In[19]:
p = kmf.plot(ci_force_lines=True, title='Tumor DNA Profile 1 (95% CI)')
# Collect the plot object
kmf1 = plt.gcf()
示例13: hazard
# 需要导入模块: from lifelines.estimation import KaplanMeierFitter [as 别名]
# 或者: from lifelines.estimation.KaplanMeierFitter import plot [as 别名]
dead.hist(bins=20, column='lenfol')
plt.show()
#plot the cumulative hazard (cdf)
dead.hist(bins=100, column='lenfol',
cumulative=True, normed=1)
plt.show()
#plot survival curve
kaplen_meier = KaplanMeierFitter()
time_of_event = df['lenfol'];
event = df['fstat'];
time = np.linspace(0, 2500, 100)
kaplen_meier.fit(time_of_event, timeline=time, event_observed=event, label='All patients')
kaplen_meier.plot()
plt.show()
#stratify Congestive Heart Complications
history = df['chf'] == 1;
kaplen_meier = KaplanMeierFitter()
kaplen_meier.fit(time_of_event[history], timeline=time, event_observed=event[history], label='Congestive heart complications')
ax = kaplen_meier.plot()
kaplen_meier.fit(time_of_event[~history], timeline=time, event_observed=event[~history], label='No congestive heart complications')
kaplen_meier.plot(ax=ax, c="b")
plt.show()
#Cox proportional hazard
示例14: list
# 需要导入模块: from lifelines.estimation import KaplanMeierFitter [as 别名]
# 或者: from lifelines.estimation.KaplanMeierFitter import plot [as 别名]
#df2= pd.read_table('genomicMatrix.tab',sep='\t')
#print list(df.columns.values)
survival_col = '_OS'
censor_col = '_OS_IND'
clinical_predictors = ['age_at_initial_pathologic_diagnosis']
df = df[pd.notnull(df[survival_col])]
tx = df['history_of_neoadjuvant_treatment']=='Yes'
ax = plt.subplot(111)
kmf1 = KaplanMeierFitter(alpha=0.95)
kmf1.fit(durations=df.ix[tx, survival_col], event_observed=df.ix[tx, censor_col], label=['Tx==Yes'])
kmf1.plot(ax=ax, show_censors=True, ci_show=False)
kmf2 = KaplanMeierFitter(alpha=0.95)
kmf2.fit(durations=df.ix[~tx, survival_col], event_observed=df.ix[~tx, censor_col], label=['Tx==No'])
kmf2.plot(ax=ax, show_censors=True, ci_show=False )
add_at_risk_counts(kmf1, kmf2, ax=ax)
plt.title ('Acute myeloid leukemia survival analysis with Tx and without Tx')
plt.xlabel(survival_col)
plt.savefig('km.png')
results = logrank_test(df.ix[tx, survival_col], df.ix[~tx, survival_col], df.ix[tx, censor_col], df.ix[~tx, censor_col], alpha=.99 )
results.print_summary()
cox = CoxPHFitter(normalize=False)
开发者ID:jokerbea,项目名称:BIN3005-PANDAS-AND-SKLEARN-SCRIPT-FOR-HANDLING-TCGA,代码行数:33,代码来源:survival_analysis_python.py
示例15: uw_tier_histplots
# 需要导入模块: from lifelines.estimation import KaplanMeierFitter [as 别名]
# 或者: from lifelines.estimation.KaplanMeierFitter import plot [as 别名]
def uw_tier_histplots():
sample['Underwriter Tier'] = sample['lead_underwriter_tier']
sample['IPO Duration'] = sample['IPO_duration']
ranks = ["-1", "0+", "7+", "9"]
def uw_tier_duration(x):
return sample[sample.lead_underwriter_tier==x]['IPO_duration']
kwstat = kruskalwallis(*[uw_tier_duration(x) for x in ranks])
# g = sb.FacetGrid(sample,
# row="Underwriter Tier",
# hue="Underwriter Tier",
# palette=cp_four("cool_r"),
# size=2, aspect=4,
# hue_order=ranks, row_order=ranks,
# legend=ranks, xlim=(0,1095))
# g.map(sb.distplot, "IPO Duration")
# plt.savefig("IPO_tiers_KP_survival.pdf", format='pdf', dpi=200)
from lifelines.estimation import KaplanMeierFitter
from lifelines.statistics import logrank_test
import matplotlib.pyplot as plt
ranks = ["-1", "0+", "7+", "9"]
ranklabels = ['No Underwriter', 'Low Rank', 'Mid Rank', 'Rank 9 (elite)']
kmf = KaplanMeierFitter()
# Success
f, ax = plt.subplots(1,1,figsize=(12, 4), sharex=True)
T = 1 # annotation line thickness
for rank, rlabel, color in zip(ranks, ranklabels, cp_four("cool_r")):
uw = sample[sample.lead_underwriter_tier==rank]
kmf.fit(uw['IPO_duration'],
label='{} N={}'.format(rlabel, len(uw)),
alpha=0.9)
kmf.plot(ax=ax, c=color, alpha=0.7)
quartiles = [int(np.percentile(kmf.durations, x)) for x in [25, 50, 75]][::-1]
aprops = dict(facecolor=color, width=T, headwidth=T)
if rank=="-1":
plt.annotate("75%: {} days".format(quartiles[0]),
(quartiles[0], 0.25),
xytext=(quartiles[0]+145, 0.25+.04),
arrowprops=aprops)
plt.annotate("50%: {} days".format(quartiles[1]),
(quartiles[1], 0.50),
xytext=(quartiles[1]+145, 0.50+.04),
arrowprops=aprops)
plt.annotate("25%: {} days".format(quartiles[2]),
(quartiles[2], 0.75),
xytext=(quartiles[2]+145, 0.75+0.04),
arrowprops=aprops)
elif rank=="9":
plt.annotate("75%: {} days".format(quartiles[0]),
(quartiles[0], 0.25),
xytext=(quartiles[0]+415, 0.25+.1),
arrowprops=aprops)
plt.annotate("50%: {} days".format(quartiles[1]),
(quartiles[1], 0.50),
xytext=(quartiles[1]+290, 0.50+.1),
arrowprops=aprops)
plt.annotate("25%: {} days".format(quartiles[2]),
(quartiles[2], 0.75),
xytext=(quartiles[2]+165, 0.75+0.1),
arrowprops=aprops)
plt.annotate("Kruskall Wallis\nH: {:.3f}\nprob: {:.3f}".format(*kwstat),
(960, 0.1))
plt.ylim(0,1)
plt.xlim(0,1095)
plt.title("Kaplan-Meier survival times by bank tier")
plt.xlabel("IPO Duration (days)")
plt.ylabel(r"$S(t)=Pr(T>t)$")
plt.savefig("IPO_tiers_KP_survival.pdf", format='pdf', dpi=200)