### Install scikit-survival from Source (Development Version) Source: https://github.com/sebp/scikit-survival/blob/master/doc/install.rst Installs the latest development version of scikit-survival directly from its GitHub repository. Requires Git to be installed. ```shell pip install git+https://github.com/sebp/scikit-survival.git ``` -------------------------------- ### Setting up a Development Environment Source: https://github.com/sebp/scikit-survival/blob/master/doc/contributing.rst Instructions for setting up a local development environment using either conda or uv, including installing dependencies and the package in development mode. ```bash python ci/render-requirements.py ci/deps/requirements.yaml.tmpl > dev-environment.yaml conda env create -n sksurv --file dev-environment.yaml conda run -n sksurv pip install --group dev -e . ``` ```bash uv sync ``` -------------------------------- ### Install Pre-commit Hooks Source: https://github.com/sebp/scikit-survival/blob/master/doc/contributing.rst Installs pre-commit hooks to automatically check code style on every commit. ```bash pre-commit install ``` -------------------------------- ### Import Libraries and Setup Source: https://github.com/sebp/scikit-survival/blob/master/doc/user_guide/survival-svm.ipynb Imports necessary libraries from scikit-survival and scikit-learn, including plotting and data handling tools. Sets up display configuration for estimators and plotting style. ```python %matplotlib inline import matplotlib.pyplot as plt import numpy as np import pandas import seaborn as sns from sklearn import set_config from sklearn.model_selection import GridSearchCV, ShuffleSplit from sksurv.column import encode_categorical from sksurv.datasets import load_veterans_lung_cancer from sksurv.metrics import concordance_index_censored from sksurv.svm import FastSurvivalSVM set_config(display="text") # displays text representation of estimators sns.set_style("whitegrid") ``` -------------------------------- ### Install scikit-survival using Conda Source: https://github.com/sebp/scikit-survival/blob/master/doc/index.rst Provides instructions for installing the scikit-survival library using the conda package manager. This is the recommended installation method. ```Shell conda install -c conda-forge scikit-survival ``` -------------------------------- ### Rebuild Cython Code Source: https://github.com/sebp/scikit-survival/blob/master/doc/contributing.rst Instructions for rebuilding Cython code after making changes. This typically involves re-running the installation command, which is detailed in the setup-dev-environment section. ```shell # Re-run the install command from the setup-dev-environment section ``` -------------------------------- ### Install scikit-survival with Pip (Latest Release) Source: https://github.com/sebp/scikit-survival/blob/master/doc/install.rst Installs the latest stable release of scikit-survival using pip. This command assumes pip is installed and configured. ```shell pip install scikit-survival ``` -------------------------------- ### Install macOS Command Line Tools Source: https://github.com/sebp/scikit-survival/blob/master/doc/install.rst Installs the necessary command line developer tools on macOS, which are required for compiling extensions when installing from source. This command should be run in a terminal. ```shell xcode-select --install ``` -------------------------------- ### Install scikit-survival with Conda Source: https://github.com/sebp/scikit-survival/blob/master/README.rst Installs the scikit-survival library using the conda package manager from the conda-forge channel. This is the recommended installation method. ```bash conda install -c conda-forge scikit-survival ``` -------------------------------- ### Install scikit-survival with Conda Source: https://github.com/sebp/scikit-survival/blob/master/doc/install.rst Installs the scikit-survival package using Conda from the conda-forge channel. This is the recommended method if you use Conda. ```shell conda install -c conda-forge scikit-survival ``` -------------------------------- ### Install scikit-survival with Pip (No Binary) Source: https://github.com/sebp/scikit-survival/blob/master/doc/install.rst Installs the latest stable release of scikit-survival from source using pip, bypassing pre-built binary packages. This may be useful for development or specific build environments. ```shell pip install scikit-survival --no-binary scikit-survival ``` -------------------------------- ### Grid Search Setup Source: https://github.com/sebp/scikit-survival/blob/master/doc/user_guide/survival-svm.ipynb Sets up the grid search process, defining the parameter space to explore and the cross-validation strategy. It specifies the number of jobs for parallel processing and the number of repetitions for evaluation. ```python # The last part of the setup specifies the set of parameters we want to try and how many repetitions of training and testing we want to perform for each parameter setting. In the end, the parameters that on average performed best across all test sets (100 in this case) are selected. [GridSearchCV](https://scikit-learn.org/stable/modules/generated/sklearn.model_selection.GridSearchCV.html) can leverage multiple cores by evaluating multiple parameter settings concurrently (4 jobs in this example). # Example of GridSearchCV setup (parameters not fully specified in original text): # param_grid = { # "alpha": [0.1, 0.5, 1.0], # "r": [0.5, 1.0] # } # cv = ShuffleSplit(n_splits=100, test_size=0.2, random_state=0) # search = GridSearchCV(estimator, param_grid, cv=cv, scoring=score_survival_model, n_jobs=4) # search.fit(x, y) ``` -------------------------------- ### Import Libraries and Setup Source: https://github.com/sebp/scikit-survival/blob/master/doc/user_guide/random-survival-forest.ipynb Imports necessary libraries for data manipulation, machine learning, and survival analysis. Sets up plotting and estimator display configurations. ```python import matplotlib.pyplot as plt import numpy as np import pandas as pd %matplotlib inline from sklearn import set_config from sklearn.model_selection import train_test_split from sklearn.preprocessing import OrdinalEncoder from sksurv.datasets import load_gbsg2 from sksurv.ensemble import RandomSurvivalForest from sksurv.preprocessing import OneHotEncoder set_config(display="text") # displays text representation of estimators ``` -------------------------------- ### Setup GridSearchCV for Hyperparameter Tuning Source: https://github.com/sebp/scikit-survival/blob/master/doc/user_guide/survival-svm.ipynb Configures the parameter grid, cross-validation strategy, and the GridSearchCV object for hyperparameter optimization. It prepares the model for fitting by defining the search space and evaluation method. ```python param_grid = {"alpha": [2.0**v for v in range(-12, 13, 2)]} cv = ShuffleSplit(n_splits=100, test_size=0.5, random_state=0) gcv = GridSearchCV(estimator, param_grid, scoring=score_survival_model, n_jobs=1, refit=False, cv=cv) ``` -------------------------------- ### scikit-survival Setup and Imports Source: https://github.com/sebp/scikit-survival/blob/master/doc/user_guide/evaluating-survival-models.ipynb Imports necessary libraries from scikit-survival, matplotlib, numpy, and pandas for survival analysis tasks. Includes configuration for matplotlib display and scikit-learn estimator display. ```Python import matplotlib.pyplot as plt import numpy as np %matplotlib inline import pandas as pd from sklearn import set_config from sklearn.impute import SimpleImputer from sklearn.model_selection import train_test_split from sklearn.pipeline import make_pipeline from sksurv.datasets import load_flchain, load_gbsg2 from sksurv.functions import StepFunction from sksurv.linear_model import CoxnetSurvivalAnalysis, CoxPHSurvivalAnalysis from sksurv.metrics import ( concordance_index_censored, concordance_index_ipcw, cumulative_dynamic_auc, integrated_brier_score, ) from sksurv.nonparametric import kaplan_meier_estimator from sksurv.preprocessing import OneHotEncoder, encode_categorical from sksurv.util import Surv set_config(display="text") # displays text representation of estimators plt.rcParams["figure.figsize"] = [7.2, 4.8] ``` -------------------------------- ### Build Documentation with Tox Source: https://github.com/sebp/scikit-survival/blob/master/doc/contributing.rst Instructions to build the project's documentation using Tox. This command invokes Sphinx to generate HTML files. ```shell tox -e docs ``` -------------------------------- ### Open Generated Documentation Source: https://github.com/sebp/scikit-survival/blob/master/doc/contributing.rst Command to open the main page of the generated HTML documentation in the default web browser. ```shell xdg-open _build/html/index.html ``` -------------------------------- ### Run Test Suite Source: https://github.com/sebp/scikit-survival/blob/master/doc/contributing.rst Executes the project's unit tests using pytest. ```bash pytest ``` -------------------------------- ### Clone scikit-survival Repository Source: https://github.com/sebp/scikit-survival/blob/master/doc/contributing.rst Clones the scikit-survival repository from GitHub, including submodules, to your local disk. ```bash git clone --recurse-submodules git@github.com:YourLogin/scikit-survival.git cd scikit-survival ``` -------------------------------- ### Create Git Commit Source: https://github.com/sebp/scikit-survival/blob/master/doc/contributing.rst Creates a commit with a clear subject and optional detailed body to record changes. ```bash git commit ``` -------------------------------- ### Load Veterans Lung Cancer Data Source: https://github.com/sebp/scikit-survival/blob/master/doc/user_guide/00-introduction.ipynb Loads the Veterans Lung Cancer dataset from scikit-survival. This dataset is commonly used for survival analysis examples. It returns features (data_x) and survival data (data_y) structured for survival analysis. ```python from sksurv.datasets import load_veterans_lung_cancer data_x, data_y = load_veterans_lung_cancer() data_y ``` -------------------------------- ### Exception for Incomparable Data Source: https://github.com/sebp/scikit-survival/blob/master/doc/release_notes/v0.13.rst This entry details that estimators in `sksurv.svm` and related metric functions will now raise an exception when attempting to fit or calculate metrics on data with incomparable pairs. This prevents silent errors and guides users to provide comparable data. ```python sksurv.svm.FastSurvivalSVM.fit(X, y) # Raises error on incomparable data ``` ```python sksurv.metrics.concordance_index_censored(y_true, y_pred) # Raises error on incomparable data ``` -------------------------------- ### Initialize and configure FastKernelSurvivalSVM and GridSearchCV Source: https://github.com/sebp/scikit-survival/blob/master/doc/user_guide/survival-svm.ipynb Initializes the FastKernelSurvivalSVM model with specific parameters like optimizer and kernel type ('precomputed'). It then sets up GridSearchCV for hyper-parameter tuning, specifying the model, parameter grid, scoring metric, and cross-validation strategy. ```python kssvm = FastKernelSurvivalSVM(optimizer="rbtree", kernel="precomputed", random_state=0) kgcv = GridSearchCV(kssvm, param_grid, scoring=score_survival_model, n_jobs=1, refit=False, cv=cv) ``` -------------------------------- ### Add Files to Git Staging Source: https://github.com/sebp/scikit-survival/blob/master/doc/contributing.rst Stages modified or new files for the next commit. ```bash git add path/to/modified_file ``` -------------------------------- ### Push Changes to Origin Source: https://github.com/sebp/scikit-survival/blob/master/doc/contributing.rst Pushes local commits to the remote 'origin' repository, setting upstream tracking. ```bash git push -u origin my_feature ``` -------------------------------- ### Lint Code with Tox Source: https://github.com/sebp/scikit-survival/blob/master/doc/contributing.rst Checks code conformity to PEP8 standards using the 'lint' tox environment. ```bash tox -e lint ``` -------------------------------- ### Initialize FastSurvivalSVM Estimator Source: https://github.com/sebp/scikit-survival/blob/master/doc/user_guide/survival-svm.ipynb Creates an instance of the FastSurvivalSVM class with specified parameters for maximum iterations and tolerance, and sets a random state for reproducibility. This estimator will be used in the grid search. ```python estimator = FastSurvivalSVM(max_iter=1000, tol=1e-5, random_state=0) ``` -------------------------------- ### Train Random Survival Forest Model Source: https://github.com/sebp/scikit-survival/blob/master/doc/user_guide/random-survival-forest.ipynb Initializes and trains a RandomSurvivalForest model with specified parameters (1000 estimators, min_samples_split, min_samples_leaf) using the training data. `n_jobs=-1` utilizes all available CPU cores. ```python rsf = RandomSurvivalForest( n_estimators=1000, min_samples_split=10, min_samples_leaf=15, n_jobs=-1, random_state=random_state ) rsf.fit(X_train, y_train) ``` -------------------------------- ### Create Feature Branch Source: https://github.com/sebp/scikit-survival/blob/master/doc/contributing.rst Creates a new Git branch for a specific feature or bug fix, ensuring changes are self-contained. ```bash git checkout -b my-new-feature ``` -------------------------------- ### Refit Estimator with Best Parameters Source: https://github.com/sebp/scikit-survival/blob/master/doc/user_guide/survival-svm.ipynb Sets the best found hyper-parameters on the original estimator and refits the model. This ensures the final model uses the optimal configuration identified during the search. ```python estimator.set_params(**gcv.best_params_) estimator.fit(x, y) ``` -------------------------------- ### Fit AFT Model with Gradient Boosting Source: https://github.com/sebp/scikit-survival/blob/master/doc/user_guide/boosting.ipynb Demonstrates fitting an Accelerated Failure Time (AFT) model using scikit-survival's ComponentwiseGradientBoostingSurvivalAnalysis with the 'ipcwls' loss function. It shows how to initialize the estimator, fit it to training data, and score it on test data. ```python est_aft_ls = ComponentwiseGradientBoostingSurvivalAnalysis( loss="ipcwls", n_estimators=300, learning_rate=1.0, random_state=0 ).fit(X_train, y_train) cindex = est_aft_ls.score(X_test, y_test) print(round(cindex, 3)) ``` -------------------------------- ### Run Simulation with 100 Samples Source: https://github.com/sebp/scikit-survival/blob/master/doc/user_guide/evaluating-survival-models.ipynb Generates a synthetic dataset with 100 samples and a hazard ratio of 2.0. It then estimates the concordance index and plots the simulation results, focusing on the difference between actual and estimated values, particularly concerning censoring. ```python hazard_ratio = 2.0 ylim = [-0.035, 0.035] mean_1, std_1 = simulation(100, hazard_ratio) plot_results(mean_1, std_1, ylim=ylim) ``` -------------------------------- ### Load and Split FLchain Data Source: https://github.com/sebp/scikit-survival/blob/master/doc/user_guide/evaluating-survival-models.ipynb Loads the FLchain dataset and splits it into training and testing sets for model evaluation. The split is configured with a test size of 0.2 and a fixed random state for reproducibility. ```Python x, y = load_flchain() x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.2, random_state=0) ``` -------------------------------- ### Pipeline for Feature Selection and Modeling Source: https://github.com/sebp/scikit-survival/blob/master/doc/user_guide/00-introduction.ipynb Creates a scikit-learn pipeline that first encodes categorical features using OneHotEncoder, then selects the top k features using SelectKBest with a custom scoring function, and finally fits a CoxPHSurvivalAnalysis model. ```python from sklearn.feature_selection import SelectKBest from sklearn.pipeline import Pipeline from sklearn.preprocessing import OneHotEncoder from sksurv.linear_model import CoxPHSurvivalAnalysis # Assuming fit_and_score_features is defined and data_x_numeric is available # pipe = Pipeline( # [ # ("encode", OneHotEncoder()), # ("select", SelectKBest(fit_and_score_features, k=3)), # ("model", CoxPHSurvivalAnalysis()), # ] # ) ``` -------------------------------- ### Loading and Splitting Veterans' Lung Cancer Data Source: https://github.com/sebp/scikit-survival/blob/master/doc/user_guide/evaluating-survival-models.ipynb Loads the Veterans' Administration Lung Cancer Trial dataset and splits it into training and testing sets, ensuring stratification by survival status to maintain data distribution. ```python from sksurv.datasets import load_veterans_lung_cancer from sklearn.model_selection import train_test_split va_x, va_y = load_veterans_lung_cancer() va_x_train, va_x_test, va_y_train, va_y_test = train_test_split( va_x, va_y, test_size=0.2, stratify=va_y["Status"], random_state=0 ) ``` -------------------------------- ### scikit-survival API Modules Overview Source: https://github.com/sebp/scikit-survival/blob/master/doc/api/index.rst Provides an organized structure to access all public objects, functions, and methods within the scikit-survival library. It serves as a central index to the library's capabilities, categorized by functional areas. ```APIDOC scikit-survival API Reference Structure: This document outlines the primary modules available in the scikit-survival library, facilitating navigation to specific functionalities. Modules: - datasets: Contains various datasets for testing and examples. - ensemble: Implements ensemble methods for survival analysis. - functions: Provides core survival analysis functions. - compare: Tools for comparing survival models. - io: Input/output utilities for survival data and models. - kernels: Defines and manages kernels for kernel-based methods. - linear_model: Implements linear models for survival analysis. - meta: Meta-estimators and utilities. - metrics: Evaluation metrics for survival models. - nonparametric: Nonparametric survival analysis methods. - preprocessing: Data preprocessing steps for survival analysis. - svm: Support Vector Machines for survival analysis. - tree: Tree-based models for survival analysis. - util: Utility functions and helpers. ``` -------------------------------- ### Import necessary libraries for survival analysis Source: https://github.com/sebp/scikit-survival/blob/master/doc/user_guide/boosting.ipynb Imports essential libraries including matplotlib for plotting, numpy for numerical operations, pandas for data manipulation, scikit-learn for model selection, and specific modules from scikit-survival for datasets, ensemble methods, and preprocessing. ```python import matplotlib.pyplot as plt import numpy as np import pandas as pd from sklearn.model_selection import train_test_split %matplotlib inline from sksurv.datasets import load_breast_cancer from sksurv.ensemble import ComponentwiseGradientBoostingSurvivalAnalysis, GradientBoostingSurvivalAnalysis from sksurv.preprocessing import OneHotEncoder ``` -------------------------------- ### Fit GridSearchCV and Handle Warnings Source: https://github.com/sebp/scikit-survival/blob/master/doc/user_guide/survival-svm.ipynb Executes the hyper-parameter search using the configured GridSearchCV object. It also suppresses UserWarnings during the fitting process to keep the output clean. ```python import warnings warnings.filterwarnings("ignore", category=UserWarning) gcv = gcv.fit(x, y) ``` -------------------------------- ### Run Simulation with 1000 Samples Source: https://github.com/sebp/scikit-survival/blob/master/doc/user_guide/evaluating-survival-models.ipynb Increases the dataset size to 1000 samples to repeat the simulation with the same hazard ratio. This allows for a clearer observation of how Harrell's c and Uno's c estimators behave with more data and varying censoring levels. ```python mean_2, std_2 = simulation(1000, hazard_ratio) plot_results(mean_2, std_2, ylim=ylim) ``` -------------------------------- ### Split Data for Training and Testing Source: https://github.com/sebp/scikit-survival/blob/master/doc/user_guide/random-survival-forest.ipynb Splits the preprocessed dataset into training (75%) and testing (25%) sets to evaluate the model's generalization performance. A fixed random state ensures reproducibility. ```python random_state = 20 X_train, X_test, y_train, y_test = train_test_split(Xt, y, test_size=0.25, random_state=random_state) ``` -------------------------------- ### Add SurvivalTree.apply and decision_path Source: https://github.com/sebp/scikit-survival/blob/master/doc/release_notes/v0.19.rst This enhancement introduces the `apply` and `decision_path` methods to the `sksurv.tree.SurvivalTree` class. These methods are useful for inspecting the structure and predictions of a trained survival tree. ```APIDOC sksurv.tree.SurvivalTree: apply(X, return_leaf=False) Applies the trained survival tree to new data X. Parameters: X: Input data, array-like of shape (n_samples, n_features). return_leaf: If True, returns the leaf index; otherwise, returns the prediction. Returns: Array of predictions or leaf indices. decision_path(X) Returns the decision path for each sample in X. Parameters: X: Input data, array-like of shape (n_samples, n_features). Returns: A sparse matrix representing the decision path. ``` -------------------------------- ### Grid Search for Optimal Feature Selection (k) Source: https://github.com/sebp/scikit-survival/blob/master/doc/user_guide/00-introduction.ipynb Performs a grid search to find the optimal number of features (k) for the SelectKBest transformer within a pipeline. It uses KFold cross-validation to evaluate different values of k and identifies the best performing configuration. ```python from sklearn.model_selection import GridSearchCV, KFold import numpy as np import pandas as pd # Assuming 'pipe' is the defined Pipeline and 'data_x', 'data_y' are available # param_grid = {"select__k": np.arange(1, data_x_numeric.shape[1] + 1)} # cv = KFold(n_splits=3, random_state=1, shuffle=True) # gcv = GridSearchCV(pipe, param_grid, return_train_score=True, cv=cv) # gcv.fit(data_x, data_y) # results = pd.DataFrame(gcv.cv_results_).sort_values(by="mean_test_score", ascending=False) # results.loc[:, ~results.columns.str.endswith("_time")] ``` -------------------------------- ### Compare Regularization Strategies Source: https://github.com/sebp/scikit-survival/blob/master/doc/user_guide/boosting.ipynb Demonstrates how to compare different regularization strategies (learning rate, dropout, subsampling) against no regularization in gradient boosting survival analysis. It fits models with varying numbers of estimators and records their concordance index scores. ```python n_estimators = [i * 5 for i in range(1, 21)] estimators = { "no regularization": GradientBoostingSurvivalAnalysis(learning_rate=1.0, max_depth=1, random_state=0), "learning rate": GradientBoostingSurvivalAnalysis(learning_rate=0.1, max_depth=1, random_state=0), "dropout": GradientBoostingSurvivalAnalysis(learning_rate=1.0, dropout_rate=0.1, max_depth=1, random_state=0), "subsample": GradientBoostingSurvivalAnalysis(learning_rate=1.0, subsample=0.5, max_depth=1, random_state=0), } scores_reg = {k: [] for k in estimators.keys()} for n in n_estimators: for name, est in estimators.items(): est.set_params(n_estimators=n) est.fit(X_train, y_train) cindex = est.score(X_test, y_test) scores_reg[name].append(cindex) scores_reg = pd.DataFrame(scores_reg, index=n_estimators) ``` -------------------------------- ### sksurv.svm.FastSurvivalSVM Optimizer Warning Source: https://github.com/sebp/scikit-survival/blob/master/doc/release_notes/v0.13.rst This fix resolves a PendingDeprecationWarning related to the use of matrices when fitting `sksurv.svm.FastSurvivalSVM` with specific optimizers (`PRSVM` or `simple`). It ensures the internal matrix handling is compatible with current best practices. ```python sksurv.svm.FastSurvivalSVM(optimizer='PRSVM' or 'simple') ``` -------------------------------- ### Show Versions Functionality Source: https://github.com/sebp/scikit-survival/blob/master/doc/release_notes/v0.24.rst Fixes the printing of the Python version within the `sksurv.show_versions` function. This ensures accurate reporting of the environment details for debugging and reproducibility. ```python from sksurv import show_versions # Calling this function will now correctly display the Python version. # show_versions() ``` -------------------------------- ### Solver Changes for MinlipSurvivalAnalysis and HingeLossSurvivalSVM Source: https://github.com/sebp/scikit-survival/blob/master/doc/release_notes/v0.15.rst Support for cvxpy and cvxopt solvers has been removed from MinlipSurvivalAnalysis and HingeLossSurvivalSVM. The default solver is now ECOS, which was previously used internally by cvxpy, ensuring similar results. ```APIDOC sksurv.svm.MinlipSurvivalAnalysis(..., solver='ecos') - Default solver changed from cvxpy/cvxopt to ECOS. sksurv.svm.HingeLossSurvivalSVM(..., solver='ecos') - Default solver changed from cvxpy/cvxopt to ECOS. ```