Skip to content

Commit

Permalink
adding explict col ordering
Browse files Browse the repository at this point in the history
  • Loading branch information
CamDavidsonPilon committed Dec 5, 2014
1 parent ee278b0 commit e6e9350
Showing 1 changed file with 32 additions and 9 deletions.
41 changes: 32 additions & 9 deletions lifelines/estimation.py
Original file line number Diff line number Diff line change
Expand Up @@ -718,7 +718,9 @@ def _compute_confidence_intervals(self):

def predict_cumulative_hazard(self, X, id_col=None):
"""
X: a (n,d) covariate matrix
X: a (n,d) covariate numpy array or DataFrame. If a DataFrame, columns
can be in any order. If a numpy array, columns must be in the
same order as the training data.
Returns the hazard rates for the individuals
"""
Expand All @@ -727,25 +729,32 @@ def predict_cumulative_hazard(self, X, id_col=None):
raise NotImplementedError

n, d = X.shape
try:
X_ = X.values.copy()
except:
X_ = X.copy()
X_ = X.copy() if not self.fit_intercept else np.c_[X.copy(), np.ones((n, 1))]

cols = get_index(X)
if isinstance(X, pd.DataFrame):
order = self.cumulative_hazards_.columns.drop('baseline')
X_ = X[order].values.copy()
else:
X_ = X.copy()
X_ = X_ if not self.fit_intercept else np.c_[X_, np.ones((n, 1))]
return pd.DataFrame(np.dot(self.cumulative_hazards_, X_.T), index=self.timeline, columns=cols)

def predict_survival_function(self, X):
"""
X: a (n,d) covariate matrix
X: a (n,d) covariate numpy array or DataFrame. If a DataFrame, columns
can be in any order. If a numpy array, columns must be in the
same order as the training data.
Returns the survival functions for the individuals
"""
return np.exp(-self.predict_cumulative_hazard(X))

def predict_percentile(self, X, p=0.5):
"""
X: a (n,d) covariate matrix
X: a (n,d) covariate numpy array or DataFrame. If a DataFrame, columns
can be in any order. If a numpy array, columns must be in the
same order as the training data.
Returns the median lifetimes for the individuals.
http://stats.stackexchange.com/questions/102986/percentile-loss-functions
"""
Expand All @@ -754,14 +763,23 @@ def predict_percentile(self, X, p=0.5):

def predict_median(self, X):
"""
X: a (n,d) covariate matrix
X: a (n,d) covariate numpy array or DataFrame. If a DataFrame, columns
can be in any order. If a numpy array, columns must be in the
same order as the training data.
Returns the median lifetimes for the individuals
"""
return self.predict_percentile(X, 0.5)

def predict_expectation(self, X):
"""
Compute the expected lifetime, E[T], using covarites X.
X: a (n,d) covariate numpy array or DataFrame. If a DataFrame, columns
can be in any order. If a numpy array, columns must be in the
same order as the training data.
Returns the expected lifetimes for the individuals
"""
index = get_index(X)
t = self.cumulative_hazards_.index
Expand Down Expand Up @@ -1117,10 +1135,15 @@ def predict_partial_hazard(self, X):
"""
index = get_index(X)

if isinstance(X, pd.DataFrame):
order = self.hazards_.columns
X = X[order]

if self.normalize:
# Assuming correct ordering and number of columns
X = normalize(X, self._norm_mean.values, self._norm_std.values)


return pd.DataFrame(exp(np.dot(X, self.hazards_.T)), index=index)

def predict_cumulative_hazard(self, X):
Expand Down

0 comments on commit e6e9350

Please sign in to comment.