Created
May 12, 2014 11:57
-
-
Save ansjsun/ca155b7495350ae57f92 to your computer and use it in GitHub Desktop.
logistic_regression
This file contains hidden or 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 matplotlib.pyplot as plt | |
import re; | |
data = [] ; | |
class instance: | |
def __init__(self,array): | |
self.id = array[0] | |
self.label = int(array[-1]) ; | |
self.feature = [int(i) for i in array[1:-1]] | |
for line in open("dataset.txt","r"): | |
if line[0]=='#': | |
continue ; | |
i = instance(re.split('\s+',line[:-1])) ; | |
data.append(i) ; | |
weights = [0.0 for i in range(5)] ; | |
rate = 0.0001 ; | |
max_iter = 3000 ; | |
def sigmoid(value): | |
return 1/(1+math.exp(-value)) | |
def classify(instance): | |
logit = 0.0 ; | |
for index in range(len(weights)): | |
# print index , instance.feature, instance.id | |
logit = logit + weights[index]*instance.feature[index] ; | |
return sigmoid(logit) ; | |
for i in range(max_iter): | |
global weights ; | |
lik = 0.0 | |
for n ,instance in enumerate(data): | |
predicted = classify(instance) | |
for index,weight in enumerate(weights): | |
weights[index] = weight+rate*(instance.label-predicted)*instance.feature[index] ; | |
lik = instance.label*math.log(classify(instance))+(1-instance.label)*math.log(1-classify(instance)) | |
print "iterator ",n , weights , lik | |
for i in data: | |
print classify(i), i.label |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment