### Install GCC on MacOS using Homebrew Source: https://www.pysurvival.io/installation.html Use this command if you have never installed GCC before on MacOS. ```bash brew install gcc ``` -------------------------------- ### Kaplan Meier Model Example Source: https://www.pysurvival.io/models/kaplan_meier.html Demonstrates how to initialize, fit, and visualize a Kaplan Meier survival model using sample data. Ensure matplotlib is installed for display. ```python # Importing modules import numpy as np from matplotlib import pyplot as plt from pysurvival.models.non_parametric import KaplanMeierModel from pysurvival.utils.display import display_non_parametric # %matplotlib inline #Uncomment when using Jupyter # Generating random times and event indicators T = np.round(np.abs(np.random.normal(10, 10, 1000)), 1) E = np.random.binomial(1, 0.3, 1000) # Initializing the KaplanMeierModel km_model = KaplanMeierModel() # Fitting the model km_model.fit(T, E, alpha=0.95) # Displaying the survival function and confidence intervals display_non_parametric(km_model) ``` -------------------------------- ### Example Usage of SmoothKaplanMeierModel Source: https://www.pysurvival.io/models/smooth_kaplan_meier.html Demonstrates initializing, fitting, and displaying the survival function of the SmoothKaplanMeierModel with sample data. ```python # Importing modules import numpy as np from matplotlib import pyplot as plt from pysurvival.models.non_parametric import SmoothKaplanMeierModel from pysurvival.utils.display import display_non_parametric # %matplotlib inline #Uncomment when using Jupyter # Generating random times and event indicators T = np.round(np.abs(np.random.normal(10, 10, 1000)), 1) E = np.random.binomial(1, 0.3, 1000) # Initializing the SmoothKaplanMeierModel skm_model = SmoothKaplanMeierModel(bandwidth=1., kernel='Cosine') # Fitting the model and display the survival function and confidence intervals skm_model.fit(T, E, alpha=0.95) # Displaying the survival function and confidence intervals display_non_parametric(skm_model) ``` -------------------------------- ### Importing necessary libraries Source: https://www.pysurvival.io/models/extra_survival_trees.html Imports all required libraries for data simulation, model building, and evaluation. Ensure these are installed before running. ```python import numpy as np import pandas as pd from matplotlib import pyplot as plt from sklearn.model_selection import train_test_split from pysurvival.models.simulations import SimulationModel from pysurvival.models.survival_forest import ExtraSurvivalTreesModel from pysurvival.utils.metrics import concordance_index from pysurvival.utils.display import integrated_brier_score %pylab inline ``` -------------------------------- ### Install PySurvival via pip Source: https://www.pysurvival.io/installation.html The recommended method for installing PySurvival is using pip. This command will download and install the latest version from PyPI. ```bash pip install pysurvival ``` -------------------------------- ### Build and Evaluate Survival Models with PySurvival Source: https://www.pysurvival.io/index.html This example demonstrates loading data, building CoxPH and LinearMultiTask models, fitting them to training data, and evaluating their performance using concordance index on test data. Ensure necessary modules are imported. ```python # Loading the modules from pysurvival.models.semi_parametric import CoxPHModel from pysurvival.models.multi_task import LinearMultiTaskModel from pysurvival.datasets import Dataset from pysurvival.utils.metrics import concordance_index # Loading and splitting a simple example into train/test sets X_train, T_train, E_train, X_test, T_test, E_test = \ Dataset('simple_example').load_train_test() # Building a CoxPH model coxph_model = CoxPHModel() coxph_model.fit(X=X_train, T=T_train, E=E_train, init_method='he_uniform', l2_reg = 1e-4, lr = .4, tol = 1e-4) # Building a MTLR model mtlr = LinearMultiTaskModel() mtlr.fit(X=X_train, T=T_train, E=E_train, init_method = 'glorot_uniform', optimizer ='adam', lr = 8e-4) # Checking the model performance c_index1 = concordance_index(model=coxph_model, X=X_test, T=T_test, E=E_test ) print("CoxPH model c-index = {:.2f}".format(c_index1)) c_index2 = concordance_index(model=mtlr, X=X_test, T=T_test, E=E_test ) print("MTLR model c-index = {:.2f}".format(c_index2)) ``` -------------------------------- ### List GCC files on MacOS Source: https://www.pysurvival.io/installation.html This command helps in identifying the GCC installation path on MacOS. ```bash brew ls gcc ``` -------------------------------- ### Install GCC on Ubuntu Source: https://www.pysurvival.io/installation.html Install GCC version 8 and its C++ compiler on Ubuntu. The version number can be changed to match your system's requirements. ```bash sudo apt install gcc-8 g++-8 ``` -------------------------------- ### Import Libraries for Survival Analysis Example Source: https://www.pysurvival.io/models/random_survival_forest.html Imports necessary libraries for data manipulation, modeling, and visualization in survival analysis. Includes pysurvival specific modules for simulations and models. ```python import numpy as np import pandas as pd from matplotlib import pyplot as plt from sklearn.model_selection import train_test_split from pysurvival.models.simulations import SimulationModel from pysurvival.models.survival_forest import RandomSurvivalForestModel from pysurvival.utils.metrics import concordance_index from pysurvival.utils.display import integrated_brier_score %pylab inline ``` -------------------------------- ### Import Necessary Libraries Source: https://www.pysurvival.io/models/coxph.html Imports essential libraries for data manipulation, modeling, and visualization. Ensure these are installed before running the code. ```python import numpy as np import pandas as pd from matplotlib import pyplot as plt from sklearn.model_selection import train_test_split from pysurvival.models.simulations import SimulationModel from pysurvival.models.semi_parametric import CoxPHModel from pysurvival.utils.metrics import concordance_index from pysurvival.utils.display import integrated_brier_score ``` -------------------------------- ### Import necessary libraries for Kernel SVM example Source: https://www.pysurvival.io/models/kernel_svm.html Import required libraries including numpy, pandas, matplotlib, PySurvival models, simulation utilities, and scikit-learn for data splitting. ```python import numpy as np import pandas as pd from matplotlib import pyplot as plt from pysurvival.models.svm import KernelSVMModel from pysurvival.models.simulations import SimulationModel from pysurvival.utils.metrics import concordance_index from sklearn.model_selection import train_test_split from scipy.stats.stats import pearsonr # %pylab inline # to use in jupyter notebooks ``` -------------------------------- ### Import necessary packages for Linear MTLR example Source: https://www.pysurvival.io/models/linear_mtlr.html Imports required libraries for data manipulation, modeling, and evaluation, including numpy, pandas, matplotlib, scikit-learn, and pysurvival components. ```python import numpy as np import pandas as pd from matplotlib import pyplot as plt from sklearn.model_selection import train_test_split from pysurvival.models.simulations import SimulationModel from pysurvival.models.multi_task import LinearMultiTaskModel from pysurvival.utils.metrics import concordance_index from pysurvival.utils.display import integrated_brier_score #%matplotlib inline # To use with Jupyter notebooks ``` -------------------------------- ### Upgrade GCC on MacOS using Homebrew Source: https://www.pysurvival.io/installation.html Use this command if you have already installed GCC and want to update to the latest version on MacOS. ```bash brew upgrade gcc ``` -------------------------------- ### Importing Packages for Parametric Models Source: https://www.pysurvival.io/models/parametric.html Imports necessary libraries for using pysurvival's parametric models, including data manipulation, modeling, and plotting tools. Ensure all listed packages are installed. ```python import numpy as np import pandas as pd from matplotlib import pyplot as plt from sklearn.model_selection import train_test_split from pysurvival.models.simulations import SimulationModel from pysurvival.models.parametric import GompertzModel from pysurvival.utils.metrics import concordance_index from pysurvival.utils.display import integrated_brier_score %pylab inline ``` -------------------------------- ### Set C++ and C environment variables on Ubuntu Source: https://www.pysurvival.io/installation.html Assign the paths for the C++ and C compilers to the CC and CXX environment variables on Ubuntu. Ensure the version number matches your GCC installation. ```bash export CXX=/usr/bin/g++-8 export CC=/usr/bin/gcc-8 ``` -------------------------------- ### Importing necessary libraries for PySurvival and data simulation Source: https://www.pysurvival.io/models/linear_svm.html Imports required packages including numpy, pandas, matplotlib, PySurvival models and utilities, scikit-learn for data splitting, and scipy for statistical functions. This setup is essential for data generation and model training. ```python import numpy as np import pandas as pd from matplotlib import pyplot as plt from pysurvival.models.svm import LinearSVMModel from pysurvival.models.simulations import SimulationModel from pysurvival.utils.metrics import concordance_index from sklearn.model_selection import train_test_split from scipy.stats.stats import pearsonr # %pylab inline # to use in jupyter notebooks ``` -------------------------------- ### LinearSVMModel Instance and Fit Method Source: https://www.pysurvival.io/models/linear_svm.html Demonstrates how to create an instance of the LinearSVMModel and fit it to training data. The `fit` method trains the model using input samples, time-to-event data, and event indicators. ```APIDOC ## LinearSVMModel ### Description Creates an instance of the Linear SVM model. ### Method Signature `LinearSVMModel()` ## fit ### Description Fit the estimator based on the given parameters. ### Method Signature `fit(X, T, E, with_bias = True, init_method='glorot_normal', lr = 1e-2, max_iter = 100, l2_reg = 1e-4, tol = 1e-3, verbose = True)` ### Parameters #### Path Parameters - **X** (array-like) - Input samples; where the rows correspond to an individual sample and the columns represent the features (shape=[n_samples, n_features]). - **T** (array-like) - Target values describing the time when the event of interest or censoring occurred. - **E** (array-like) - Values that indicate if the event of interest occurred (E[i]=1 corresponds to an event, and E[i] = 0 means censoring, for all i). - **with_bias** (bool) - Optional - Whether a bias should be added (default=True). - **init_method** (str) - Optional - Initialization method to use. Options include 'glorot_uniform', 'he_uniform', 'uniform', 'glorot_normal', 'he_normal', 'normal', 'ones', 'zeros', 'orthogonal' (default = 'glorot_normal'). - **lr** (float) - Optional - Learning rate used in the optimization (default=1e-2). - **max_iter** (int) - Optional - Maximum number of iterations in the Newton optimization (default=100). - **l2_reg** (float) - Optional - L2 regularization parameter for the model coefficients (default=1e-4). - **tol** (float) - Optional - Tolerance for stopping criteria (default=1e-3). - **verbose** (bool) - Optional - Whether or not producing detailed logging about the modeling (default=True). ### Returns - self : object ``` -------------------------------- ### Install GCC on CentOS 7 Source: https://www.pysurvival.io/installation.html Install GCC and its C++ compiler on CentOS 7 using yum. The version number in 'devtoolset-8' can be adjusted based on the latest available GCC version. ```bash sudo yum install centos-release-scl sudo yum install devtoolset-8-gcc devtoolset-8-gcc-c++ -y ``` -------------------------------- ### Initialize SmoothKaplanMeierModel Source: https://www.pysurvival.io/models/smooth_kaplan_meier.html Instantiate the SmoothKaplanMeierModel with specified bandwidth and kernel type. Bandwidth controls smoothing degree, while kernel defines the smoothing function. ```python SmoothKaplanMeierModel(bandwidth=0.1, kernel='normal') ``` -------------------------------- ### Import Libraries and Load Dataset Source: https://www.pysurvival.io/tutorials/credit_risk.html Imports necessary libraries and loads the German Credit dataset. Ensures the dataset is ready for analysis. ```python # Importing modules import pandas as pd import numpy as np from matplotlib import pyplot as plt from pysurvival.datasets import Dataset %pylab inline # Reading the dataset raw_dataset = Dataset('credit_risk').load() print("The raw_dataset has the following shape: {}.".format(raw_dataset.shape)) raw_dataset.head(3) ``` -------------------------------- ### Importing necessary packages Source: https://www.pysurvival.io/models/nonlinear_coxph.html Imports all required libraries for data simulation, modeling, and evaluation. ```python import numpy as np import pandas as pd from matplotlib import pyplot as plt from sklearn.model_selection import train_test_split from pysurvival.models.simulations import SimulationModel from pysurvival.models.semi_parametric import NonLinearCoxPHModel from pysurvival.utils.metrics import concordance_index from pysurvival.utils.display import integrated_brier_score #%pylab inline ``` -------------------------------- ### KernelSVMModel Initialization Source: https://www.pysurvival.io/models/kernel_svm.html Initializes a KernelSVMModel instance with specified kernel and scaling parameters. ```APIDOC ## KernelSVMModel ### Description Initializes a KernelSVMModel instance. ### Method `__init__` ### Parameters - **kernel** (str) - Optional - The type of kernel used to fit the model. Available kernels: Polynomial, Gaussian, Exponential, Tanh, Sigmoid, Rational Quadratic, Inverse Multiquadratic, Multiquadratic. (default="gaussian") - **scale** (float) - Optional - Scale parameter of the kernel function. (default=1) - **offset** (float) - Optional - Offset parameter of the kernel function. (default=0) - **degree** (float) - Optional - Degree parameter of the polynomial/kernel function. (default=1) - **auto_scaler** (bool) - Optional - Whether to automatically scale the features. (default=True) ``` -------------------------------- ### __init__ Source: https://www.pysurvival.io/models/parametric.html Initializes the Parametric model. Allows setting the number of time bins and whether to automatically scale features. ```APIDOC ## __init__ ### Description Initializes the Parametric model with specified bins and auto-scaler settings. ### Parameters - **bins** (int) - Optional - Number of subdivisions of the time axis. Defaults to 100. - **auto_scaler** (boolean) - Optional - Determines whether a sklearn scaler should be automatically applied. Defaults to True. ``` -------------------------------- ### Importing necessary packages Source: https://www.pysurvival.io/models/neural_mtlr.html Imports all required libraries for data simulation, model building, and evaluation. ```python import numpy as np import pandas as pd from matplotlib import pyplot as plt from sklearn.model_selection import train_test_split from pysurvival.models.simulations import SimulationModel from pysurvival.models.multi_task import NeuralMultiTaskModel from pysurvival.utils.metrics import concordance_index from pysurvival.utils.display import integrated_brier_score %pylab inline ``` -------------------------------- ### Initialize LinearMultiTaskModel Source: https://www.pysurvival.io/models/linear_mtlr.html Initializes the LinearMultiTaskModel with specified number of bins and auto-scaler option. Use this to set up the model before fitting. ```python LinearMultiTaskModel(bins=100, auto_scaler=True) ``` -------------------------------- ### Initialize SimulationModel Source: https://www.pysurvival.io/models/simulations.html Instantiate a SimulationModel with specified survival distribution, risk type, and parameters. Default values are used if not provided. ```python SimulationModel( survival_distribution = 'exponential', risk_type = 'linear', censored_parameter = 1., alpha = 1, beta = 1., bins = 100, risk_parameter = 1.) ``` -------------------------------- ### SmoothKaplanMeierModel Initialization Source: https://www.pysurvival.io/models/smooth_kaplan_meier.html Initializes the SmoothKaplanMeierModel with specified bandwidth and kernel type. ```APIDOC ## SmoothKaplanMeierModel ### Description Initializes the estimator with a specified bandwidth and kernel. ### Parameters - **bandwidth** (double) - Optional (default=0.1) - Controls the degree of smoothing. Smaller values result in a curve closer to the original Kaplan Meier estimator but increase computation time. Larger values result in a smoother curve but may decrease accuracy. - **kernel** (str) - Optional (default='normal') - Defines the type of kernel to use. Options include: 'Uniform', 'Epanechnikov', 'Normal', 'Biweight', 'Triweight', 'Cosine'. ``` -------------------------------- ### Set C++ and C environment variables on MacOS Source: https://www.pysurvival.io/installation.html Assign the paths for the C++ and C compilers to the CC and CXX environment variables on MacOS. Ensure to use the version number that matches your GCC installation. ```bash export CXX=/usr/local/Cellar/gcc/8.2.0/bin/g++-8 export CC=/usr/local/Cellar/gcc/8.2.0/bin/gcc-8 ``` -------------------------------- ### Prepare Training and Testing Datasets Source: https://www.pysurvival.io/models/linear_mtlr.html Splits the generated dataset into training and testing sets, and extracts features, time, and event columns for model input. ```python features = sim.features index_train, index_test = train_test_split( range(N), test_size = 0.2) data_train = dataset.loc[index_train].reset_index( drop = True ) data_test = dataset.loc[index_test].reset_index( drop = True ) X_train, X_test = data_train[features], data_test[features] T_train, T_test = data_train['time'].values, data_test['time'].values E_train, E_test = data_train['event'].values, data_test['event'].values ``` -------------------------------- ### Set C++ and C environment variables on CentOS 7 Source: https://www.pysurvival.io/installation.html Assign the paths for the C++ and C compilers to the CC and CXX environment variables on CentOS 7. Adjust the path based on the installed GCC version. ```bash export CXX=/opt/rh/devtoolset-8/root/usr/bin/x86_64-redhat-linux-g++ export CC=/opt/rh/devtoolset-8/root/usr/bin/x86_64-redhat-linux-gcc ``` -------------------------------- ### Initialize KernelSVMModel Source: https://www.pysurvival.io/models/kernel_svm.html Instantiate the KernelSVMModel with specified kernel and parameters. The `auto_scaler` parameter, when True, automatically scales the input features. ```python KernelSVMModel(kernel = "gaussian", scale=1., offset=0., degree=1., auto_scaler = True) ``` -------------------------------- ### LinearMultiTaskModel Initialization Source: https://www.pysurvival.io/models/linear_mtlr.html Initializes the LinearMultiTaskModel with specified parameters for time discretization and automatic scaling. ```APIDOC ## `__init__` - Initialization ```python LinearMultiTaskModel(bins=100, auto_scaler=True) ``` ### Parameters: * `bins`: **int** _(default=100)_ -- Number of subdivisions of the time axis * `auto_scaler`: **bool** _(default=True)_ -- Determines whether a sklearn scaler should be automatically applied ``` -------------------------------- ### Creating and fitting the Linear SVM model Source: https://www.pysurvival.io/models/linear_svm.html Instantiates a LinearSVMModel and fits it to the training data (X_train, T_train, E_train). Customizes the fitting process with specific initialization methods, learning rate, and regularization parameters. ```python svm_model = LinearSVMModel() svm_model.fit(X_train, T_train, E_train, init_method='he_uniform', with_bias = True, lr = 0.5, tol = 1e-3, l2_reg = 1e-3) ``` -------------------------------- ### Initialize and Generate Simulation Data Source: https://www.pysurvival.io/models/random_survival_forest.html Initializes a SimulationModel with specified survival and risk parameters, then generates synthetic data for training and testing. Use this to create realistic survival data for model development. ```python from pysurvival.models.simulation import SimulationModel # Initializing the simulation model sim = SimulationModel( survival_distribution = 'exponential', risk_type = 'linear', censored_parameter = 1, alpha = 3) # Generating N random samples N = 1000 dataset = sim.generate_data(num_samples = N, num_features=4) ``` -------------------------------- ### Initialize RandomSurvivalForestModel Source: https://www.pysurvival.io/models/random_survival_forest.html Initializes the RandomSurvivalForestModel with a specified number of trees. The default is 10 trees. ```python RandomSurvivalForestModel(num_trees = 10) ``` -------------------------------- ### Initialize and Generate Simulation Data Source: https://www.pysurvival.io/models/linear_mtlr.html Initializes a SimulationModel with Weibull survival distribution and linear risk type, then generates a dataset with specified number of samples and features. ```python sim = SimulationModel( survival_distribution = 'Weibull', risk_type = 'linear', censored_parameter = 10.0, alpha = .01, beta = 3.0 ) N = 1000 dataset = sim.generate_data(num_samples = N, num_features = 3) ``` -------------------------------- ### Initialize and Generate Simulation Data Source: https://www.pysurvival.io/models/parametric.html Initializes a SimulationModel with specified parameters and generates a dataset. This is useful for creating synthetic data for testing survival models. ```python sim = SimulationModel( survival_distribution = 'Gompertz', risk_type = 'linear', censored_parameter = 10.0, alpha = .01, beta = 3.0 ) N = 1000 dataset = sim.generate_data(num_samples = N, num_features = 3) ``` -------------------------------- ### Instantiate and Fit Kernel SVM Model Source: https://www.pysurvival.io/models/kernel_svm.html Creates an instance of the KernelSVMModel with specified kernel and scale, then fits the model to the training data. Adjust 'lr', 'tol', and 'l2_reg' for optimal performance. ```python svm_model = KernelSVMModel(kernel='Gaussian', scale=0.25) svm_model.fit(X_train, T_train, E_train, init_method='orthogonal', with_bias = True, lr = 0.8, tol = 1e-3, l2_reg = 1e-4) ``` -------------------------------- ### Initialize ExtraSurvivalTreesModel Source: https://www.pysurvival.io/models/extra_survival_trees.html Initializes the ExtraSurvivalTreesModel with a specified number of trees. The default is 10 trees. ```python ExtraSurvivalTreesModel(num_trees = 10) ``` -------------------------------- ### Import Libraries and Load Dataset Source: https://www.pysurvival.io/tutorials/churn.html Imports necessary libraries and loads the churn dataset using PySurvival. Displays the shape and first two rows of the raw dataset. ```python # Importing modules import pandas as pd import numpy as np from matplotlib import pyplot as plt from pysurvival.datasets import Dataset %pylab inline # Reading the dataset raw_dataset = Dataset('churn').load() print("The raw_dataset has the following shape: {}.".format(raw_dataset.shape)) raw_dataset.head(2) ``` -------------------------------- ### Load and Inspect Maintenance Dataset Source: https://www.pysurvival.io/tutorials/maintenance.html Imports necessary libraries and loads the 'maintenance' dataset. Displays the shape and the first few rows of the raw data. ```python # Importing modules import pandas as pd import numpy as np from matplotlib import pyplot as plt from pysurvival.datasets import Dataset %pylab inline # Reading the dataset raw_dataset = Dataset('maintenance').load() print("The raw_dataset has the following shape: {}.".format(raw_dataset.shape)) raw_dataset.head(3) ``` -------------------------------- ### Generating a simulated dataset Source: https://www.pysurvival.io/models/neural_mtlr.html Creates a dataset using a Log-Logistic parametric model with Gaussian risk. Ensure `num_samples` and `num_features` are set appropriately for your needs. ```python # Initializing the simulation model sim = SimulationModel( survival_distribution = 'Log-Logistic', risk_type = 'gaussian', censored_parameter = 10.1, alpha = 0.1, beta=3.2 ) # Generating N random samples N = 1000 dataset = sim.generate_data(num_samples = N, num_features = 3) # Showing a few data-points dataset.head(2) ``` -------------------------------- ### RandomSurvivalForestModel Initialization Source: https://www.pysurvival.io/models/random_survival_forest.html Initializes the RandomSurvivalForestModel with a specified number of trees. ```APIDOC ## RandomSurvivalForestModel ### Description Initializes the estimator. ### Method Signature ```python RandomSurvivalForestModel(num_trees = 10) ``` ### Parameters * **num_trees** (int) - Optional - number of trees that will be built in the forest. Defaults to 10. ``` -------------------------------- ### Generate Simulated Dataset Source: https://www.pysurvival.io/models/coxph.html Initializes a simulation model with specified survival distribution and risk type, then generates a dataset with a given number of samples and features. The 'head(2)' call displays the first two data points. ```python # Initializing the simulation model sim = SimulationModel( survival_distribution = 'log-logistic', risk_type = 'linear', censored_parameter = 10.1, alpha = 0.1, beta=3.2 ) # Generating N random samples N = 1000 dataset = sim.generate_data(num_samples = N, num_features = 4) # Showing a few data-points dataset.head(2) ``` -------------------------------- ### Fitting the ExtraSurvivalTreesModel Source: https://www.pysurvival.io/models/extra_survival_trees.html Initializes and fits the ExtraSurvivalTreesModel using the training data. Hyperparameters like `max_features`, `max_depth`, `min_node_size`, and `num_random_splits` can be tuned for optimal performance. ```python # Building the model xst = ExtraSurvivalTreesModel(num_trees=200) xst.fit(X_train, T_train, E_train, max_features="sqrt", max_depth=5, min_node_size=20, num_random_splits = 1000) ``` -------------------------------- ### Compare Individual Repayment Speed Functions Source: https://www.pysurvival.io/tutorials/credit_risk.html Visualizes and compares the cumulative density functions (speed of repayment) for individuals across different risk groups. It highlights actual event times. ```python # Initializing the figure fig, ax = plt.subplots(figsize=(15, 5)) # Selecting a random individual that experienced an event from each group groups = [] for i, (label, (color, indexes)) in enumerate(risk_groups.items()) : # Selecting the individuals that belong to this group if len(indexes) == 0 : continue X = X_test.values[indexes, :] T = T_test.values[indexes] E = E_test.values[indexes] # Randomly extracting an individual that experienced an event choices = np.argwhere((E==1.)).flatten() if len(choices) == 0 : continue k = np.random.choice( choices, 1)[0] # Saving the time of event t = T[k] # Computing the CDF for all times t cdf = 1. - neural_mtlr.predict_survival(X[k, :]).flatten() # Displaying the functions label_ = '{} risk'.format(label) plt.plot(neural_mtlr.times, cdf, color = color, label=label_, lw=2) groups.append(label) # Actual time plt.axvline(x=t, color=color, ls ='--') ax.annotate('T={:.1f}'.format(t), xy=(t, 0.5*(1.+0.2*i)), xytext=(t, 0.5*(1.+0.2*i)), fontsize=12) # Show everything groups_str = ', '.join(groups) title = "Comparing cumulative density functions between {} risk grades" title = title.format(groups_str) plt.legend(fontsize=12) plt.title(title, fontsize=15) plt.xlim(0, 65) plt.ylim(0, 1.05) plt.show() ``` -------------------------------- ### SimulationModel Initialization Source: https://www.pysurvival.io/models/simulations.html Initializes a SimulationModel instance with specified survival distribution, risk type, and parameters. ```APIDOC ## SimulationModel ### Description Initializes a SimulationModel instance. ### Method __init__ ### Parameters - **survival_distribution** (string) - Optional (default = 'exponential') - Defines a known survival distribution. Available distributions: Exponential, Weibull, Gompertz, Log-Logistic, Log-Normal. - **risk_type** (string) - Optional (default='linear') - Defines the type of risk function: Linear, Square, Gaussian. - **censored_parameter** (double) - Optional (default = 1.) - Coefficient used to calculate the censored distribution (Normal distribution N(loc=censored_parameter, scale=5)). - **alpha** (double) - Optional (default = 1.) - The scale parameter. - **beta** (double) - Optional (default = 1.) - The shape parameter. - **bins** (int) - Optional (default=100) - The number of bins of the time axis. - **risk_parameter** (double) - Optional (default = 1.) - Scaling coefficient for the risk score. For linear: r(x)=exp(x⋅ω). For square: r(x)=exp(risk_parameter∗(x⋅ω)^2). For gaussian: r(x)=exp(e^-(x⋅ω)^2∗risk_parameter). ### Request Example ```python from pysurvival.models.simulations import SimulationModel model = SimulationModel( survival_distribution='weibull', risk_type='gaussian', alpha=2.0, beta=1.5, risk_parameter=0.5, bins=50 ) ``` ``` -------------------------------- ### ExtraSurvivalTreesModel Initialization Source: https://www.pysurvival.io/models/extra_survival_trees.html Initializes the ExtraSurvivalTreesModel with a specified number of trees. ```APIDOC ## ExtraSurvivalTreesModel(num_trees = 10) ### Description Initializes the estimator. ### Parameters * `num_trees` (int) - Optional - number of trees that will be built in the forest. Defaults to 10. ``` -------------------------------- ### Parametric Model Initialization Source: https://www.pysurvival.io/models/parametric.html Initializes a parametric model with specified bins and auto-scaler settings. The auto_scaler determines if a sklearn scaler is automatically applied. ```python __init__(bins=100, auto_scaler=True) ``` -------------------------------- ### Generate simulated dataset Source: https://www.pysurvival.io/models/kernel_svm.html Initialize a SimulationModel with specified survival distribution and risk type, then generate N random samples with 4 features. The `dataset.head(2)` call displays the first two generated data points. ```python # Initializing the simulation model sim = SimulationModel( survival_distribution = 'Log-Logistic', risk_type = 'square', censored_parameter = 1.1, alpha = 1.5, beta = 4) # Generating N Random samples N = 1000 dataset = sim.generate_data(num_samples = N, num_features = 4) # Showing a few data-points dataset.head(2) ``` -------------------------------- ### Prepare Training and Testing Data Source: https://www.pysurvival.io/models/coxph.html Splits the generated dataset into training and testing sets. It then separates features (X) from survival time (T) and event indicators (E) for both sets. ```python # Defining the features features = sim.features # Building training and testing sets # index_train, index_test = train_test_split( range(N), test_size = 0.2) data_train = dataset.loc[index_train].reset_index( drop = True ) data_test = dataset.loc[index_test].reset_index( drop = True ) # Creating the X, T and E input X_train, X_test = data_train[features], data_test[features] T_train, T_test = data_train['time'].values, data_test['time'].values E_train, E_test = data_train['event'].values, data_test['event'].values ``` -------------------------------- ### Create and fit ConditionalSurvivalForestModel Source: https://www.pysurvival.io/models/conditional_survival_forest.html Initializes a ConditionalSurvivalForestModel with 200 trees and fits it to the training data using specified hyperparameters for tree construction. ```python # Building the model csf = ConditionalSurvivalForestModel(num_trees=200) csf.fit(X_train, T_train, E_train, max_features="sqrt", max_depth=5, min_node_size=20, alpha = 0.05, minprop=0.1) ``` -------------------------------- ### Compare Model Predictions to Actual Values (Active Loans) Source: https://www.pysurvival.io/miscellaneous/tips.html This function visualizes the time series of actual and predicted numbers of active loans. It's similar to the repaid loans comparison but focuses on loans that are still active. Ensure you provide the correct model and test data. ```python results = compare_to_actual(neural_mtlr, X_test, T_test, E_test, is_at_risk = True, figure_size=(16, 6), metrics = ['rmse', 'mean', 'median']) ``` -------------------------------- ### Compare Overall Churn Predictions Source: https://www.pysurvival.io/tutorials/churn.html Compares the time series of actual and predicted churned customers using the `compare_to_actual` utility. Requires the trained model, test data (features, survival times, and event indicators), and specifies metrics for comparison. ```python from pysurvival.utils.display import compare_to_actual results = compare_to_actual(csf, X_test, T_test, E_test, is_at_risk = False, figure_size=(16, 6), metrics = ['rmse', 'mean', 'median']) ``` -------------------------------- ### Import necessary libraries Source: https://www.pysurvival.io/models/conditional_survival_forest.html Imports all required libraries for data manipulation, modeling, and visualization. ```python import numpy as np import pandas as pd from matplotlib import pyplot as plt from sklearn.model_selection import train_test_split from pysurvival.models.simulations import SimulationModel from pysurvival.models.survival_forest import ConditionalSurvivalForestModel from pysurvival.utils.metrics import concordance_index from pysurvival.utils.display import integrated_brier_score %pylab inline ``` -------------------------------- ### Initialize ConditionalSurvivalForestModel Source: https://www.pysurvival.io/models/conditional_survival_forest.html Initializes the ConditionalSurvivalForestModel with a specified number of trees. The default is 10 trees. ```python ConditionalSurvivalForestModel(num_trees = 10) ``` -------------------------------- ### NonLinearCoxPH Initialization Source: https://www.pysurvival.io/models/nonlinear_coxph.html Initializes the NonLinearCoxPH model with a specified MLP structure and optional auto-scaler. ```APIDOC ## NonLinearCoxPH(structure, auto_scaler=True) ### Description Initializes the NonLinearCoxPH model. ### Parameters * `structure`: **list of dictionaries** -- Provides the structure of the MLP within the model. ex: `structure = [ {'activation': 'ReLU', 'num_units': 128}, {'activation': 'Tanh', 'num_units': 128}, ]`. Each dictionary corresponds to a fully connected hidden layer: * `num_units` is the number of hidden units in this layer * `activation` is the activation function that will be used. The list of all available activation functions can be found here. In case there are more than one dictionary, each hidden layer will be applied in the resulting MLP, using the order it is provided in the structure . * `auto_scaler`: **boolean** _(default=True)_ -- Determines whether a sklearn scaler should be automatically applied ``` -------------------------------- ### Generate Survival Data with SimulationModel Source: https://www.pysurvival.io/models/simulations.html Initializes a SimulationModel with specified survival distribution, risk type, and parameters, then generates a synthetic dataset. Use this to create sample data for testing survival analysis models. ```python import pandas as pd from pysurvival.models.simulations import SimulationModel %pylab inline # Initializing the simulation model sim = SimulationModel( survival_distribution = 'gompertz', risk_type = 'linear', censored_parameter = 5.0, alpha = 0.01, beta = 5., ) # Generating N Random samples N = 1000 dataset = sim.generate_data(num_samples = N, num_features=5) # Showing a few data-points dataset.head(2) ``` -------------------------------- ### Configure Matplotlib backend on MacOS Source: https://www.pysurvival.io/installation.html This command creates or appends to the matplotlibrc file to set the backend to TkAgg, resolving potential Matplotlib issues on MacOS. ```bash cd echo "backend: TkAgg" >> ~/.matplotlib/matplotlibrc ``` -------------------------------- ### Generating dataset from a nonlinear Weibull model Source: https://www.pysurvival.io/models/nonlinear_coxph.html Initializes a SimulationModel with Weibull distribution and Gaussian risk type, then generates synthetic data. Use this to create realistic survival data for testing models. ```python # Initializing the simulation model sim = SimulationModel( survival_distribution = 'weibull', risk_type = 'gaussian', censored_parameter = 2.1, alpha = 0.1, beta=3.2 ) # Generating N random samples N = 1000 dataset = sim.generate_data(num_samples = N, num_features=3) # Showing a few data-points dataset.head(2) ``` -------------------------------- ### Fit Gompertz Parametric Model Source: https://www.pysurvival.io/models/parametric.html Initializes and fits a Gompertz parametric model to the training data. Various optimization parameters like learning rate, optimizer, regularization, and epochs can be specified. ```python from pysurvival.models.parametric import GompertzModel gomp_model = GompertzModel() gomp_model.fit(X_train, T_train, E_train, lr=1e-2, init_method='zeros', optimizer ='adam', l2_reg = 1e-3, num_epochs=2000) ``` -------------------------------- ### Displaying baseline simulations Source: https://www.pysurvival.io/models/nonlinear_coxph.html Visualizes the base survival function of the simulation model. This helps understand the underlying survival characteristics of the generated data. ```python from pysurvival.utils.display import display_baseline_simulations display_baseline_simulations(sim, figure_size=(10, 5)) ``` -------------------------------- ### Fit KernelSVMModel Source: https://www.pysurvival.io/models/kernel_svm.html Fit the Kernel SVM model to the provided data. Ensure `X`, `T`, and `E` are correctly formatted. The `init_method` parameter controls the weight initialization strategy. ```python fit(X, T, E, with_bias = True, init_method='glorot_normal', lr = 1e-2, max_iter = 100, l2_reg = 1e-4, tol = 1e-3, verbose = True) ``` -------------------------------- ### Compare Model Predictions to Actual Values (Repaid Loans) Source: https://www.pysurvival.io/miscellaneous/tips.html Use this function to visualize the time series of actual and predicted numbers of fully repaid loans. It requires the trained model, test data features, survival times, and event indicators. Metrics like RMSE, mean, and median can be calculated. ```python from pysurvival.utils.display import compare_to_actual results = compare_to_actual(neural_mtlr, X_test, T_test, E_test, is_at_risk = False, figure_size=(16, 6), metrics = ['rmse', 'mean', 'median']) ``` -------------------------------- ### Import Libraries and Load Dataset Source: https://www.pysurvival.io/tutorials/employee_retention.html Imports necessary libraries (pandas, numpy, matplotlib) and the PySurvival Dataset module. Loads the 'employee_attrition' dataset and prints its shape. Use this to begin data loading and initial inspection. ```python # Importing modules import pandas as pd import numpy as np from matplotlib import pyplot as plt from pysurvival.datasets import Dataset %pylab inline # Reading the dataset raw_dataset = Dataset('employee_attrition').load() print("The raw_dataset has the following shape: {}.".format(raw_dataset.shape)) raw_dataset.head(3) ``` -------------------------------- ### ConditionalSurvivalForestModel Initialization Source: https://www.pysurvival.io/models/conditional_survival_forest.html Initializes the Conditional Survival Forest model with a specified number of trees. ```APIDOC ## ConditionalSurvivalForestModel ### Description Initializes the estimator. ### Parameters * `num_trees` (int, default=10) - The number of trees to build in the forest. ``` -------------------------------- ### Display Baseline Simulation Data and Survival Function Source: https://www.pysurvival.io/models/random_survival_forest.html Displays the first few data points of the generated dataset and visualizes the base survival function of the simulation model. This helps in understanding the underlying data distribution and model assumptions. ```python from pysurvival.utils.display import display_baseline_simulations # Showing a few data-points print(dataset.head(2)) # Displaying the Base Survival function of the Simulation model display_baseline_simulations(sim, figure_size=(20, 6)) ``` -------------------------------- ### RandomSurvivalForestModel Instantiation Source: https://www.pysurvival.io/models/random_survival_forest.html To create an instance of the Random Survival Forest model, use the `RandomSurvivalForestModel` class from `pysurvival.models.survival_forest`. ```APIDOC ## Instantiate RandomSurvivalForestModel ### Description Creates an instance of the Random Survival Forest model. ### Method ```python RandomSurvivalForestModel() ``` ### Parameters This method does not take any parameters for instantiation. ``` -------------------------------- ### Fit Cox PH Model Source: https://www.pysurvival.io/models/coxph.html Initializes a CoxPHModel and fits it to the training data. Parameters like learning rate (lr), L2 regularization (l2_reg), and initialization method (init_method) can be specified during fitting. ```python # Building the model coxph = CoxPHModel() coxph.fit(X_train, T_train, E_train, lr=0.5, l2_reg=1e-2, init_method='zeros') ``` -------------------------------- ### Prepare Data for Kernel SVM Source: https://www.pysurvival.io/models/kernel_svm.html Splits the dataset into training and testing sets and extracts features, time, and event data. Ensure 'features', 'time', and 'event' columns exist in your dataset. ```python index_train, index_test = train_test_split( range(N), test_size = 0.2) data_train = dataset.loc[index_train].reset_index( drop = True ) data_test = dataset.loc[index_test].reset_index( drop = True ) X_train, X_test = data_train[features], data_test[features] T_train, T_test = data_train['time'].values, data_test['time'].values E_train, E_test = data_train['event'].values, data_test['event'].values ```