-
-
Save goyder/db059e68d3dbcb146bbdbfdd23ee9f38 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 math | |
import random | |
# Define useful functions for iteration | |
def matmul(X, Y): | |
# 1-dimensional arrays only, here | |
sum = 0 | |
for i in range(len(X)): | |
sum = sum + X[i] * Y[i] | |
return sum | |
# Define our model | |
class LogisticRegression: | |
def __init__(self, feature_count, lr, iter): | |
# Accept inputs | |
self.feature_count = feature_count | |
self.lr = lr | |
self.iter = iter | |
# Establish weights and parameters, etc | |
self.w_bias = random.gauss(0, 0.01) | |
self.w_values = [] | |
for _ in range(feature_count): | |
self.w_values.append(random.gauss(0, 0.01)) | |
self.cost_values = [] | |
self.errors = None | |
def _input(self, X): | |
output = map(lambda x, self=self: matmul(x, self.w_values) + self.w_bias, X) | |
output = map(lambda z: 1. / (1. + math.exp(-z)), output) | |
return output | |
def fit(self, X, Y): | |
# Split our data into rows for future parameter optimisation | |
X_rowwise = {} | |
for f_i in range(self.feature_count): | |
X_rowwise[f_i] = map(lambda x, f_i=f_i: x[f_i], X) | |
# Run fit | |
for _ in range(self.iter): | |
# In each iteration, we: | |
# Calculate the output and error | |
output = self._input(X) | |
errors = [] | |
for i in range(len(Y)): | |
errors.append(Y[i] - output[i]) | |
# Calculate the change to the weights | |
for f_i in range(self.feature_count): | |
self.w_values[f_i] = self.w_values[f_i] + self.lr * matmul(X_rowwise[f_i], errors) | |
self.w_bias = self.w_bias + self.lr * reduce(lambda x1, x2: x1 + x2, errors) | |
# Calculate the logistic 'cost' | |
cost = -matmul(Y, map(math.log, output)) - matmul(map(lambda y: 1 - y, Y), map(math.log, map(lambda y_hat: 1 - y_hat, output))) | |
self.cost_values.append(cost) | |
print "Error cost: " + str(cost) | |
def predict(self, X): | |
output = self._input(X) | |
output = map(round, output) | |
return output | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment