-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathlogistic_regression_python.py
195 lines (152 loc) · 4.99 KB
/
logistic_regression_python.py
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# coding:utf-8
# Author:codewithzichao
# Date:2020-1-2
# E-mail:[email protected]
'''
数据集:Mnist
准确率:0.9919
时间:29.48268699645996
--------------
tips:在加载数据的时候,把>=5为1,<5为0这样处理数据的时候,在同样的训练次数与学习率的时候,最后的准确率只有78%左右。
可能是数据类别太多,导致比较混乱,所以在这里,我采取的是标签为0的为1,不为0的全为0。
这样准确率大大提高了。
'''
import numpy as np
import time
def loadData(fileName):
'''
加载数据
:param fileName:数据路径名
:return: 特征向量矩阵、还有标签矩阵
'''
data_list = [];
label_list = []
with open(fileName, "r") as f:
for line in f.readlines():
curline = line.strip().split(",")
if (int(curline[0]) >= 5):
label_list.append(1)
else:
label_list.append(0)
data_list.append([int(feature) / 255 for feature in curline[1:]])
data_matrix = np.array(data_list)
label_matrix = np.array(label_list).reshape(1, -1)
return data_matrix, label_matrix
def sigmoid(z):
'''
定义sigmoid函数
:param z: 输入
:return: 返回(0,1)的数
'''
result = 1 / (1 + np.exp(-z))
return result
def initialize_params(feature_dim):
'''
初始化参数w,b
:param feature_dim:实例特征数目
:return: 参数w,b
'''
w = np.zeros((feature_dim, 1))
b = 0
return w, b
def propagation(w, b, X, Y):
'''
一次前向与反向传播过程
:param w:参数w
:param b: 参数b
:param X: 输入的特征向量
:param Y: 输入的类别向量
:return:dw,db,costs
'''
N, _ = np.shape(X) # 训练集数目
X = X.T
# print(X.shape)
A = sigmoid(np.dot(w.T, X) + b)
# epsilon=1e-5
cost = -1 / N * (np.sum(Y * np.log(A) + (1 - Y) * np.log(1 - A)))
dz = A - Y
dw = 1 / N * np.dot(X, dz.T)
db = 1 / N * np.sum(dz)
assert (dw.shape == w.shape)
assert (db.dtype == float)
cost = np.squeeze(cost)
assert (cost.shape == ())
grads = {"dw": dw, "db": db}
return grads, cost
def optimization(w, b, X, Y, iterations, learning_rate):
'''
优化,使用batch GD
:param w: 参数w
:param b: 参数b
:param X: 输入的特征向量
:param Y: 输入的类别向量
:param iterations: 迭代次数(其实就是epoch)
:param learning_rate: 学习率
:return: 最优化的参数w,b,以及costs(costs可有可无,取决于你是否想看训练过程中的cost的变化)
'''
costs = []
for iter in range(iterations):
grads, cost = propagation(w, b, X, Y)
dw = grads["dw"]
db = grads["db"]
w = w - learning_rate * dw
b = b - learning_rate * db
# 每100次epoch打印一次信息
if (iter % 100 == 0):
costs.append(cost)
print(f"the current iteration is {iter},the current cost is {cost}.")
params = {"w": w, "b": b}
grads = {"dw": dw, "db": db}
return params, grads, costs
def predict(w, b, X):
'''
预测新实例的类别
:param w:最优化的参数w
:param b:最优化的参数b
:param X:实例的特征向量
:return:实例的类别
'''
N = X.shape[0]
prediction = np.zeros((1, N))
X = X.T
A = sigmoid(np.dot(w.T, X) + b)
for i in range(N):
if (A[0][i] <= 0.5):
prediction[0][i] = 0
else:
prediction[0][i] = 1
assert (prediction.shape == (1, N))
return prediction
def model(train_data, train_label, test_data, test_label, iterations, learning_rate):
'''
将上述定义的函数结合起来,就是整个LR模型的执行过程
:param train_data: 训练数据集
:param train_label: 训练数据集的标签
:param test_data: 测试数据集
:param test_label: 测试数据集的标签
:param iterations: 迭代次数(epoch)
:param learning_rate: 学习率
:return: 在测试数据集上的准确率
'''
w, b = initialize_params(train_data.shape[1])
params, grads, costs = optimization(w, b, train_data, train_label, iterations, learning_rate)
w = params["w"]
b = params["b"]
prediction = predict(w, b, test_data)
error = 0
for i in range(prediction.shape[1]):
if (prediction[0][i] != test_label[0][i]):
error += 1
accuracy = (prediction.shape[1] - error) / prediction.shape[1]
print(f"the accuracy is {accuracy}.")
d = {"w": w, "b": b, "costs": costs}
return d
if __name__ == "__main__":
start = time.time()
print("start load data.")
train_data, train_label = loadData("../MnistData/mnist_train.csv")
test_data, test_label = loadData("../MnistData/mnist_test.csv")
print("finished load data.")
d = model(train_data, train_label, test_data, test_label, iterations=200, learning_rate=0.7)
end = time.time()
print(f"the total time is {end - start}.")