Skip to content

Instantly share code, notes, and snippets.

@heolin
Last active January 6, 2016 22:51
Show Gist options
  • Select an option

  • Save heolin/c2c542bb48f980de45c3 to your computer and use it in GitHub Desktop.

Select an option

Save heolin/c2c542bb48f980de45c3 to your computer and use it in GitHub Desktop.
#Not mine code, but a very good example that helped me understand Logistic Regression
#Found it here: http://stackoverflow.com/questions/25880634/logistic-regression-objects-are-not-aligned
import numpy as np
from scipy.optimize import fmin_bfgs
import io
data = np.loadtxt('ex2data1.txt',delimiter=",")
m,n = data.shape
X = np.array(np.column_stack((np.ones(m),data[:,:-1])))
y = np.array(data[:,2].reshape(m,1))
theta = np.array(np.zeros(n).reshape(n,1))
def sigmoid(z):
return 1/(1+np.exp(-z))
def hypothesis(X,theta):
return sigmoid( X.dot(theta) )
def cost(theta):
h = hypothesis(X,theta)
cost = (-y.T.dot(np.log(h))-(1-y).T.dot(np.log(1-h)))/m
r = cost[0]
if np.isnan(r):
return np.inf
return r
def gradient(theta):
theta = theta.reshape(-1, 1)
h = hypothesis(X,theta)
grad = ((h-y).T.dot(X)).T/m
return grad.flatten()
def fmin():
initial_theta=np.zeros(n)
theta=fmin_bfgs(cost,initial_theta,fprime=gradient)
return theta
theta = fmin()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment