逻辑回归----梯度上升
- 逻辑回归—-梯度上升
- 测试代码
- import math
- def sigmoid(data, weight):
- z = sum([data[i]*weight[i] for i in range(len(data))])
- try:
- return 1.0/(1+math.exp(-z))
- except:
- if z > 0: return 1.0
- else: return 0.0
- def logistic_classify(data, weight):
- prob = sigmoid(data, weight)
- if prob > 0.5: return 1.0
- else: return 0.0
- def grad_ascent(dataset, datalabel):
- weight = [1 for i in range(len(dataset[0]))]
- alpha = 0.01
- for k in range(500):
- errset = []
- for i in range(len(dataset)):
- sig = sigmoid(dataset[i], weight)
- errset.append(datalabel[i]-sig)
- for i in range(len(dataset[0])):
- for j in range(len(dataset)):
- weight[i] += alpha*dataset[j][i]*errset[j]
- return weight
- def rand_grad_ascent(dataset, datalabel):
- weight = [1 for i in range(len(dataset[0]))]
- alpha = 0.01
- for i in range(len(dataset)):
- sig = sigmoid(dataset[i], weight)
- err = datalabel[i] – sig
- for j in range(len(weight)):
- weight[j] += alpha*err*dataset[i][j]
- return weight
- def test(class_func):
- f_train = open(\’horseColicTraining.txt\’)
- f_test = open(\’horseColicTest.txt\’)
- trainset, trainlabel = [], []
- for line in f_train.readlines():
- line_cur = line.strip().split(\’\t\’)
- trainset.append([1]+[float(line_cur[i]) for i in range(21)])
- trainlabel.append(float(line_cur[21]))
- trainweight = class_func(trainset, trainlabel)
- errnu, tolnum= 0, 0
- for line in f_test.readlines():
- line_cur = line.strip().split(\’\t\’)
- pred_class = logistic_classify([1]+[float(line_cur[i]) for i in range(21)], trainweight)
- read_class = float(line_cur[21])
- if pred_class == read_class:
- #print “class succ”
- pass
- else:
- errnu += 1
- #print “class fail, read_class=%d, pred_class=%d” %(read_class, pred_class)
- tolnum += 1
- print “totol num=%d, fail num = %d, rate = %f” % (tolnum, errnu, float(errnu)/tolnum)
- if __name__ == \’__main__\’:
- test(grad_ascent)
- test(rand_grad_ascent)
来自:http://blackproof.iteye.com/blog/2064084
仅供学习参考
版权声明:本文为altlb原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。