forked from jasonge27/picasso
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtutorial.py
More file actions
157 lines (118 loc) · 4.11 KB
/
tutorial.py
File metadata and controls
157 lines (118 loc) · 4.11 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
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
import pycasso
import numpy as np
from sklearn.preprocessing import scale
## Sparse linear regression
## Generate the design matrix and regression coefficient vector
n = 100 # sample number
d = 80 # sample dimension
c = 0.5 # correlation parameter
s = 20 # support size of coefficient
X = scale(np.random.randn(n,d)+c* np.tile(np.random.randn(n),[d,1]).T )/ (n*(n-1))**0.5
beta = np.append(np.random.rand(s), np.zeros(d-s))
## Generate response using Gaussian noise, and fit sparse linear models
noise = np.random.randn(n)
Y = np.matmul(X,beta) + noise
## l1 regularization solved with naive update
solver_l1 = pycasso.Solver(X,Y, lambdas=(100,0.05), family="gaussian")
solver_l1.train()
## mcp regularization
solver_mcp = pycasso.Solver(X,Y, lambdas=(100,0.05), penalty="mcp")
solver_mcp.train()
## scad regularization
solver_scad = pycasso.Solver(X,Y, lambdas=(100,0.05), penalty="scad")
solver_scad.train()
## Obtain the result
result = solver_l1.coef()
## print out training time
print(result['total_train_time'])
## lambdas used
print(solver_l1.lambdas)
## number of nonzero coefficients for each lambda
print(result['df'])
## coefficients and intercept for the i-th lambda
i = 30
print(solver_l1.lambdas[i])
print(result['beta'][i])
print(result['intercept'][i])
## Visualize the solution path
solver_l1.plot()
solver_mcp.plot()
solver_scad.plot()
################################################################
## Sparse logistic regression
## Generate the design matrix and regression coefficient vector
n = 100 # sample number
d = 80 # sample dimension
c = 0.5 # correlation parameter
s = 20 # support size of coefficient
X = scale(np.random.randn(n,d)+c* np.tile(np.random.randn(n),[d,1]).T )/ (n*(n-1))**0.5
beta = np.append(np.random.rand(s), np.zeros(d-s))
## Generate response and fit sparse logistic models
noise = np.random.randn(n)
p = 1/(1+np.exp(-np.matmul(X,beta) - noise))
Y = np.random.binomial(np.ones(n,dtype='int64'),p)
## l1 regularization
solver_l1 = pycasso.Solver(X,Y, lambdas=(100,0.05), family="binomial", penalty="l1")
solver_l1.train()
## mcp regularization
solver_mcp = pycasso.Solver(X,Y, lambdas=(100,0.05), family="binomial", penalty="mcp")
solver_mcp.train()
## scad regularization
solver_scad = pycasso.Solver(X,Y, lambdas=(100,0.05), family="binomial", penalty="scad")
solver_scad.train()
## Obtain the result
result = solver_l1.coef()
## print out training time
print(result['total_train_time'])
## lambdas used
print(solver_l1.lambdas)
## number of nonzero coefficients for each lambda
print(result['df'])
## coefficients and intercept for the i-th lambda
i = 30
print(solver_l1.lambdas[i])
print(result['beta'][i])
print(result['intercept'][i])
## Visualize the solution path
solver_l1.plot()
solver_mcp.plot()
solver_scad.plot()
################################################################
## Sparse poisson regression
## Generate the design matrix and regression coefficient vector
n = 100 # sample number
d = 80 # sample dimension
c = 0.5 # correlation parameter
s = 20 # support size of coefficient
X = scale(np.random.randn(n,d)+c* np.tile(np.random.randn(n),[d,1]).T )/ (n*(n-1))**0.5
beta = np.append(np.random.rand(s), np.zeros(d-s))/(s**0.5)
## Generate response and fit sparse logistic models
noise = np.random.randn(n)
p = np.exp(-np.matmul(X,beta) - noise)
Y = np.random.poisson(p, n)
## l1 regularization
solver_l1 = pycasso.Solver(X,Y, lambdas=(100,0.05), family="poisson", penalty="l1")
solver_l1.train()
## mcp regularization
solver_mcp = pycasso.Solver(X,Y, lambdas=(100,0.05), family="poisson", penalty="mcp")
solver_mcp.train()
## scad regularization
solver_scad = pycasso.Solver(X,Y, lambdas=(100,0.05), family="poisson", penalty="scad")
solver_scad.train()
## Obtain the result
result = solver_l1.coef()
## print out training time
print(result['total_train_time'])
## lambdas used
print(solver_l1.lambdas)
## number of nonzero coefficients for each lambda
print(result['df'])
## coefficients and intercept for the i-th lambda
i = 30
print(solver_l1.lambdas[i])
print(result['beta'][i])
print(result['intercept'][i])
## Visualize the solution path
solver_l1.plot()
solver_mcp.plot()
solver_scad.plot()