code:
def sigmoid(x):
return 1.0/(1+np.exp(-x))
def cost(x,y,th):
pro = sigmoid(np.dot(x,th))
result = sum(-y * np.log(pro) - (1-y) * np.log(1-pro))
result = result/len(x) #len: number of feature rows
return result
def gradient(x,y,th):
xTrans = x.transpose()
sig = sigmoid(np.dot(x,th))
grad = np.dot(xTrans, ( sig - y ))
grad = grad / len(x) #len: number of feature rows
return grad
def hessian(x,y,th):
xTrans = x.transpose()
sig = sigmoid(np.dot(x,th))
result = (1.0/len(x) * np.dot(xTrans, x) * np.diag(sig) * np.diag(1 - sig) )
return result
def updateTh(x,y,th):
hessianInv = np.linalg.inv(hessian(x,y,th))
grad = gradient(x,y,th)
th = th - np.dot(hessianInv, grad)
return th
m = 80 #number of x rows
x = np.ones([m,3])
y = np.empty([m,1], dtype = int)
th = np.zeros([3,1])
hessianResult = np.identity(3) #identity 3x3
with open('exam.csv','r') as csvfile:
i = 0
reader = csv.reader(csvfile)
next(reader) #skip header
for line in reader:
x[i][1] = line[0]
x[i][2] = line[1]
y[i][0] = line[2]
i+=1
#m = x.shape[0] #number of x rows
for i in range(10):
costResult = cost(x,y,th)
hessianResult = hessian(x,y,th)
grad = gradient(x,y,th)
th = updateTh(x,y,th)
If I loop more than 28 times, I get a divide by 0 issue with the "sum" part of my cost function, and I also get an error saying a matrix cannot be inversed because it is singular. Not sure what is wrong, following exact algorithm given by my professor. The data set is a list of 80 student entries, with two exam scores per entry and a binary 1 or 0 for whether or not the student was admitted to a college.