-
Notifications
You must be signed in to change notification settings - Fork 0
/
skscope_tools.py
114 lines (98 loc) · 4.5 KB
/
skscope_tools.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
# By Pongpisit Thanasutives
import jax.numpy as jnp
import numpy as np
from skscope import ScopeSolver
from sklearn.preprocessing import normalize
from abess import LinearRegression as abess_linear
from solvel0 import refine_solvel0
def get_support(coef_vec):
return np.where(np.abs(coef_vec)>0)[0]
def best_subset_solution(X_pre, y_pre, sparsity, p=1, normalize_order=2, normalize_axis=None, center_y=False, lstsq=False):
XX = X_pre
yy = y_pre.flatten()
if center_y:
yy = yy - yy.mean()
if normalize_axis is not None:
X_norm = np.linalg.norm(X_pre, ord=normalize_order, axis=normalize_axis)
XX = np.divide(XX, X_norm)
objective_function = lambda coefs: jnp.linalg.norm(yy-XX@coefs)**p
scope_solver = ScopeSolver(dimensionality=XX.shape[-1], sparsity=sparsity)
scope_solver.solve(objective_function)
coefs = scope_solver.params
if normalize_axis is not None:
coefs = np.divide(coefs, X_norm)
if lstsq:
supports = scope_solver.get_support()
coefs[supports] = np.linalg.lstsq(X_pre[:, supports], y_pre, rcond=None)[0][:, 0]
return coefs
def best_subset_all_solutions(X_pre, y_pre, sparsity, p=1, normalize_order=2, normalize_axis=None, center_y=False, refine=False):
XX = X_pre
yy = y_pre.flatten()
if center_y:
yy = yy - yy.mean()
if normalize_axis is not None:
X_norm = np.linalg.norm(X_pre, ord=normalize_order, axis=normalize_axis)
XX = np.divide(XX, X_norm)
objective_function = lambda coefs: jnp.linalg.norm(yy-XX@coefs)**p
all_coefs = []
all_supports = []
for sp in range(1, sparsity+1):
scope_solver = ScopeSolver(dimensionality=XX.shape[-1], sparsity=sp)
scope_solver.solve(objective_function)
coefs = scope_solver.params
if normalize_axis is not None:
coefs = np.divide(coefs, X_norm)
all_coefs.append(coefs)
all_supports.append(scope_solver.get_support())
all_coefs = np.array(all_coefs)
if refine:
if refine == 'original': all_supports = refine_solvel0(all_supports, (XX, yy), 'bic', False)
else: all_supports = refine_solvel0(all_supports, (X_pre, y_pre), 'bic', False)
all_supports = sorted([list(all_supports.track[e][0]) for e in all_supports.track], key=len)
for i, coefs in enumerate(all_coefs):
tmp_coef = np.zeros_like(coefs)
tmp_coef[all_supports[i]] = np.linalg.lstsq(X_pre[:, all_supports[i]], y_pre, rcond=None)[0][:, 0]
all_coefs[i] = tmp_coef
return all_coefs, all_supports
def abess_solution(X_pre, y_pre, sparsity, p=1, normalize_order=2, normalize_axis=None, center_y=False, lstsq=False):
XX = X_pre
yy = y_pre.flatten()
if center_y:
yy = yy - yy.mean()
if normalize_axis is not None:
X_norm = np.linalg.norm(X_pre, ord=normalize_order, axis=normalize_axis)
XX = np.divide(XX, X_norm)
coefs = abess_linear(support_size=sparsity).fit(XX, yy).coef_
if normalize_axis is not None:
coefs = np.divide(coefs, X_norm)
if lstsq:
supports = get_support(coefs)
coefs[supports] = np.linalg.lstsq(X_pre[:, supports], y_pre, rcond=None)[0][:, 0]
return coefs
def abess_all_solutions(X_pre, y_pre, sparsity, p=1, normalize_order=2, normalize_axis=None, center_y=False, refine=False):
XX = X_pre
yy = y_pre.flatten()
if center_y:
yy = yy - yy.mean()
if normalize_axis is not None:
X_norm = np.linalg.norm(X_pre, ord=normalize_order, axis=normalize_axis)
XX = np.divide(XX, X_norm)
objective_function = lambda coefs: jnp.linalg.norm(yy-XX@coefs)**p
all_coefs = []
all_supports = []
for sp in range(1, sparsity+1):
coefs = abess_linear(support_size=sp).fit(XX, yy).coef_
if normalize_axis is not None:
coefs = np.divide(coefs, X_norm)
all_coefs.append(coefs)
all_supports.append(get_support(coefs))
all_coefs = np.array(all_coefs)
if refine:
if refine == 'original': all_supports = refine_solvel0(all_supports, (XX, yy), 'bic', False)
else: all_supports = refine_solvel0(all_supports, (X_pre, y_pre), 'bic', False)
all_supports = sorted([list(all_supports.track[e][0]) for e in all_supports.track], key=len)
for i, coefs in enumerate(all_coefs):
tmp_coef = np.zeros_like(coefs)
tmp_coef[all_supports[i]] = np.linalg.lstsq(X_pre[:, all_supports[i]], y_pre, rcond=None)[0][:, 0]
all_coefs[i] = tmp_coef
return all_coefs, all_supports