Base Solver Class#
Classes#
- class skscope.base_solver.BaseSolver(dimensionality, sparsity=None, sample_size=1, *, preselect=[], numeric_solver=convex_solver_nlopt, max_iter=100, group=None, ic_method=None, cv=1, cv_fold_id=None, split_method=None, random_state=None)[source]#
Get sparse optimal solution of convex objective function by searching all possible combinations of variables. Specifically,
BaseSolveraims to tackle this problem: \(\min_{x \in R^p} f(x) \text{ s.t. } ||x||_0 \leq s\), where \(f(x)\) is a convex objective function and \(s\) is the sparsity level. Each element of \(x\) can be seen as a variable, and the nonzero elements of \(x\) are the selected variables.- Parameters:
dimensionality (int) – Dimension of the optimization problem, which is also the total number of variables that will be considered to select or not, denoted as \(p\).
sparsity (int or array of int, optional) – The sparsity level, which is the number of nonzero elements of the optimal solution, denoted as \(s\). Default is
range(int(p/log(log(p))/log(p))).sample_size (int, default=1) – sample size, denoted as \(n\).
preselect (array of int, default=[]) – An array contains the indexes of variables which must be selected.
numeric_solver (callable, optional) – A solver for the convex optimization problem.
BaseSolverwill call this function to solve the convex optimization problem in each iteration. It should have the same interface asskscope.convex_solver_nlopt.max_iter (int, default=100) – Maximum number of iterations taken for converging.
group (array of shape (dimensionality,), default=range(dimensionality)) – The group index for each variable, and it must be an incremental integer array starting from 0 without gap. The variables in the same group must be adjacent, and they will be selected together or not. Here are wrong examples:
[0,2,1,2](not incremental),[1,2,3,3](not start from 0),[0,2,2,3](there is a gap). It’s worth mentioning that the concept “a variable” means “a group of variables” in fact. For example,``sparsity=[3]`` means there will be 3 groups of variables selected rather than 3 variables, andalways_include=[0,3]means the 0-th and 3-th groups must be selected.ic_method (callable, optional) – A function to calculate the information criterion for choosing the sparsity level.
ic(loss, p, s, n) -> ic_value, wherelossis the value of the objective function,pis the dimensionality,sis the sparsity level andnis the sample size. Used only whensparsityis array andcvis 1. Note thatsample_sizemust be given when usingic_method.cv (int, default=1) – The folds number when use the cross-validation method. - If
cv= 1, the sparsity level will be chosen by the information criterion. - Ifcv> 1, the sparsity level will be chosen by the cross-validation method.split_method (callable, optional) – A function to get the part of data used in each fold of cross-validation. Its interface should be
(data, index) -> part_datawhereindexis an array of int.cv_fold_id (array of shape (sample_size,), optional) – An array indicates different folds in CV, which samples in the same fold should be given the same number. The number of different elements should be equal to
cv. Used only when cv > 1. random_state : int, optional The random seed used for cross-validation.
- params#
The sparse optimal solution.
- Type:
array of shape(dimensionality,)
- objective_value#
The value of objective function on the solution.
- Type:
float
- support_set#
The indices of selected variables, sorted in ascending order.
- Type:
array of int
- get_config(deep=True)[source]#
Get parameters for this solver.
- Parameters:
deep (bool, default=True) – If True, will return the parameters for this solver and contained subobjects that are estimators.
- Returns:
params – Parameter names mapped to their values.
- Return type:
dict
- get_estimated_params()[source]#
Get the optimal parameters of the objective function.
- Returns:
parameters – The optimal solution of optimization.
- Return type:
array of shape (dimensionality,)
- get_result()[source]#
Get the result of optimization.
- Returns:
results – The result of optimization, including the following keys:
paramsarray of shape (dimensionality,)The optimal parameters.
support_setarray of intThe support set of the optimal parameters.
objective_valuefloatThe value of objective function at the optimal parameters.
eval_objectivefloatThe value of information criterion or mean loss of cross-validation.
- Return type:
dict
- get_support()[source]#
Get the support set of the optimal parameters.
- Returns:
support_set – The indices of selected variables, sorted in ascending order.
- Return type:
array of int
- set_config(**params)[source]#
Set the parameters of this solver.
- Parameters:
**params (dict) – Solver parameters.
- Returns:
Solver instance.
- Return type:
self
- solve(objective, data=None, layers=[], gradient=None, init_support_set=None, init_params=None, jit=False)[source]#
Optimize the optimization objective function.
- Parameters:
objective (callable) – The objective function to be minimized:
objective(params, data) -> losswhereparamsis a 1-D array with shape (dimensionality,) anddatais the fixed parameters needed to completely specify the function.objectivemust be written inJAXlibrary ifgradientis not provided.data (optional) – Extra arguments passed to the objective function and its derivatives (if existed).
layers (list of
Layerobjects, default=[]) –Layeris a “decorator” of the objective function. The parameters will be processed by theLayerbefore entering the objective function. The different layers can achieve different effects, and they can be sequentially concatenated together to form a larger layer, enabling the implementation of more complex functionalities. TheLayerobjects can be found inskscope.layers. Iflayersis not empty,objectivemust be written inJAXlibrary.init_support_set (array of int, default=[]) – The index of the variables in initial active set.
init_params (array of shape (dimensionality,), optional) – The initial value of parameters, default is an all-zero vector.
gradient (callable, optional) – A function that returns the gradient vector of parameters:
gradient(params, data) -> array of shape (dimensionality,), whereparamsis a 1-D array with shape (dimensionality,) anddatais the fixed parameters needed to completely specify the function. Ifgradientis not provided,objectivemust be written inJAXlibrary.jit (bool, default=False) – If
objectiveorgradientare written in JAX,jitcan be set to True to speed up the optimization.
- Returns:
parameters – The optimal solution of optimization.
- Return type:
array of shape (dimensionality,)