-
-
Save keflavich/4013464 to your computer and use it in GitHub Desktop.
Logistic prediction
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def sigmoid(X): | |
'''Compute the sigmoid function ''' | |
#d = zeros(shape=(X.shape)) | |
den = 1.0 + e ** (-1.0 * X) | |
d = 1.0 / den | |
return d | |
def compute_cost(theta, X, y): | |
''' | |
Comput cost for logistic regression | |
''' | |
#Number of training samples | |
theta.shape = (1, 3) | |
m = y.size | |
h = sigmoid(X.dot(theta.T)) | |
h[h==1] = 0.99999999999 | |
# according to wikipedia, the linear version is: | |
# sigmoid^y * (1-sigmoid)^(1-y) | |
J = ((y.T.dot(log(h))) + ((1.0 - y.T).dot(log(1.0 - h)))) | |
# not really sure why negative, but in order to make it a COST function and not something to maximize, need it | |
return -(1.0/m) * J.sum() | |
def compute_grad(theta, X, y): | |
#print theta.shape | |
theta.shape = (1, 3) | |
m = y.size | |
grad = zeros(3) | |
h = np.squeeze(sigmoid(X.dot(theta.T))) | |
delta = h - y | |
l = grad.size | |
for i in range(l): | |
sumdelta = delta.T.dot(X[:, i]) | |
grad[i] = (1.0 / m) * sumdelta * - 1 | |
theta.shape = (3,) | |
return grad |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment