### Import the new solver Source: https://skscope.readthedocs.io/en/latest/_sources/contribute/ContributeCode.rst Example of importing the newly implemented solver after installation. ```python from skscope import NewSolver ``` -------------------------------- ### Install official release via pip Source: https://skscope.readthedocs.io/en/latest/userguide/install.html Recommended installation method for most users. ```bash pip install skscope ``` -------------------------------- ### Basic Data Loading Example Source: https://skscope.readthedocs.io/en/latest/_sources/gallery/LinearModelAndVariants/isotonic-regression.ipynb Demonstrates how to load data from a CSV file using pandas. Ensure pandas is installed. ```python import pandas as pd df = pd.read_csv('data.csv') print(df.head()) ``` -------------------------------- ### Install documentation dependencies Source: https://skscope.readthedocs.io/en/latest/_sources/contribute/ContributeDocs.rst Install the required Python packages for building the documentation. ```bash pip install -r docs/requirements.txt ``` -------------------------------- ### SKscope Example Source: https://skscope.readthedocs.io/en/latest/_sources/gallery/Miscellaneous/exemplar-based-clustering.ipynb A basic example demonstrating the usage of SKscope. ```python rzBa8jY5gEN/eKLWADkLyT0wclAKBQGj6uKeuFoVKhTGjXWgNjN/jQW00YUjNQGux0FReGqz4mJSCObM9AZ+XuI5daSzdEhw0lUp08QloLPHUr12FUqvF1K5jMIBucqBQa1DpDSiUyohgOxAIEPB68Hu9GDIyoxbT0MRZg+t+AgH8LieYzP/Vr0oIcZiy2+2hzHZ8fDwpKSkAoW1Go5GvvvoKtVpNY2Mjjz32GIsWLeLBBx9k6tSpQDBrbTKZeHT0aIacfjoJKhVby8pYtGgRPU88EYvFQk1NDWlpacTHx9O+ffuY9+H3+yPWlDfLycmhqKgo1BZMAm4hxL7i83jwNtbjrNhGwOtFY01AZ0vAWVGGp74WhVKFPikFS9cjcFVX0Li1BLXRjM6WSMDvw5TZHldVBS57Jfj9qAxGDMlpuOyVeOpqMGa0216ZvB6/y4XaaEali16w0u/zEfB6UKo1qAxGfE2N4QcolOiSUnBsXBfsMittwQ5KEnCLA1rA78Pv9Qan0hAIrrNRKgl4vdSvWxnqaa1LTMFtr8RVXRk611Nfi6e+FlNmBwJeL4a0DNz2Khy1NcFr6fTBddgqNT6XE3etnYYNa4EAPmcTjVs2ojbF7ShioVRizuyAV6FAvT2bHggE8DU1Ur9xDQGPB40lHnNWJ5yV5aHp57qEJJQaLY7NG4OXkaIXQoi9JFpQbbPZ2LBhAzfffHMouG3upT1+/PiwgDc3N5eRI0dyzTXXAPD888/z7bffUlJSwlNPPYVSqaSjVkvcmDEotxdS6wRk5eSwoUsXPpk7l4cffjh0rVdeeYXOnTuHrl9SUhIqlNacSff7/RQUFISOabl+HKQtmBBi72iexu33uFGqNcEiZYEATeVbd2STlUq0ZjN1a4pDwWwAbyjI1toSCPh8qA1GGjauDbWj1SWnYuncA7/LFXxmLN0c2te4ZSPm9p1pLN0CgeA1NZZ4DKkZYRluv8dDY9nm4L00P2M6GnDVVBHw+dHEWdAlJuPctpWA34fKYIxYDy4ODodFwB2I1pPhEHMofke/z4e3oQ5XjZ2Az4O3oQFdYhIqoxlPrT3Yk1ClQqU3oLXaqF+3Mup1mrZtxdS2PY1lm/E17pgC5Hc5aVi/Gn1KOr4mB576urDzvI4G9MlpLW7IT0PJeiwdu+FXqVGqVPg9burWrQwN0p66GryOYJVzQ1oGfrcbZ2V5aOqRUqOVgFsIsVe0DGabDR48mAkTJnDTTTeFBbWjR4/miSeeiJgmXlBQQCAQ4O677+aYY45h4sSJ3HzzzaH9H7/2Gr0//hjlrFlh56kLC2kPXPG//4UC7oKCAm655RamTp1KVlZWRFVyh8PB0KFDGT16NGPHjsXn8+F2u0MtxJornUtbMHGgOBSfrXZ2qH5Hn9uFs2Ibfo8bX1Mjfp+XuPaC ``` -------------------------------- ### Compare Warm Start Strategies Source: https://skscope.readthedocs.io/en/latest/_sources/gallery/GeneralizedLinearModels/logistic-regression.ipynb Demonstrates the performance difference between default warm start and explicitly disabling it in ScopeSolver. ```python # Record start time start_time = time.time() solver_ws = ScopeSolver(p, sparsity = range(10), sample_size = n, cv = 5, split_method=lambda data, index: (data[0][index, :], data[1][index])) solver_ws.solve(logistic_loss_cv, jit=True, data=(X, y)) # Calculate runtime runtime = time.time() - start_time print("Runtime:", runtime, "seconds") print("True support set: ", (true_params.nonzero()[0])) print("Estimated support set: ", (solver_ws.support_set)) ``` ```python # Record start time start_time = time.time() solver_nws = ScopeSolver(p, sparsity = range(10), sample_size = n, cv = 5, split_method=lambda data, index: (data[0][index, :], data[1][index])) solver_nws.warm_start = False solver_nws.solve(logistic_loss_cv, jit=True, data=(X, y)) # Calculate runtime runtime = time.time() - start_time print("Runtime:", runtime, "seconds") print("True support set: ", (true_params.nonzero()[0])) print("Estimated support set: ", (solver_nws.support_set)) ``` -------------------------------- ### Logging Setup Source: https://skscope.readthedocs.io/en/latest/_sources/gallery/FusionModels/depth-first-search-graph-trend-filtering.ipynb Basic configuration for logging messages to a file. Logs will be appended to 'app.log'. ```python import logging def setup_logging(): logging.basicConfig( level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s', filename='app.log', filemode='a' ) print("Logging configured. Messages will be written to app.log") # Example usage: # setup_logging() # logging.info('Application started.') ``` -------------------------------- ### Basic JavaScript Example Source: https://skscope.readthedocs.io/en/latest/_sources/gallery/FusionModels/piecewise-linear-trend-filtering.ipynb A simple JavaScript snippet demonstrating a basic function. No specific setup or imports are required for this example. ```javascript function example() { return true; } ``` -------------------------------- ### Install Documentation Dependencies Source: https://skscope.readthedocs.io/en/latest/contribute/ContributeCode.html Install the necessary packages for developing Python API documentation using Sphinx and related tools. Ensure all packages in docs/requirements.txt are installed. ```bash pip install -r docs/requirements.txt ``` -------------------------------- ### Database Connection Example Source: https://skscope.readthedocs.io/en/latest/_sources/gallery/FusionModels/depth-first-search-graph-trend-filtering.ipynb Illustrates establishing a connection to a PostgreSQL database. Replace placeholders with actual credentials. Requires 'psycopg2'. ```python import psycopg2 def connect_db(db_params): try: conn = psycopg2.connect(**db_params) print("Database connection established successfully.") return conn except psycopg2.Error as e: print(f"Error connecting to database: {e}") return None # Example usage: # db_config = { # 'database': 'mydatabase', # 'user': 'myuser', # 'password': 'mypassword', # 'host': 'localhost', # 'port': '5432' # } # connection = connect_db(db_config) ``` -------------------------------- ### Import Dependencies Source: https://skscope.readthedocs.io/en/latest/_sources/gallery/LinearModelAndVariants/linear-mixed-model.ipynb Initial setup for the environment, including necessary libraries for numerical computation and modeling. ```python import numpy as np import jax.numpy as jnp import matplotlib.pyplot as plt import seaborn as sns from skscope import ScopeSolver import warnings warnings.filterwarnings('ignore') ``` -------------------------------- ### Install official release via conda Source: https://skscope.readthedocs.io/en/latest/userguide/install.html Alternative installation method for Linux or Mac users. ```bash conda install skscope ``` -------------------------------- ### Install skscope via Package Managers Source: https://skscope.readthedocs.io/en/latest/_sources/userguide/install.rst Standard installation methods for official releases using pip or conda. ```Bash pip install skscope ``` ```Bash conda install skscope ``` -------------------------------- ### Importing skscope dependencies Source: https://skscope.readthedocs.io/en/latest/_sources/gallery/SparsityLevelSelection/supported-information-criteria-and-cross-validation.ipynb Initial setup for using skscope solvers and utilities with JAX and NumPy. ```python import numpy as np import jax.numpy as jnp from skscope import ScopeSolver, utilities from sklearn.datasets import make_regression import warnings warnings.filterwarnings('ignore') ``` -------------------------------- ### Build skscope from Source Source: https://skscope.readthedocs.io/en/latest/_sources/userguide/install.rst Install the package in editable mode to allow for development changes without rebuilding. ```Bash pip install -e . ``` -------------------------------- ### Import Libraries for Sparse Matrix Example Source: https://skscope.readthedocs.io/en/latest/feature/ComputationalTips.html Import necessary libraries including numpy, jax.numpy, jax.experimental.sparse, skscope, and scipy.sparse. Filter warnings for cleaner output. ```python import numpy as np import jax.numpy as jnp from jax.experimental import sparse from skscope import ScopeSolver import scipy.sparse as sp import warnings warnings.filterwarnings('ignore') ``` -------------------------------- ### Bash: Scripting Example Source: https://skscope.readthedocs.io/en/latest/_sources/gallery/FusionModels/depth-first-search-graph-trend-filtering.ipynb A simple Bash script for automating tasks. Ensure correct permissions and environment variables are set. ```bash #!/bin/bash echo "Hello, World!" ``` -------------------------------- ### File Path Example in JavaScript Source: https://skscope.readthedocs.io/en/latest/_sources/gallery/FusionModels/piecewise-linear-trend-filtering.ipynb Illustrates how to define a file path string in JavaScript, including handling backslashes. This is useful for path manipulation. ```javascript const path = "C:\\Users\\file"; ``` -------------------------------- ### Install JAX with CUDA Support Source: https://skscope.readthedocs.io/en/latest/_sources/feature/ComputationalTips.rst To utilize GPU acceleration with JAX and skscope, install the appropriate JAX version matching your CUDA toolkit. This command installs JAX with CUDA 12 support. ```bash pip install -U "jax[cuda12]" ``` -------------------------------- ### Build documentation locally Source: https://skscope.readthedocs.io/en/latest/_sources/contribute/ContributeDocs.rst Generate the HTML documentation from the Jupyter notebooks in the docs directory. ```bash make html ``` -------------------------------- ### Configuration File Loading Source: https://skscope.readthedocs.io/en/latest/_sources/gallery/FusionModels/depth-first-search-graph-trend-filtering.ipynb Shows how to load configuration settings from a JSON file. Assumes a 'config.json' file exists in the same directory. ```python import json def load_config(filepath='config.json'): try: with open(filepath, 'r') as f: config = json.load(f) return config except FileNotFoundError: print(f"Error: Configuration file '{filepath}' not found.") return None except json.JSONDecodeError: print(f"Error: Could not decode JSON from '{filepath}'.") return None # Example usage: # app_config = load_config() ``` -------------------------------- ### Cross Validation Output Example Source: https://skscope.readthedocs.io/en/latest/gallery/SparsityLevelSelection/supported-information-criteria-and-cross-validation.html Example output showing sparsity and active set results for cross-validation. ```text sparsity: 5 active set: [10, 16, 20, 28, 45] ``` -------------------------------- ### Import Necessary Libraries Source: https://skscope.readthedocs.io/en/latest/_sources/gallery/GraphicalModels/sparse-precision-matrix.ipynb Initializes the environment by importing required libraries for numerical computation, optimization, and visualization. ```python from skscope import ScopeSolver import jax.numpy as jnp import numpy as np from sklearn.datasets import make_sparse_spd_matrix import cvxpy as cp import matplotlib.pyplot as plt import seaborn as sns import scipy.linalg as la ``` -------------------------------- ### OMPSolver Initialization and Usage Source: https://skscope.readthedocs.io/en/latest/autoapi/solver.html This snippet demonstrates how to initialize and use the OMPSolver class to find a sparse optimal solution. ```APIDOC ## OMPSolver ### Description Get sparse optimal solution of convex objective function by Orthogonal Matching Pursuit (OMP) algorithm. Specifically, `OMPSolver` aims to tackle this problem: minx∈Rpf(x) s.t. ||x||0≤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. ### Method __init__ ### Parameters #### Path Parameters - **dimensionality** (int) - Required - Dimension of the optimization problem, which is also the total number of variables that will be considered to select or not, denoted as p. #### Query Parameters - **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. - **threshold** (float, default=0.0) - The threshold to determine whether a variable is selected or not. - **strict_sparsity** (bool, default=True) - Whether to strictly control the sparsity level to be `sparsity` or not. - **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. `OMPSolver` will call this function to solve the convex optimization problem in each iteration. It should have the same interface as `skscope.convex_solver_LBFGS`. - **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, and `always_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`, where `loss` is the value of the objective function, `p` is the dimensionality, `s` is the sparsity level and `n` is the sample size. Used only when `sparsity` is array and `cv` is 1. Note that `sample_size` must be given when using `ic_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. - If `cv` > 1, the sparsity level will be chosen by the cross-validation method. - **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. - **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_data` where `index` is an array of int. - **random_state** (int, optional) - The random seed used for cross-validation. ### Returns - **params** (array of shape(dimensionality,)) - The sparse optimal solution. - **objective_value** (float) - The value of objective function on the solution. - **support_set** (array of int) - The indices of selected variables, sorted in ascending order. ### References Yuan X T, Li P, Zhang T. Gradient Hard Thresholding Pursuit[J]. J. Mach. Learn. Res., 2017, 18(1): 6027-6069. ``` -------------------------------- ### Example Output for BIC Source: https://skscope.readthedocs.io/en/latest/gallery/SparsityLevelSelection/supported-information-criteria-and-cross-validation.html This is an example output showing the sparsity and active set identified after applying the BIC criterion for model selection. ```text sparsity: 3 active set: [10, 20, 45] ``` -------------------------------- ### Error Handling Example Source: https://skscope.readthedocs.io/en/latest/_sources/gallery/FusionModels/1d-trend-filtering.ipynb Provides an example of how Skscope handles errors during processing. This snippet is for illustrative purposes and may not reflect all error scenarios. ```python from skscope import Scope import pandas as pd df = pd.DataFrame({'col1': [1, 'a', 3]}) scope = Scope(df) try: scope.process_data() except Exception as e: print(f"An error occurred: {e}") ``` -------------------------------- ### Initialize Solver with Cross-Validation Source: https://skscope.readthedocs.io/en/latest/feature/DataScienceTool.html Configure the solver with sample size, split method, and fold count to enable cross-validation. ```python solver = ScopeSolver( dimensionality=p, ## there are p parameters sparsity=[1, 2, 3, 4, 5], ## we want to select 1-5 variables sample_size=n, ## the number of samples split_method=split_method, ## use split_method to split data cv=10, ## use 10-fold cross validation ) params = solver.solve(custom_objective, data = (X, y)) ``` -------------------------------- ### Import required libraries Source: https://skscope.readthedocs.io/en/latest/gallery/GraphicalModels/sparse-gaussian-precision-matrix.html Initializes the environment with necessary libraries for optimization, data handling, and visualization. ```python from scope import ScopeSolver import jax.numpy as jnp import numpy as np from sklearn.datasets import make_sparse_spd_matrix from sklearn.covariance import GraphicalLassoCV import cvxpy as cp import matplotlib.pyplot as plt import matplotlib.cm as cm import seaborn as sns import networkx as nx import scipy.linalg as la import warnings warnings.filterwarnings("ignore", message="invalid value encountered in subtract") ``` -------------------------------- ### Install JAX with CUDA Support Source: https://skscope.readthedocs.io/en/latest/feature/ComputationalTips.html To utilize GPU acceleration, install a JAX version that matches your CUDA toolkit. This allows skscope to leverage GPU computation transparently. ```bash pip install -U "jax[cuda12]" ``` -------------------------------- ### Skscope Solver with Warm Start Enabled Source: https://skscope.readthedocs.io/en/latest/gallery/GeneralizedLinearModels/logistic-regression.html Initializes and solves using the skscope solver with the default warm-start strategy enabled. Records and prints the runtime and estimated support set. ```python # Record start time start_time = time.time() solver_ws = ScopeSolver(p, sparsity = range(10), sample_size = n, cv = 5, split_method=lambda data, index: (data[0][index, :], data[1][index])) solver_ws.solve(logistic_loss_cv, jit=True, data=(X, y)) # Calculate runtime untime = time.time() - start_time print("Runtime:", runtime, "seconds") print("True support set: ", (true_params.nonzero()[0])) print("Estimated support set: ", (solver_ws.support_set)) ``` -------------------------------- ### GET /websites/skscope_readthedocs_io_en Source: https://skscope.readthedocs.io/en/latest/_sources/gallery/Miscellaneous/exemplar-based-clustering.ipynb Retrieves the documentation content for the SKScope project. ```APIDOC ## GET /websites/skscope_readthedocs_io_en ### Description Retrieves the documentation content for the SKScope project. ### Method GET ### Endpoint /websites/skscope_readthedocs_io_en ### Response #### Success Response (200) - **content** (string) - The documentation content for the project. #### Response Example { "content": "Project: /websites/skscope_readthedocs_io_en..." } ``` -------------------------------- ### GET /get_support Source: https://skscope.readthedocs.io/en/latest/_modules/skscope/base_solver.html Retrieves the support set of the optimal parameters. ```APIDOC ## GET /get_support ### Description Returns the support set indices of the optimal parameters. ### Method GET ### Response #### Success Response (200) - **support_set** (array) - The indices of the support set. ``` -------------------------------- ### Initialize Solver with Sparse Matrix Support Source: https://skscope.readthedocs.io/en/latest/_sources/feature/ComputationalTips.rst Import necessary libraries including `jax.numpy`, `jax.experimental.sparse`, and `scipy.sparse` to work with sparse matrices in skscope. This setup is demonstrated for linear regression. ```python import numpy as np import jax.numpy as jnp from jax.experimental import sparse from skscope import ScopeSolver import scipy.sparse as sp import warnings warnings.filterwarnings('ignore') ``` -------------------------------- ### GET /get_config Source: https://skscope.readthedocs.io/en/latest/_modules/skscope/base_solver.html Retrieves the configuration parameters for the solver instance. ```APIDOC ## GET /get_config ### Description Returns the parameters configured for the current solver instance. ### Method GET ### Parameters #### Query Parameters - **deep** (boolean) - Optional - Whether to return parameters of sub-objects. ``` -------------------------------- ### Initialize and Run ScopeSolver Source: https://skscope.readthedocs.io/en/latest/_sources/feature/ComputationalTips.rst Configures the ScopeSolver with a specified sparsity level and executes the optimization. ```python solver = ScopeSolver(p, sparsity=3) params_skscope = solver.solve(ols_loss, jit=True) ``` -------------------------------- ### Initialize and execute data generation Source: https://skscope.readthedocs.io/en/latest/_sources/gallery/GeneralizedLinearModels/gamma-regression.ipynb Demonstrates usage of the data generation function with 500 observations and 5 active variables. ```python n = 500 p = 500 s = 5 x, y, coef_ = make_gamma_data(n=n, p=p, k=s) print("The predictor variables of the first five samples:",'\n',x[:,:5]) print("The first five noisy observations:", '\n', y[:5]) ``` -------------------------------- ### GET /get_config Source: https://skscope.readthedocs.io/en/latest/_modules/skscope/solver.html Retrieves the current configuration parameters for the Skscope solver. ```APIDOC ## GET /get_config ### Description Returns a dictionary containing the parameters for the current solver instance. ### Method GET ### Endpoint /get_config ### Parameters #### Query Parameters - **deep** (bool) - Optional - If True, returns parameters for this solver and contained subobjects that are estimators. Default is True. ### Response #### Success Response (200) - **params** (dict) - A dictionary of the solver's configuration parameters. #### Response Example { "dimensionality": 100, "sparsity": null, "thread": 1, "splicing_type": "halve" } ``` -------------------------------- ### Initialize Support Set and Parameters Source: https://skscope.readthedocs.io/en/latest/_modules/skscope/base_solver.html Validates the initial support set and sets up random parameter initialization if required. ```python if init_support_set is None: init_support_set = np.array([], dtype="int32") else: init_support_set = np.array(init_support_set, dtype="int32") if init_support_set.ndim > 1: raise ValueError( "The initial active set should be an 1D array of integers." ) if init_support_set.min() < 0 or init_support_set.max() >= p: raise ValueError("init_support_set contains wrong index.") # init_params if init_params is None: random_init = False if len(layers) > 0: random_init = np.any( np.array([layer.random_initilization for layer in layers]) ) if random_init: init_params = np.random.RandomState(self.random_state).randn(p) ``` -------------------------------- ### GET /api/v1/scope Source: https://skscope.readthedocs.io/en/latest/_sources/gallery/LinearModelAndVariants/isotonic-regression.ipynb Retrieves the scope data for the specified project identifier. ```APIDOC ## GET /api/v1/scope ### Description Retrieves the scope data for the specified project identifier. ### Method GET ### Endpoint /api/v1/scope ### Parameters #### Query Parameters - **project_id** (string) - Required - The unique identifier for the project. ### Request Example GET /api/v1/scope?project_id=skscope_readthedocs_io_en ### Response #### Success Response (200) - **data** (object) - The scope information for the project. #### Response Example { "project_id": "skscope_readthedocs_io_en", "status": "active", "data": {} } ``` -------------------------------- ### Initialize and Solve with ScopeSolver using Sparse Matrix Source: https://skscope.readthedocs.io/en/latest/feature/ComputationalTips.html Initialize ScopeSolver with the problem dimensions and sparsity level, then solve the OLS loss function using JIT compilation. ```python solver = ScopeSolver(p, sparsity=3) params_skscope = solver.solve(ols_loss, jit=True) ``` -------------------------------- ### GET /get_estimated_params Source: https://skscope.readthedocs.io/en/latest/_modules/skscope/base_solver.html Retrieves only the optimal parameters calculated by the objective function. ```APIDOC ## GET /get_estimated_params ### Description Returns the optimal solution parameters. ### Method GET ### Response #### Success Response (200) - **parameters** (array) - The optimal solution of shape (dimensionality,) #### Response Example { "parameters": [0.1, 0.0, 0.5] } ``` -------------------------------- ### Initialize HTPSolver Source: https://skscope.readthedocs.io/en/latest/_modules/skscope/solver.html Initializes the HTPSolver with various parameters controlling sparsity, optimization, and cross-validation. Ensure 'convex_solver_LBFGS' is available if not providing a custom solver. ```python def __init__( self, dimensionality, sparsity=None, sample_size=1, *, preselect=[], step_size=0.005, numeric_solver=convex_solver_LBFGS, max_iter=100, group=None, ic_method=None, cv=1, cv_fold_id=None, split_method=None, random_state=None, ): super().__init__( dimensionality=dimensionality, sparsity=sparsity, sample_size=sample_size, preselect=preselect, numeric_solver=numeric_solver, max_iter=max_iter, group=group, ic_method=ic_method, cv=cv, cv_fold_id=cv_fold_id, split_method=split_method, random_state=random_state, ) self.step_size = step_size ``` -------------------------------- ### PortfolioSelection Class Initialization Source: https://skscope.readthedocs.io/en/latest/_modules/skscope/skmodel.html Initializes a sparse portfolio selection model. Configure sparsity, objective function (MinVar/MeanVar), alpha for mean-variance, covariance matrix estimator, and random state. ```python class PortfolioSelection(BaseEstimator): r""" Construct a sparse portfolio using ``skscope`` with ``MinVar`` or ``MeanVar`` measure. Parameters ------------ sparsity : int, default=10 The number of stocks to be chosen, i.e., the sparsity level obj : {"MinVar", "MeanVar"}, default="MinVar" The objective of the portfolio optimization alpha: float, default=0 The penalty coefficient of the return cov_matrix : {"empirical", "lw"}, default="lw" Specify the estimator of covariance matrix. If ``empirical``, it will be the empirical estimator. If ``lw``, it will be the LedoitWolf estimator. random_state : {None, int, array_like[ints], SeedSequence, BitGenerator, Generator}, default=None The seed to initialize the parameter ``init_params`` in ``ScopeSolver`` """ def __init__( self, sparsity=1, obj="MinVar", alpha=0, cov_matrix="lw", random_state=None, ): self.sparsity = sparsity self.obj = obj self.alpha = alpha self.cov_matrix = cov_matrix self.random_state = random_state _parameter_constraints: dict = { "sparsity": [Interval(Integral, 1, None, closed="left")], "obj": [StrOptions({"MinVar", "MeanVar"})], "alpha": [Interval(Real, 0, None, closed="left")], "cov_matrix": [StrOptions({"empirical", "lw"})], "random_state": ["random_state"], } # def _more_tags(self): # return {'non_deterministic': True} ``` -------------------------------- ### Initialize ScopeSolver Source: https://skscope.readthedocs.io/en/latest/_modules/skscope/solver.html Instantiate the ScopeSolver with specified parameters. Ensure 'dimensionality' is provided. Other parameters like 'sparsity', 'sample_size', and 'numeric_solver' can be customized. ```python from sklearn.model_selection import KFold from .base_solver import BaseSolver from sklearn.base import BaseEstimator import numpy as np import jax from jax import numpy as jnp from . import _scope, utilities from .numeric_solver import convex_solver_LBFGS [docs] class ScopeSolver(BaseEstimator): r""" Get sparse optimal solution of convex objective function by Sparse-Constrained Optimization via Splicing Iteration (SCOPE) algorithm, which also can be used for variables selection. Specifically, ``ScopeSolver`` aims to tackle this problem: :math:`\min_{x \in R^p} f(x) \text{ s.t. } ||x||_0 \leq s`, where :math:`f(x)` is a convex objective function and :math:`s` is the sparsity level. Each element of :math:`x` can be seen as a variable, and the nonzero elements of :math:`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 :math:`p`. sparsity : int or array of int, optional The sparsity level, which is the number of nonzero elements of the optimal solution, denoted as :math:`s`. default is ``range(int(p/log(log(p))/log(p)))``. Used only when ``path_type`` is "seq". sample_size : int, default=1 Sample size, denoted as :math:`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. ``ScopeSolver`` will call this function to solve the convex optimization problem in each iteration. It should have the same interface as ``skscope.convex_solver_LBFGS``. max_iter : int, default=20 Maximum number of iterations taken for converging. ic_method : callable, optional A function to calculate the information criterion for choosing the sparsity level. ``ic(loss, p, s, n) -> ic_value``, where ``loss`` is the value of the objective function, ``p`` is the dimensionality, ``s`` is the sparsity level and ``n`` is the sample size. Used only when ``sparsity`` is array and ``cv`` is 1. Note that ``sample_size`` must be given when using ``ic_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. If ``cv`` > 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_data`` where ``index`` is 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. 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, and ``always_include=[0,3]`` means the 0-th and 3-th groups must be selected. important_search : int, default=128 The number of important variables which need be splicing. This is used to reduce the computational cost. If it's too large, it would greatly increase runtime. screening_size : int, default=-1 The number of variables remaining after the screening before variables select. Screening is used to reduce the computational cost. ``screening_size`` should be a non-negative number smaller than p, but larger than any value in sparsity. If ``screening_size`` is -1, screening will not be used. If ``screening_size`` is 0, ``screening_size`` will be set as ``int(p/log(log(p))/log(p))``. max_exchange_num : int, optional, default=5 Maximum exchange number when splicing. is_dynamic_max_exchange_num : bool, default=True If ``is_dynamic_max_exchange_num`` is True, ``max_exchange_num`` will be decreased dynamically according to the number of variables exchanged in the last iteration. greedy : bool, default=True, If ``greedy`` is True, the first exchange-number which can reduce the objective function value will be selected. """ pass ``` -------------------------------- ### Data Visualization with Matplotlib Source: https://skscope.readthedocs.io/en/latest/_sources/gallery/LinearModelAndVariants/isotonic-regression.ipynb Shows a simple scatter plot using matplotlib. Requires matplotlib to be installed. ```python import matplotlib.pyplot as plt plt.scatter(df['x'], df['y']) plt.xlabel('X-axis') plt.ylabel('Y-axis') plt.title('Scatter Plot') plt.show() ``` -------------------------------- ### PDASSolver Initialization Source: https://skscope.readthedocs.io/en/latest/autoapi/solver.html Initializes the PDASSolver to solve min β ∈ Rp l(β) s.t. ||β||0=k. ```APIDOC ## PDASSolver ### Description Solve the best subset selection problem with the subset size k by Primal-dual active set (PDAS) algorithm. ### Parameters - **dimensionality** (int) - Required - Dimension of the optimization problem (p). - **sparsity** (int or array of int) - Optional - The number of nonzero elements of the optimal solution (s). - **sample_size** (int) - Optional - Sample size (n). - **preselect** (array of int) - Optional - Indexes of variables which must be selected. - **step_size** (float) - Optional - Step size of gradient descent. - **numeric_solver** (callable) - Optional - A solver for the convex optimization problem. - **max_iter** (int) - Optional - Maximum number of iterations. - **group** (array) - Optional - The group index for each variable. - **cv** (int) - Optional - The folds number for cross-validation. - **cv_fold_id** (array) - Optional - Indicates different folds in CV. - **split_method** (callable) - Optional - Function to get data for each fold. - **random_state** (int) - Optional - Random seed for cross-validation. - **ic_method** (callable) - Optional - Function to calculate information criterion. ``` -------------------------------- ### Setup regression model with intercept Source: https://skscope.readthedocs.io/en/latest/_sources/gallery/LinearModelAndVariants/linear-regression.ipynb Generates synthetic data for a regression model including an intercept term. ```python n, p = 150, 30 rng = np.random.default_rng(0) X = rng.normal(0, 1, (n, p)) beta = np.zeros(p) beta[:3] = [1, 2, 3] y = X @ beta + rng.normal(1, 0.1, n) ``` -------------------------------- ### Import Libraries and Set Up Source: https://skscope.readthedocs.io/en/latest/_sources/gallery/Miscellaneous/compositional-data-correlation-inference.ipynb Imports necessary libraries including numpy, jax, matplotlib, seaborn, and skscope. It also sets a random seed for reproducibility and suppresses warnings. ```python import numpy as np np.random.seed(123) import jax.numpy as jnp import matplotlib.pyplot as plt import seaborn as sns from skscope import ScopeSolver import warnings warnings.filterwarnings('ignore') ``` -------------------------------- ### Initialize BaseSolver with various parameters Source: https://skscope.readthedocs.io/en/latest/_modules/skscope/solver.html Initializes the BaseSolver with parameters for dimensionality, sparsity, sample size, and advanced options like numeric solver, max iterations, group constraints, and cross-validation settings. ```python def __init__( self, dimensionality, sparsity=None, sample_size=1, *, threshold=0.0, strict_sparsity=True, preselect=[], numeric_solver=convex_solver_LBFGS, max_iter=100, group=None, ic_method=None, cv=1, cv_fold_id=None, split_method=None, random_state=None, ): super().__init__( dimensionality=dimensionality, sparsity=sparsity, sample_size=sample_size, threshold=threshold, strict_sparsity=strict_sparsity, preselect=preselect, numeric_solver=numeric_solver, max_iter=max_iter, group=group, ic_method=ic_method, cv=cv, cv_fold_id=cv_fold_id, split_method=split_method, random_state=random_state, ) self.use_gradient = True ``` -------------------------------- ### Use LinearSIC for Sparsity Selection Source: https://skscope.readthedocs.io/en/latest/feature/DataScienceTool.html Example demonstrating the use of LinearSIC to evaluate sparsity levels in a regression task. ```python import jax.numpy as jnp import numpy as np from sklearn.datasets import make_regression from skscope.utilities import LinearSIC n, p, k = 100, 10, 3 X, y = make_regression(n_samples=n, n_features=p, n_informative=k) solver = ScopeSolver( dimensionality=p, sparsity=[1, 2, 3, 4, 5] ## we want to select 1-5 variables sample_size=n, ## the number of samples ic_method=LinearSIC, ## use SIC to evaluate sparsity levels ) solver.solve( lambda params: jnp.sum((X @ params - y)**2), jit = True, ) print(solver.get_result()) ``` -------------------------------- ### HTML/JavaScript: Interactive Element Source: https://skscope.readthedocs.io/en/latest/_sources/gallery/FusionModels/depth-first-search-graph-trend-filtering.ipynb An example of an interactive HTML element with embedded JavaScript. This can be used for dynamic user interfaces. ```html
This is a paragraph.
```