Skip to content

Instantly share code, notes, and snippets.

@OriaGr
Last active August 12, 2016 17:20
Show Gist options
  • Save OriaGr/e49533e151771f5e5257b1060e7dec8b to your computer and use it in GitHub Desktop.
Save OriaGr/e49533e151771f5e5257b1060e7dec8b to your computer and use it in GitHub Desktop.
#blog post https://oriamathematics.wordpress.com/2016/08/12/binary-classification-with-logistic-regression/
import numpy as np
import matplotlib.pyplot as plt
def predict(X, W):
return 1/(1+np.exp(-np.dot(X, W)))
def logLikelihood(X, Y, W):
m = X.shape[0]
predictions = predict(X, W)
onesVector = np.ones((m, 1))
return np.dot(Y.T, np.log(predictions)) + np.dot((onesVector - Y).T, np.log(onesVector - predictions + np.min(predictions)/(100*m)))
def gradient(X, Y, W):
return np.dot(X.T, Y - predict(X, W))
def successRate(X, Y, W):
m = Y.shape[0]
predictions = predict(X, W) > 0.5
correct = (Y == predictions)
return 100 * np.sum(correct)/float(correct.shape[0])
trX = np.load("binaryMnistTrainX.npy")
trY = np.load("binaryMnistTrainY.npy")
teX = np.load("binaryMnistTestX.npy")
teY = np.load("binaryMnistTestY.npy")
m, n = trX.shape
trX = np.concatenate((trX, np.ones((m, 1))),axis=1)
teX = np.concatenate((teX, np.ones((teX.shape[0], 1))),axis=1)
W = np.random.rand(n+1, 1)
learningRate = 0.00001
numIter = 100
likelihoodArray = np.zeros((numIter, 1))
for i in range(0, numIter):
W = W + learningRate * gradient(trX, trY, W)
likelihoodArray[i, 0] = logLikelihood(trX, trY, W)
print("train success rate is %lf" %(successRate(trX, trY, W)))
print("test success rate is %lf" %(successRate(teX, teY, W)))
plt.plot(likelihoodArray)
plt.xlabel("Iteration")
h = plt.ylabel("Log-Likelihood")
h.set_rotation(0)
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment