Skip to content

Instantly share code, notes, and snippets.

@suzaku
Created December 10, 2012 06:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save suzaku/4248812 to your computer and use it in GitHub Desktop.
Save suzaku/4248812 to your computer and use it in GitHub Desktop.
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