Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def cumulative_hazard_at_times(self, times, label=None) -> pd.Series:
"""
Return a Pandas series of the predicted cumulative hazard value at specific times.
Parameters
-----------
times: iterable or float
values to return the cumulative hazard at.
label: string, optional
Rename the series returned. Useful for plotting.
"""
label = utils.coalesce(label, self._label)
return pd.Series(
self._cumulative_hazard(self._fitted_parameters_, times), index=utils._to_1d_array(times), name=label
)
conditional_after: iterable, optional
Must be equal is size to X.shape[0] (denoted `n` above). An iterable (array, list, series) of possibly non-zero values that represent how long the
subject has already lived for. Ex: if :math:`T` is the unknown event time, then this represents
:math:`T | T > s`. This is useful for knowing the *remaining* hazard/survival of censored subjects.
The new timeline is the remaining duration of the subject, i.e. reset back to starting at 0.
"""
if isinstance(X, pd.Series):
return self.predict_cumulative_hazard(X.to_frame().T, times=times, conditional_after=conditional_after)
n = X.shape[0]
if times is not None:
times = np.atleast_1d(times).astype(float)
if conditional_after is not None:
conditional_after = _to_1d_array(conditional_after).reshape(n, 1)
if self.strata:
X = X.copy()
cumulative_hazard_ = pd.DataFrame()
if conditional_after is not None:
X["_conditional_after"] = conditional_after
for stratum, stratified_X in X.groupby(self.strata):
try:
strata_c_0 = self.baseline_cumulative_hazard_[[stratum]]
except KeyError:
raise StatError(
dedent(
"""The stratum %s was not found in the original training data. For example, try
the following on the original dataset, df: `df.groupby(%s).size()`. Expected is that %s is not present in the output."""
% (stratum, self.strata, stratum)
def cumulative_density_at_times(self, times, label=None) -> pd.Series:
"""
Return a Pandas series of the predicted cumulative density at specific times
Parameters
-----------
times: iterable or float
Returns
--------
pd.Series
"""
label = coalesce(label, self._label)
return pd.Series(1 - self.predict(times), index=_to_1d_array(times), name=label)
def survival_function_at_times(self, times, label=None) -> pd.Series:
"""
Return a Pandas series of the predicted survival value at specific times
Parameters
-----------
times: iterable or float
Returns
--------
pd.Series
"""
label = coalesce(label, self._label)
return pd.Series(self.predict(times), index=_to_1d_array(times), name=label)
def hazard_at_times(self, times, label=None) -> pd.Series:
"""
Return a Pandas series of the predicted hazard at specific times.
Parameters
-----------
times: iterable or float
values to return the hazard at.
label: string, optional
Rename the series returned. Useful for plotting.
"""
label = utils.coalesce(label, self._label)
return pd.Series(self._hazard(self._fitted_parameters_, times), index=utils._to_1d_array(times), name=label)
"""
Predict the {0} at certain point in time. Uses a linear interpolation if
points in time are not in the index.
Parameters
----------
times: scalar, or array
a scalar or an array of times to predict the value of {0} at.
interpolate: boolean, optional (default=False)
for methods that produce a stepwise solution (Kaplan-Meier, Nelson-Aalen, etc), turning this to
True will use an linear interpolation method to provide a more "smooth" answer.
"""
if callable(self._estimation_method):
return (
pd.DataFrame(self._estimation_method(utils._to_1d_array(times)), index=utils._to_1d_array(times))
.loc[times]
.squeeze()
)
estimate = getattr(self, self._estimation_method)
if not interpolate:
return estimate.asof(times).squeeze()
return utils.interpolate_at_times_and_return_pandas(estimate, times)