Skip to content

Instantly share code, notes, and snippets.

@mrvege
Created February 23, 2016 13:06
Show Gist options
  • Save mrvege/43e67e5677d043cb39f2 to your computer and use it in GitHub Desktop.
Save mrvege/43e67e5677d043cb39f2 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
# encoding: utf-8
from numpy import *
import matplotlib.pyplot as plt
def logistic(wTx):
return 1.0/(1.0 + exp(-wTx))
def file2matrix(filename,delimiter):
recordlist = []
fp = open(filename,"rb")
content = fp.read()
fp.close()
rowlist = content.splitlines()
# 逐行遍历 并将结果按分隔符分割为行向量
recordlist=[map(eval, row.split(delimiter)) for row in rowlist if row.strip()]
# 返回转换后的矩阵形式
return mat(recordlist)
def buildMat(dataSet):
m,n=shape(dataSet)
dataMat = zeros((m,n))
dataMat[:,0] = 1
dataMat[:,1:] = dataSet[:,:-1]
return dataMat
def load_data():
filename = "testSet.txt"
Input = file2matrix(filename, delimiter='\t')
label = Input[:,-1]
return Input, label
def logistic_regression_bgd(Input, label):
num_samples, num_features = shape(Input)
dataMat = buildMat(Input)
alpha = 0.001
steps = 500
weights = ones((num_features, 1))
for iteration in range(steps):
output = logistic(dataMat * mat(weights))
error = label - output
weights = weights + alpha*dataMat.T*error
print weights
return weights
def logistic_regression_sgd(Input, label):
num_samples, num_features = shape(Input)
dataMat = buildMat(Input)
alpha = 0.001
steps = 500
weights = ones(num_features)
for iteration in range(steps):
dataIndex = range(num_samples)
for i in range(num_samples):
randIndex = random.randint(0, len(dataIndex))
output = logistic(sum(dataMat[randIndex]*weights.T))
error = label[randIndex] - output
weights = weights + alpha*error*dataMat[randIndex]
del(dataIndex[randIndex])
print weights
return weights
def logistic_regression_nsgd(Input, label):
num_samples, num_features = shape(Input)
dataMat = buildMat(Input)
# alpha = 0.001
steps = 500
weights = ones(num_features)
for iteration in range(steps):
dataIndex = range(num_samples)
for i in range(num_samples):
alpha = 1.0/(1.0+i+iteration) + 0.0001
randIndex = random.randint(0, len(dataIndex))
output = logistic(sum(dataMat[randIndex]*weights.T))
error = label[randIndex] - output
weights = weights + alpha*error*dataMat[randIndex]
del(dataIndex[randIndex])
print weights
return weights
if __name__ == '__main__':
Input, label = load_data()
weights = logistic_regression_bgd(Input, label)
weights_2 = logistic_regression_sgd(Input, label)
weights_3 = logistic_regression_nsgd(Input, label)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment