def plot(self, columns=None, loc=None, iloc=None, **kwargs): """" A wrapper around plotting. Matplotlib plot arguments can be passed in, plus: Parameters ----------- columns: string or list-like, optional If not empty, plot a subset of columns from the ``cumulative_hazards_``. Default all. loc: iloc: slice, optional specify a location-based subsection of the curves to plot, ex: ``.plot(iloc=slice(0,10))`` will plot the first 10 time points. """ from matplotlib import pyplot as plt assert loc is None or iloc is None, "Cannot set both loc and iloc in call to .plot" def shaded_plot(ax, x, y, y_upper, y_lower, **kwargs): base_line, = ax.plot(x, y, drawstyle="steps-post", **kwargs) ax.fill_between(x, y_lower, y2=y_upper, alpha=0.25, color=base_line.get_color(), linewidth=1.0, step="post") def create_df_slicer(loc, iloc): get_method = "loc" if loc is not None else "iloc" if iloc is None and loc is None: user_submitted_ix = slice(0, None) else: user_submitted_ix = loc if loc is not None else iloc return lambda df: getattr(df, get_method)[user_submitted_ix] subset_df = create_df_slicer(loc, iloc) if not columns: columns = self.cumulative_hazards_.columns else: columns = _to_list(columns) set_kwargs_ax(kwargs) ax = kwargs.pop("ax") x = subset_df(self.cumulative_hazards_).index.values.astype(float) for column in columns: y = subset_df(self.cumulative_hazards_[column]).values index = subset_df(self.cumulative_hazards_[column]).index y_upper = subset_df(self.confidence_intervals_[column].loc["upper-bound"]).values y_lower = subset_df(self.confidence_intervals_[column].loc["lower-bound"]).values shaded_plot(ax, x, y, y_upper, y_lower, label=column, **kwargs) plt.hlines(0, index.min() - 1, index.max(), color="k", linestyles="--", alpha=0.5) ax.legend() return ax
def plot(self, columns=None, loc=None, iloc=None, **kwargs): """" A wrapper around plotting. Matplotlib plot arguments can be passed in, plus: Parameters ----------- columns: string or list-like, optional If not empty, plot a subset of columns from the ``cumulative_hazards_``. Default all. loc: iloc: slice, optional specify a location-based subsection of the curves to plot, ex: ``.plot(iloc=slice(0,10))`` will plot the first 10 time points. """ from matplotlib import pyplot as plt assert loc is None or iloc is None, "Cannot set both loc and iloc in call to .plot" def shaded_plot(ax, x, y, y_upper, y_lower, **kwargs): base_line, = ax.plot(x, y, drawstyle="steps-post", **kwargs) ax.fill_between(x, y_lower, y2=y_upper, alpha=0.25, color=base_line.get_color(), linewidth=1.0, step="post") def create_df_slicer(loc, iloc): get_method = "loc" if loc is not None else "iloc" if iloc is None and loc is None: user_submitted_ix = slice(0, None) else: user_submitted_ix = loc if loc is not None else iloc return lambda df: getattr(df, get_method)[user_submitted_ix] subset_df = create_df_slicer(loc, iloc) if not columns: columns = self.cumulative_hazards_.columns else: columns = _to_list(columns) set_kwargs_ax(kwargs) ax = kwargs.pop("ax") x = subset_df(self.cumulative_hazards_).index.values.astype(float) for column in columns: y = subset_df(self.cumulative_hazards_[column]).values index = subset_df(self.cumulative_hazards_[column]).index y_upper = subset_df(self.confidence_intervals_[column].loc["upper-bound"]).values y_lower = subset_df(self.confidence_intervals_[column].loc["lower-bound"]).values shaded_plot(ax, x, y, y_upper, y_lower, label=column, **kwargs) plt.hlines(0, index.min() - 1, index.max(), color="k", linestyles="--", alpha=0.5) ax.legend() return ax
def plot(self, columns=None, parameter=None, **errorbar_kwargs): """ Produces a visual representation of the coefficients, including their standard errors and magnitudes. Parameters ---------- columns : list, optional specify a subset of the columns to plot errorbar_kwargs: pass in additional plotting commands to matplotlib errorbar command Returns ------- ax: matplotlib axis the matplotlib axis that be edited. """ from matplotlib import pyplot as plt set_kwargs_ax(errorbar_kwargs) ax = errorbar_kwargs.pop("ax") errorbar_kwargs.setdefault("c", "k") errorbar_kwargs.setdefault("fmt", "s") errorbar_kwargs.setdefault("markerfacecolor", "white") errorbar_kwargs.setdefault("markeredgewidth", 1.25) errorbar_kwargs.setdefault("elinewidth", 1.25) errorbar_kwargs.setdefault("capsize", 3) z = inv_normal_cdf(1 - self.alpha / 2) params_ = self.params_.copy() standard_errors_ = self.standard_errors_.copy() if columns is not None: params_ = params_.loc[:, columns] standard_errors_ = standard_errors_.loc[:, columns] if parameter is not None: params_ = params_.loc[parameter] standard_errors_ = standard_errors_.loc[parameter] columns = params_.index hazards = params_.loc[columns].to_frame(name="coefs") hazards["se"] = z * standard_errors_.loc[columns] if isinstance(hazards.index, pd.MultiIndex): hazards = hazards.groupby(level=0, group_keys=False).apply( lambda x: x.sort_values(by="coefs", ascending=True)) else: hazards = hazards.sort_values(by="coefs", ascending=True) yaxis_locations = list(range(len(columns))) ax.errorbar(hazards["coefs"], yaxis_locations, xerr=hazards["se"], **errorbar_kwargs) best_ylim = ax.get_ylim() ax.vlines(0, -2, len(columns) + 1, linestyles="dashed", linewidths=1, alpha=0.65) ax.set_ylim(best_ylim) if isinstance(columns[0], tuple): tick_labels = ["%s: %s" % (c, p) for (p, c) in hazards.index] else: tick_labels = [i for i in hazards.index] plt.yticks(yaxis_locations, tick_labels) plt.xlabel("log(accelerated failure rate) (%g%% CI)" % ((1 - self.alpha) * 100)) return ax
def plot(self, columns=None, parameters=None, **errorbar_kwargs): """ Produces a visual representation of the coefficients, including their standard errors and magnitudes. Parameters ---------- columns : list, optional specify a subset of the columns (variables from the training data) to plot parameter : list, optional specify a subset of the parameters to plot errorbar_kwargs: pass in additional plotting commands to matplotlib errorbar command Returns ------- ax: matplotlib axis the matplotlib axis that be edited. """ from matplotlib import pyplot as plt set_kwargs_ax(errorbar_kwargs) ax = errorbar_kwargs.pop("ax") errorbar_kwargs.setdefault("c", "k") errorbar_kwargs.setdefault("fmt", "s") errorbar_kwargs.setdefault("markerfacecolor", "white") errorbar_kwargs.setdefault("markeredgewidth", 1.25) errorbar_kwargs.setdefault("elinewidth", 1.25) errorbar_kwargs.setdefault("capsize", 3) z = inv_normal_cdf(1 - self.alpha / 2) params_ = self.params_.copy() standard_errors_ = self.standard_errors_.copy() if columns is not None: assert isinstance(columns, list), "columns must be a list" params_ = params_.loc[:, columns] standard_errors_ = standard_errors_.loc[:, columns] if parameters is not None: assert isinstance(parameters, list), "parameter must be a list" params_ = params_.loc[parameters] standard_errors_ = standard_errors_.loc[parameters] columns = params_.index hazards = params_.loc[columns].to_frame(name="coefs") hazards["se"] = z * standard_errors_.loc[columns] hazards = hazards.swaplevel(1, 0).sort_index() yaxis_locations = list(range(len(columns) - 1, -1, -1)) ax.errorbar(hazards["coefs"], yaxis_locations, xerr=hazards["se"], **errorbar_kwargs) # set zero hline best_ylim = ax.get_ylim() ax.vlines(0, -2, len(columns) + 1, linestyles="dashed", linewidths=1, alpha=0.65) ax.set_ylim(best_ylim) if isinstance(columns[0], tuple): tick_labels = ["%s: %s" % (p, c) for (p, c) in hazards.index] else: tick_labels = [i for i in hazards.index] plt.yticks(yaxis_locations, tick_labels) plt.xlabel("log(accelerated failure rate) (%g%% CI)" % ((1 - self.alpha) * 100)) return ax