-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLogistic_Regression.py
More file actions
94 lines (64 loc) · 2.27 KB
/
Copy pathLogistic_Regression.py
File metadata and controls
94 lines (64 loc) · 2.27 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
81
82
83
84
85
86
87
88
89
90
91
92
#Linear_Regression
import matplotlib.pyplot as plt
import numpy as np
import sklearn
from sklearn import datasets
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score
# load data
data, label = sklearn.datasets.make_moons(400, noise=0.30)
m = data.shape[0]
# draw original data
# plt.figure('Original')
# plt.scatter(data[:,0], data[:,1], c=label)
# plt.xlabel("X")
# plt.ylabel("Y")
################ my program #################
# def sigmoid(z):
# return 1.0/(1.0+np.float128(np.exp(-z)))
def sigmoid(inX):
#return 1.0/(1+exp(-inX))
#优化
if inX.any()>=0:
return 1.0/(1.0+np.exp(-inX))
else:
return np.exp(inX)/(1+np.exp(inX))
# S_X = data[:,0].sum()
S_Y = data[:,1].sum()
S_XY = np.sum(data[:,0]*data[:,1])
# S_XX = np.sum(data[:,0]*data[:,1])
learning_rate = 0.01
n = 5000
coef = np.array([0., 0.])
intercept = 0
for i in range(n):
error = np.array([label - sigmoid(np.dot(data, coef.T) + intercept)]) #转化成矩阵,要加[]
# errormat = np.mat(error)
# datamat = np.mat(data)
# print(error.shape, errormat.shape)
# print(data.shape, datamat.shape)
# print(errormat.shape, datamat.shape)
gradient = np.dot(error, data).sum(axis=0)
coef = coef + learning_rate * gradient
intercept = intercept + learning_rate * error.sum()
# J = 0
# J = -1/m * (label*np.log(sigmoid(np.dot(datamat, coef.T) + intercept)) + (1-label) * np.log(1 - sigmoid(np.dot(datamat, coef.T) + intercept))).sum()
predict_of_my_prog = []
for i in range(m):
if sigmoid(np.dot(data[i, :], coef.T) + intercept) > 0.5:
predict_of_my_prog.append(1)
else:
predict_of_my_prog.append(0)
print('parameters of my_prog:', coef,intercept)
print('accuracy_score_of_my_prog:', accuracy_score(predict_of_my_prog, label))
# print(predict_of_my_prog)
# print(label)
######## sklearn methon #################
clf = LogisticRegression(penalty='l1', C=1.0, random_state=0)
clf.fit(data, label)
y_pred = clf.predict(data)
# y_test = y_test.reshape(1, -1)
# print('正确率:', clf.score(data,label))
print('parameters of sklearn:', clf.coef_, clf.intercept_)
print('accuracy_score_of_sklearn:', accuracy_score(y_pred, label)) #真值和预测值的0和1的lable刚好相反,所以精度为0,实际上是100%
# print(label, y_pred, predict_of_my_prog)