Create a gist now

Instantly share code, notes, and snippets.

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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment