Skip to content

Instantly share code, notes, and snippets.

View chokkan's full-sized avatar

Naoaki Okazaki chokkan

View GitHub Profile
@chokkan
chokkan / logress.py
Created October 10, 2012 13:38
Logistic Regression
import math
import random
def train(w, D, T):
for t in range(1, T+1):
x, y = random.choice(D)
a = sum([w[i] * x[i] for i in range(len(w))]) # a(x) = w \cdot x
g = y - (1. / (1. + math.exp(-a))) if -100. < a else y # g = y - p
eta = 1. / t
for i in range(len(w)): # w ← w + eta * g * x
@chokkan
chokkan / perceptron.py
Created October 10, 2012 13:34
Perceptron
def update(w, x, y):
a = 0.
for i in range(len(w)):
a += w[i] * x[i]
if a * y <= 0:
for i in range(len(w)):
w[i] += y * x[i]
def classify(w, x):
a = 0.