Skip to content

Instantly share code, notes, and snippets.

@itarato
Created July 19, 2015 11:42
Show Gist options
  • Select an option

  • Save itarato/40755a4b76c5d753a834 to your computer and use it in GitHub Desktop.

Select an option

Save itarato/40755a4b76c5d753a834 to your computer and use it in GitHub Desktop.
2D Logistic Regression
import numpy as np
import matplotlib.pyplot as plt
if __name__ == '__main__':
fin = file('logreg.txt', 'r')
npdata = np.genfromtxt(fin, delimiter=',')
posData = npdata[npdata[:,2] == 1,:]
negData = npdata[npdata[:,2] == 0,:]
plt.figure(1)
plt.plot(posData[:,0], posData[:,1], 'r^')
plt.plot(negData[:,0], negData[:,1], 'bs')
m = len(npdata)
X = np.concatenate((np.ones((m, 1)), npdata[:,0:2].reshape(m, 2)), axis=1)
Y = npdata[:,2].reshape(m, 1)
O = np.ones((3, 1))
alpha = 0.1
iteration = 10000
history = np.ndarray((iteration, 3))
for i in range(iteration):
hOx = 1 / (1 + np.exp(-np.dot(O.T, X.T)))
# left = Y * np.log(hOx.T)
# right = (1 - Y) * np.log(1 - hOx.T)
# JO = np.sum((left + right) * X, axis=0) / -m
O = O - np.sum(alpha * (hOx.T - Y) * X, axis=0).reshape(3, 1)
history[i] = O[:,0]
coordX = np.linspace(1, iteration, num=iteration)
plt.figure(2)
plt.plot(coordX, history[:,0], 'r-')
plt.plot(coordX, history[:,1], 'g-')
plt.plot(coordX, history[:,2], 'b-')
resultX = np.linspace(-10, 10, num=21)
plt.figure(1)
plt.plot(resultX, (-O[0][0] - O[1][0] * resultX) / O[2][0], 'g-')
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment