def test_parametric_plotting_with_show_censors(self, block): n = 200 T = (np.sqrt(50) * np.random.exponential(1, size=n)) ** 2 E = T < 100 T = np.minimum(T, 100) wf = WeibullFitter().fit(T, E) wf.plot_density(show_censors=True) wf.plot_cumulative_density(show_censors=True) self.plt.title("test_parametric_plotting_with_show_censors:cumulative_density") self.plt.show(block=block) wf.plot_survival_function(show_censors=True) self.plt.title("test_parametric_plotting_with_show_censors:survival_function") self.plt.show(block=block) wf.plot_cumulative_hazard(show_censors=True) self.plt.title("test_parametric_plotting_with_show_censors:cumulative_hazard") self.plt.show(block=block) wf.plot_density(show_censors=True) self.plt.title("test_parametric_plotting_with_show_censors:density") self.plt.show(block=block) return
def test_logx_plotting(self, block): waltons = load_waltons() kmf = KaplanMeierFitter().fit(np.exp(waltons["T"]), waltons["E"], timeline=np.logspace(0, 40)) ax = kmf.plot(logx=True) wf = WeibullFitter().fit(np.exp(waltons["T"]), waltons["E"], timeline=np.logspace(0, 40)) wf.plot_survival_function(logx=True, ax=ax) self.plt.title("test_logx_plotting") self.plt.show(block=block)
def test_parametric_plotting_with_show_censors(self, block): n = 200 T = 50 * np.random.exponential(1, size=(n, 1)) ** 2 E = np.random.rand(n) > 0.2 wf = WeibullFitter().fit(T, E, timeline=np.linspace(0, 5, 1000)) wf.plot_cumulative_density(show_censors=True) self.plt.title("test_parametric_plotting_with_show_censors:cumulative_density") self.plt.show(block=block) wf.plot_survival_function(show_censors=True) self.plt.title("test_parametric_plotting_with_show_censors:survival_function") self.plt.show(block=block) wf.plot_cumulative_hazard(show_censors=True) self.plt.title("test_parametric_plotting_with_show_censors:cumulative_hazard") self.plt.show(block=block) return
import pandas as pd data = pd.read_csv('Dataset/telco_customer.csv') data['tenure'] = pd.to_numeric(data['tenure']) data = data[data['tenure'] > 0] # Replace yes and No in the Churn column to 1 and 0. 1 for the event and 0 for the censured data. data['Churn'] = data['Churn'].apply(lambda x: 1 if x == 'Yes' else 0) fig, axes = plt.subplots(2, 2, figsize=(16, 12)) T = data['tenure'] E = data['Churn'] wbf = WeibullFitter().fit(T, E, label='WeibullFitter') ef = ExponentialFitter().fit(T, E, label='ExponentialFitter') lnf = LogNormalFitter().fit(T, E, label='LogNormalFitter') llf = LogLogisticFitter().fit(T, E, label='LogLogisticFitter') wbf.plot_survival_function(ax=axes[0][0]) ef.plot_survival_function(ax=axes[0][1]) lnf.plot_survival_function(ax=axes[1][0]) llf.plot_survival_function(ax=axes[1][1]) plt.suptitle( 'Implementation of Paramteric Models to create survival functions on the teleco dataset' ) fig.text(0.5, 0.04, 'Timeline', ha='center') fig.text(0.04, 0.5, 'Probability', va='center', rotation='vertical') plt.savefig('Images/SurvivalFunctions.jpeg') plt.show()
CI = ((df['CITY'] == 1), "city") MM = ((df['MONDAY'] == 1), "Monday") BROKEN = ((df['DEAD'] > 0), "all broken down") OKAY = ((df['DEAD'] == 0), "all okay") # not used obviously fig, ax = plt.subplots(nrows=1, ncols=1, figsize=(8, 4)) ax.hist(df["KM"], bins=100, rwidth=0.8) fig0, ax0 = plt.subplots(nrows=1, ncols=1, figsize=(8, 4)) T = df['KM'] E = df['DEAD'] # all broken trucks wf = WeibullFitter().fit(T, E) wf.print_summary() wf.plot_survival_function(ax=ax0, ci_show=False, label="Full population") kmf = KaplanMeierFitter() kmf.fit(T, event_observed=E) kmf.survival_function_ kmf.cumulative_density_ kmf.plot_survival_function(ax=ax0, ci_show=False) #kmf.plot_cumulative_density() fig, ax = plt.subplots(nrows=1, ncols=1, figsize=(8, 4)) for X, x in [BROKEN, EN, MO, CI, MM]: T = df['KM'][X] E = df['DEAD'][X]