LassoNet

repository·master·Indexed 42 days ago

https://github.com/lasso-net/lassonet

A family of neural network models that incorporate feature selection via a linear skip connection with an L1 penalty. It supports regression, classification, and survival analysis (Cox and interval-censored) while identifying a sparse subset of important features. The library follows scikit-learn standards and provides specialized classes such as LassoNetRegressor, LassoNetClassifier, and LassoNetCoxRegressor, along with cross-validated versions (CV) and stability selection for feature importance.

Tokens
1.7K
Snippets
7
Records
11
Agent score
54%

What's inside LassoNet

  1. Available LassoNet model types

    master

    LassoNet provides specialized models for different tasks:

    • Regression: LassoNetRegressor
    • Classification: LassoNetClassifier
    • Cox regression: LassoNetCoxRegressor
    • Interval-censored Cox regression: LassoNetIntervalRegressor

    For cross-validation, use the versions ending in CV (e.g., LassoNetRegressorCV).

  2. Basic usage of LassoNet models

    master

    LassoNet follows scikit-learn standards. You can use LassoNetClassifierCV or LassoNetRegressorCV for cross-validated training. Always normalize your data before passing it to the model, as it uses neural networks under the hood.

    from lassonet import LassoNetClassifierCV 
    # or LassoNetRegressorCV
    
    model = LassoNetClassifierCV() 
    path = model.fit(X_train, y_train)
    print("Best model scored", model.score(X_test, y_test))
    print("Lambda =", model.best_lambda_)
  3. How to train a dense model on selected features

    master

    After finding selected features via the regularization path, you can train a dense model (without the L1 skip connection) specifically on those features by setting dense_only=True in the .fit() method.

    # Assuming 'selected' is a boolean mask or index array from a path checkpoint
    model.fit(X_train[:, selected], y_train, dense_only=True)
    print(model.score(X_test[:, selected], y_test))
  4. Reproduce results from Table 1 of the LassoNet paper

    master

    To reproduce the results presented in Table 1 of the LassoNet paper, you must download the datasets, configure the data loading script, and execute the reproduction script.

    1. Obtain Datasets

    Datasets can be obtained via:

    2. Configure Data Loading

    Use data-utils.py to load the 6 datasets. You must:

    1. Download and unzip the files.
    2. Modify the /home/lemisma/datasets path inside data-utils.py to point to your local dataset directory.

    3. Run the Reproduction Script

    Execute run.py to run the experiments. This script allows you to specify the dataset and other parameters via command line arguments.

    python run.py
  5. How to use the cross-validation interface

    master

    Models ending in CV (e.g., LassoNetRegressorCV) perform 5-fold cross-validation by default. For each fold, a path is trained, and the best regularization value is chosen to maximize the average score. The model is then retrained on the entire training set.

    Key attributes for the best model:

    • best_lambda_: The optimal regularization parameter.
    • best_selected_: The optimal subset of features.
    • best_cv_score_: The best cross-validation score.

    You can also use model.plot_cv() to inspect the cross-validation results.

    model = LassoNetRegressorCV()
    model.fit(X_train, y_train)
    model.score(X_test, y_test)
    # Use model.plot_cv() for more info
  6. How to use the regularization path interface

    master

    The base models allow you to train along a regularization path using the .path() method. This returns a list of checkpoints. You can visualize the path with plot_path() and then load a specific model from the path using .load() with a state dictionary.

    from lassonet import LassoNetRegressor, plot_path
    
    model = LassoNetRegressor()
    # Returns a list of checkpoints
    path = model.path(X_train, y_train, return_state_dicts=True)
    
    # Visualize the path
    plot_path(model, X_test, y_test)
    
    # Choose best_id based on the plot and load it
    model.load(path[best_id].state_dict)
    print(model.score(X_test, y_test))
  7. How to use stability selection for feature importance

    master

    Stability selection is the recommended way to identify the most important features by running the model multiple times on different random subsamples. Use model.stability_selection() to perform this.

    model = LassoNetRegressor()
    # Returns several diagnostic objects
    oracle, order, wrong, paths, prob = model.stability_selection(X_train, y_train)
    
    # 'order' sorts features by decreasing importance
    # 'prob' is the probability a feature is selected at each lambda value
  8. Available LassoNet API classes

    master

    LassoNet provides several model classes for different machine learning tasks, including cross-validated versions for automated hyperparameter tuning:

    Regression

    • lassonet.LassoNetRegressor: Standard LassoNet regressor.
    • lassonet.LassoNetRegressorCV: LassoNet regressor with built-in cross-validation.

    Classification

    • lassonet.LassoNetClassifier: Standard LassoNet classifier.
    • lassonet.LassoNetClassifierCV: LassoNet classifier with built-in cross-validation.

    Survival Analysis (Cox)

    • lassonet.LassoNetCoxRegressor: LassoNet regressor for Cox proportional hazards models.
    • lassonet.LassoNetCoxRegressorCV: LassoNet Cox regressor with built-in cross-validation.
  9. Reference: LassoNet model parameters

    master

    When initializing LassoNet models, the following parameters are available:

    • hidden_dims: Number of neurons in each hidden layer (e.g., (100,) or (10, 10)).
    • path_multiplier: Number of lambda values to compute on the path. Lower values increase precision but take more time (e.g., 1.01).
    • lambda_start: Starting value for regularization. Defaults to `