Create a gist now

Instantly share code, notes, and snippets.

# coding:utf-8
import numpy as np
import math
import random
import matplotlib.pyplot as plt
dim = 2
#data = [((-0.5,-0.3), -1), ((-0.4,-0.2), -1), ((-0.5,0.3), -1) , ((1.0, 1.0), 1), ((1.0, 0.0), 1), ((-0.5, 0.8), 1)]
def divide(x):
return ((x[0], x[1]), x[2])
data = [divide(map(float, l.split(','))) for l in open("data.csv").read().rstrip('\n').split('\n') ]
weight = np.matrix([random.random() for i in range(dim)]).T
def phi(x):
return x
def logistic_sigmoid(x):
return 1.0 /(1.0+math.exp(-x))
def regression(x):
return logistic_sigmoid(weight.T.dot(phi(x))[0,0])
def main():
global weight
t = np.matrix([x[1] for x in data]).T
idx = 0
N = len(data)
R = np.zeros((N,N))
while idx < 10000:
idx += 1
y = np.matrix([regression(x[0]) for x in data]).T
p = np.matrix([phi(x[0]) for x in data])
deltaE = p.T.dot(y-t)
for i in range(N):
R[i,i] = y[i,0] * (1-y[i,0])
Hesse_inv = np.linalg.inv(p.T.dot(R).dot(p))
weight = weight - Hesse_inv.dot(deltaE)
if np.linalg.norm(y-t) < 1e-10:
break
plt.scatter([x[0][0] for x in data if x[1] == 1], [x[0][1] for x in data if x[1] == 1])
plt.scatter([x[0][0] for x in data if x[1] == 0], [x[0][1] for x in data if x[1] == 0], c="red")
x = np.arange(0,4,0.01)
y = -weight[0,0]/weight[1,0] * x
plt.plot(x,y)
plt.show()
if __name__=='__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment