def log_probs(Z, rho):
"""
compute the probabilities of the logistic loss function in a way that is numerically stable
see also: http://stackoverflow.com/questions/20085768/
Parameters
----------
Z numpy.array containing training data with shape = (n_rows, n_cols)
rho numpy.array of coefficients with shape = (n_cols,)
Returns
-------
log_probs numpy.array of probabilities under the logit model
"""
scores = Z.dot(rho)
pos_idx = scores > 0
log_probs = np.empty_like(scores)
log_probs[pos_idx] = 1.0 / (1.0 + np.exp(-scores[pos_idx]))
log_probs[~pos_idx] = np.exp(scores[~pos_idx]) / (1.0 + np.exp(scores[~pos_idx]))
return log_probs
评论列表
文章目录