Skip to content

Commit

Permalink
Merge pull request #98 from abess-team/create-pull-request/patch
Browse files Browse the repository at this point in the history
Fixes by format action
  • Loading branch information
Mamba413 authored May 26, 2024
2 parents 3387b4c + 4aadcc4 commit 0a0dc68
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 22 deletions.
19 changes: 14 additions & 5 deletions pytest/test_skmodel.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@
def test_PortfolioSelection():
# load data
port = PortfolioSelection(sparsity=50, alpha=0.001, random_state=0)
dir = os.path.normpath("/docs/source/gallery/Miscellaneous/data/csi500-2020-2021.csv")
dir = os.path.normpath(
"/docs/source/gallery/Miscellaneous/data/csi500-2020-2021.csv"
)
X = pd.read_csv(CURRENT + dir, encoding="gbk")
keep_cols = X.columns[(X.isnull().sum() <= 20)]
X = X[keep_cols]
Expand Down Expand Up @@ -77,7 +79,8 @@ def test_NonlinearSelection():
)
+ np.sum(np.square(X[:, true_support_set_list[2]]), axis=1)
+ np.sum(
(2 * X[:, true_support_set_list[3]] - 1) * (2 * X[:, true_support_set_list[4]] - 1),
(2 * X[:, true_support_set_list[3]] - 1)
* (2 * X[:, true_support_set_list[4]] - 1),
axis=1,
)
+ noise
Expand Down Expand Up @@ -150,7 +153,9 @@ def make_Clayton2_data(n, theta=15, lambda1=1, lambda2=1, c1=1, c2=1):
time2 = -np.log(1 - u2) / lambda2
time1 = (
np.log(
1 - np.power((1 - u2), -theta) + np.power((1 - u1), -theta / (1 + theta)) * np.power((1 - u2), -theta)
1
- np.power((1 - u2), -theta)
+ np.power((1 - u1), -theta / (1 + theta)) * np.power((1 - u2), -theta)
)
/ theta
/ lambda1
Expand All @@ -174,12 +179,16 @@ def make_Clayton2_data(n, theta=15, lambda1=1, lambda2=1, c1=1, c2=1):
n, p, s, rho = 100, 100, 10, 0.5
beta = np.zeros(p)
beta[:s] = 5
Sigma = np.power(rho, np.abs(np.linspace(1, p, p) - np.linspace(1, p, p).reshape(p, 1)))
Sigma = np.power(
rho, np.abs(np.linspace(1, p, p) - np.linspace(1, p, p).reshape(p, 1))
)
X = np.random.multivariate_normal(mean=np.zeros(p), cov=Sigma, size=(n,))
lambda1 = 1 * np.exp(np.matmul(X, beta))
lambda2 = 10 * np.exp(np.matmul(X, beta))

y, delta = make_Clayton2_data(n, theta=50, lambda1=lambda1, lambda2=lambda2, c1=5, c2=5)
y, delta = make_Clayton2_data(
n, theta=50, lambda1=lambda1, lambda2=lambda2, c1=5, c2=5
)

model = MultivariateFailure(s)
model = model.fit(X, y, delta)
Expand Down
4 changes: 3 additions & 1 deletion skscope/base_solver.py
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,9 @@ def solve(
for i in range(len(layers) - 1):
assert layers[i].out_features == layers[i + 1].in_features
assert layers[-1].out_features == self.dimensionality
loss_, grad_, hess_ = BaseSolver._set_objective(objective, gradient, jit, layers)
loss_, grad_, hess_ = BaseSolver._set_objective(
objective, gradient, jit, layers
)
p = layers[0].in_features
for layer in layers[::-1]:
sparsity = layer.transform_sparsity(sparsity)
Expand Down
37 changes: 21 additions & 16 deletions skscope/solver.py
Original file line number Diff line number Diff line change
Expand Up @@ -627,7 +627,9 @@ def __set_objective_cpp(self, objective, gradient, hessian):
return objective

def __set_objective_py(self, objective, gradient, hessian, jit, layers=[]):
loss_, grad_, hess_ = BaseSolver._set_objective(objective, gradient, jit, layers)
loss_, grad_, hess_ = BaseSolver._set_objective(
objective, gradient, jit, layers
)

# hess
if hessian is None:
Expand Down Expand Up @@ -798,7 +800,7 @@ def _solve(
group,
)
# init
params = init_params
params = init_params
best_suppport_group_tuple = None
best_loss = np.inf
results = {} # key: tuple of ordered support set, value: params
Expand Down Expand Up @@ -1643,7 +1645,6 @@ def __init__(
self.use_gradient = True



class PDASSolver(BaseSolver):
r"""
Solve the best subset selection problem with the subset size :math:`k` by Primal-dual active set (PDAS) algorithm.
Expand Down Expand Up @@ -1701,6 +1702,7 @@ class PDASSolver(BaseSolver):
Wen C H, Zhang A J, Quan S J, Wang X Q. BeSS: An R Package for Best Subset Selection in Linear, Logistic and Cox Proportional Hazards Models[J]. Journal of Statistical Software, 2020, 94(4): 1-24.
"""

def __init__(
self,
dimensionality,
Expand All @@ -1715,7 +1717,7 @@ def __init__(
cv_fold_id=None,
split_method=None,
random_state=None,
):
):
super().__init__(
dimensionality=dimensionality,
sparsity=sparsity,
Expand All @@ -1739,8 +1741,8 @@ def _solve(
init_params,
data,
preselect,
group
):
group,
):
if sparsity <= preselect.size:
return super()._solve(
sparsity,
Expand All @@ -1752,7 +1754,7 @@ def _solve(
preselect,
group,
)

support_set_group = np.union1d(preselect, init_support_set)
group_num = len(np.unique(group))
group_indices = [np.where(group == i)[0] for i in range(group_num)]
Expand All @@ -1761,7 +1763,9 @@ def _solve(
all_support = np.arange(group_num)
diff_support = np.setdiff1d(all_support, support_set_group)
rng = np.random.default_rng(seed=self.random_state)
support_set_group = np.union1d(rng.choice(diff_support, diff_num), support_set_group)
support_set_group = np.union1d(
rng.choice(diff_support, diff_num), support_set_group
)
support = np.concatenate([group_indices[i] for i in support_set_group])

for n_iters in range(self.max_iter):
Expand All @@ -1773,20 +1777,21 @@ def _solve(
h = np.diag(np.array(self.hess_(params, data)))
g[support] = 0
gamma = -g / h
delta = 1/2 * h * np.square(params + gamma)
delta = 1 / 2 * h * np.square(params + gamma)
score = np.array(
[
np.sum(delta[group_indices[i]])
for i in range(group_num)
]
[np.sum(delta[group_indices[i]]) for i in range(group_num)]
)
score[preselect] = np.inf
support_set_group_new = np.argpartition(score, -sparsity)[-sparsity:]
support_new = np.concatenate([group_indices[i] for i in support_set_group_new])
are_equal = np.all(np.isin(support_set_group, support_set_group_new)) and np.all(np.isin(support_set_group_new, support_set_group))
support_new = np.concatenate(
[group_indices[i] for i in support_set_group_new]
)
are_equal = np.all(
np.isin(support_set_group, support_set_group_new)
) and np.all(np.isin(support_set_group_new, support_set_group))
if are_equal:
return params, support_new
else:
support_set_group = support_set_group_new
support = support_new
return params, support_new
return params, support_new

0 comments on commit 0a0dc68

Please sign in to comment.