Exemplo n.º 1
0
for m in range(outputs.shape[0]):
	s = s + np.abs(lr.calcLogReg(w, inputs[:, m], 0.5) - outputs[m])

print float(s) / outputs.shape[0]"""




csv_file_objectT = csv.reader(open('test.csv', 'rb')) #Load in the csv file
headerT = csv_file_objectT.next() #Skip the fist line as it is a header

dataT=[] #Creat a variable called 'data'
for row in csv_file_objectT: #Skip through each row in the csv file
    dataT.append(row) #adding each row to the data variable
dataT = np.array(dataT) #Then convert from a list to an array

#print header
#pclass,name,sex,age,sibsp,parch,ticket,fare,cabin,embarked
#pclass0-0,name1,sex2-1,age3-2,sibsp4-3,parch5-4,ticket6,fare7-5,cabin8,embarked9
inputs = dataT[0:, (0, 2, 3, 4, 5, 7)]
inputs[:, 1] = lr.eapply(lambda x: int(x), dataT[0::,2] == "male")
inputs = np.matrix(lr.eapply(liq, inputs))

inputs = np.matrix(inputs).T.astype(float)

testResult = [lr.calcLogReg(w, inputs[:, m], 0.5) for m in range(inputs.shape[1])]

open_file_object = csv.writer(open("../output.csv", "wb"))

for i in xrange(len(dataT[:,0])):
	open_file_object.writerow([testResult[i]] + list(np.array(dataT[i,:]).reshape(-1))) #Write the row to the file
Exemplo n.º 2
0
import numpy as np
from random import seed, gauss
import LogisticRegression as lr

i = np.matrix([
	[1,1], [1,2], [1,3],
	[2,1], [2,2], [2,3],
	[3,1], [3,2], [3,3]
	]).T

o = np.matrix([[0, 0, 1, 0, 1, 1, 1, 1, 1]]).T

w = lr.batchGradientDescent(
	lr.LogisticDerivative, 
	i, o, np.matrix([[0], [0], [0]]), 0.5,  -0.5, 70)

print w

for i in xrange(3):
	for v in xrange(3):
		print i, v, lr.calcLogReg(w, np.matrix([i+1,v+1]).T, 0.5)

print lr.calcLogReg(w, np.matrix([3,0.85352157]).T, 0.5)