-
Notifications
You must be signed in to change notification settings - Fork 0
/
sample.py
72 lines (59 loc) · 1.6 KB
/
sample.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
import numpy as np
from robustopt.dro import drlr_cxvpy
from robustopt.dro import lr
def sigmoid(z):
return 1/(1 + np.exp(-z))
def label(y):
l = []
for i in y:
if i == 0:
l.append(-1)
if i == 1:
l.append(1)
return np.array(l)
def train_dataset():
M = 10
N = 100
beta_true = np.array([1, 0.5, -0.5] + [0]*(M - 3))
x = (np.random.random((N, M)) - 0.5)*10
beta_true = beta_true.reshape(M,1)
x = x.reshape(N,M)
y = label(np.round(sigmoid(x@beta_true)))
y = y.reshape(N,1)
return x,y,beta_true
def test_dataset():
M = 10
N = 100
beta_true = np.array([1, 0.5, -0.5] + [0]*(M - 3))
x = (np.random.random((N, M)) - 0.5)*10
beta_true = beta_true.reshape(M,1)
x = x.reshape(N,M)
y = label(np.round(sigmoid(x@beta_true)))
y = y.reshape(N,1)
return x,y,beta_true
def main():
x_train,y_train,_ = train_dataset()
x_test ,y_test ,_ = test_dataset()
epsilon = 1e-5
kappa = 1
pnorm = 2
drlr = drlr_cxvpy.dr_logistic_regression(epsilon,kappa,pnorm)
drlr.fit(x_train,y_train)
y_est = drlr.infer(x_train)
C = drlr.confusion_matrix(y_train,y_est)
print(C)
y_est = drlr.infer(x_test)
C = drlr.confusion_matrix(y_test,y_est)
print(C)
pnorm = 2
epsilon = 1e-5
lr_ = lr.logistic_regression(pnorm,epsilon)
lr_.fit(x_train,y_train)
y_est = lr_.infer(x_train)
C = lr_.confusion_matrix(y_train,y_est)
print(C)
y_est = lr_.infer(x_test)
C = lr_.confusion_matrix(y_test,y_est)
print(C)
if __name__ == "__main__":
main()