Getting Started with PySurvival: An Open Source Python Package for Survival Analysis

Nov 21, 2022 | Data Science

pysurvival_logo

What is PySurvival?

PySurvival is an open source Python package designed specifically for Survival Analysis modeling—the concept used to analyze or predict when an event is likely to happen. Built on the robust foundations of popular machine learning packages such as NumPy, SciPy, and PyTorch, it ensures reliable and efficient modeling. PySurvival is compatible with Python versions 2.7 through 3.7.

For further exploration, you can find the documentation here.

Key Features of PySurvival

PySurvival makes it easy for enthusiasts and professionals alike to navigate through the theoretical aspects of Survival Analysis while providing detailed guidance on how to implement a full analysis. Here are the features it offers:

Installation Guide

If you have a working version of gcc on your machine, installing PySurvival is a breeze. Use the following command:

pip install pysurvival

To explore the complete installation steps, refer to the documentation here.

Getting Started with Modeling

PySurvival’s straightforward API offers an exceptional user experience. Below is a modeling example to help you dive right in:

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(f"CoxPH model c-index = {c_index1:.2f}")
c_index2 = concordance_index(model=mtlr, X=X_test, T=T_test, E=E_test)
print(f"MTLR model c-index = {c_index2:.2f}")

Understanding the Code Using an Analogy

Imagine you’re the captain of a ship navigating through unknown waters. Before journeying into the unknown, you would want to gather your crew (load modules), chart your course (load and split datasets), prepare your ship (build models), and then set sail, checking for any rough waters along the way (assess model performance).

The code above plays out like this journey:

  • **Loading Modules**: Like gathering your crew, you’re importing essential models and tools to prepare for the analysis.
  • **Loading and Splitting Data**: This step is akin to charting your course, determining where you’ll be starting and how you’ll navigate the data.
  • **Building Models**: Just as you would prepare your ship, here you’re training your models to ready them for performance evaluation.
  • **Checking Performance**: Finally, inspecting your vessel and its performance against the conditions of the sea is like evaluating the models’ effectiveness with the c-index.

Troubleshooting

If you run into issues during installation or model building, consider the following troubleshooting tips:

  • Ensure your Python version is compatible (2.7-3.7).
  • Verify that all dependencies such as NumPy and PyTorch are installed correctly.
  • Make sure you are using the correct initialization methods and optimizers as specified in the documentation.
  • Check your datasets and ensure they are properly formatted for analysis.
  • If issues persist, visiting the official PySurvival documentation may provide additional assistance.
  • For more insights, updates, or to collaborate on AI development projects, stay connected with fxis.ai.

Conclusion

At fxis.ai, we believe that such advancements are crucial for the future of AI, as they enable more comprehensive and effective solutions. Our team is continually exploring new methodologies to push the envelope in artificial intelligence, ensuring that our clients benefit from the latest technological innovations.

Stay Informed with the Newest F(x) Insights and Blogs

Tech News and Blog Highlights, Straight to Your Inbox