-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathlogisticRegression.py
More file actions
80 lines (67 loc) · 2.05 KB
/
logisticRegression.py
File metadata and controls
80 lines (67 loc) · 2.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
from numpy import *
from utils import tool
# 逻辑回归算法
def sigmoid(intx: int):
"""
S函数
"""
if intx >= 0:
return 1.0 / (1 + exp(-intx))
else:
return exp(intx) / (1 + exp(intx))
def gradascent(datamatin, classlabels):
"""
梯度提升算法
"""
datamatrix = mat(datamatin)
labelmat = mat(classlabels).transpose()
m, n = shape(datamatrix)
alpha = 0.001
maxcycles = 500
weights = ones((n, 1))
for k in range(maxcycles):
h = sigmoid(datamatrix * weights)
error = (labelmat - h)
weights = weights + alpha * datamatrix.transpose() * error
return weights
def stocgradascent(datamatrix, classlabels, numiter=150):
"""
随机梯度提升算法
"""
m, n = shape(datamatrix)
weights = ones(n)
for j in range(numiter):
dataindex = list(range(m))
for i in range(m):
alpha = 4 / (1.0 + j + i) + 0.01
randindex = int(random.uniform(0, len(dataindex)))
tol = sum(datamatrix[randindex] * weights)
h = sigmoid(tol)
error = classlabels[randindex] - h
weights = weights + alpha * error * datamatrix[randindex]
del (dataindex[randindex])
return weights
def classifyvector(inx, weights):
"""
计算结果
"""
prob = sigmoid(sum(inx * weights))
if prob > 0.5:
return 1.0
else:
return 0.0
def colictest():
trainingset, traininglabels = tool.file2floatMatrix('../data/horseColicTraining.txt', 21)
testset, testlabels = tool.file2floatMatrix('../data/horseColicTest.txt', 21)
trainweights = stocgradascent(trainingset, traininglabels, 500)
errorcount = 0
numtestvec = 0.0
for i in range(testset.shape[0]):
numtestvec += 1
if int(classifyvector(testset[i], trainweights)) != testlabels[i]:
errorcount += 1
errorrate = (float(errorcount) / numtestvec)
print("the error rate of this test is: %f" % errorrate)
return errorrate
if __name__ == '__main__':
colictest()