Created
December 10, 2012 06:25
-
-
Save suzaku/4248812 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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