public
Created

  • Download Gist
logistic_regression.py
Python
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
import random
import math
import csv
 
def hypothesis(params, features):
z = sum(p * f for p, f in zip(params, features))
return 1 / (1 + math.e ** -z)
 
 
def logistic_regression(learning_rate, samples):
params = [random.random()] * len(samples[0].features)
for s in samples:
error = s.target - hypothesis(params, s.features)
params = [p + (learning_rate * error * f) for p, f in zip(params, s.features)]
return params
 
class Sample(object):
 
def __init__(self, target, features):
self.target = target
self.features = features
 
if __name__ == '__main__':
with open("wine.data") as f:
record = csv.reader(f)
rows = (map(float, row) for row in record)
samples = [Sample(r[0], r[1:]) for r in rows]
 
learning_rate = 0.1
params = logistic_regression(0.1, samples)
for i, s in enumerate(samples):
print i, hypothesis(params, s.features)

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.